Page 1 of 1

Dynamic Timer Logic

Posted: Mon Feb 20, 2017 8:44 pm
by biosboy4
Hello,

I am trying to sound an alarm whenever my laser/lightsensor is blocked for more than 60 seconds. What might this logic look like?'

I haven't been able to make a while loop work with and if and a sleep command.

I need it to be something like:

if sensor is blocked, count 60 seconds and sound the alarm.

However, I need the value of the lightsensor to be able to reset the counter.

Thanks for your direction!

biosboy4

Re: Dynamic Timer Logic

Posted: Mon Feb 20, 2017 9:30 pm
by pcmanbob
Hi.

So I assume you are monitoring a GPIO input so you know when your laser/lightsensor is blocked.
so when the input goes high ( assuming high is alarm state) you run this .
were GPIO.input(x) is your monitoring GPIO input.

Code: Select all

alarm = gpio.input(x) #this is the pin you are using to monitor your sensor goes high on alarm
count = 0
while alarm = 1:
	if count = 60:
		print "alarm"
		alarm = 0
	else:
		count = count + 1
		time.sleep(1)
		alarm = gpio.input(x)
so while the alarm = 1 it will check to see if count = 60 if it does it sounds the alarm and exits the loop by setting alarm = 0
but if count is less than 60 then it adds 1 to the count , sleeps for 1 sec then reads the GPIO input again , if its still 1 the loop continues, if how ever the input is now 0 the loop will end.
please not I have not tested this its just off the top of my head.

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 12:02 am
by biosboy4
I think you're awesome, thank you!

I'm trying it right now. :)

Edit: I am getting a random syntax error at the equal sign (alarm = 1) Thanks for you help! Here is the code:

Code: Select all

from gpiozero import LightSensor 
from time import sleep 
import pygame




ldr = LightSensor(4)

pygame.init()
pygame.mixer.init()


buzz = pygame.mixer.Sound('/home/pi/Downloads/Buzz2.ogg')
length = buzz.get_length()

alarm = gpio.input(4)
alarm
count = 0




while   alarm = 1:
        if count = 60:
        buzz.play()
        sleep(10.1)
        buzz.stop()
        alarm = 0
    else:
        count = count + 1
        time.sleep(1)
        alarm = 0





Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 5:52 am
by ghp
Hello,
use "while alarm == 1:" to check for the value.
And the identation does not look perfekt the next line.
You have:

Code: Select all

while   alarm = 1:
        if count = 60:
        buzz.play()
Most possibly it should be

Code: Select all

while   alarm == 1:
    if count == 60:
        buzz.play()
Regards,
Gerhard

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 10:41 am
by pcmanbob
biosboy4 wrote:I think you're awesome, thank you!

I'm trying it right now. :)

Edit: I am getting a random syntax error at the equal sign (alarm = 1) Thanks for you help! Here is the code:

Code: Select all

from gpiozero import LightSensor 
from time import sleep 
import pygame




ldr = LightSensor(4)

pygame.init()
pygame.mixer.init()


buzz = pygame.mixer.Sound('/home/pi/Downloads/Buzz2.ogg')
length = buzz.get_length()

alarm = gpio.input(4)
alarm
count = 0




while   alarm = 1:
        if count = 60:
        buzz.play()
        sleep(10.1)
        buzz.stop()
        alarm = 0
    else:
        count = count + 1
        time.sleep(1)
        alarm = 0




I did say it was only an example and your version is not correct with the spacing either, and you did not do the up date of alarm as I showed in my example.
so I have reworked your example code, try this.

Code: Select all

buzz = pygame.mixer.Sound('/home/pi/Downloads/Buzz2.ogg')
length = buzz.get_length()

alarm = gpio.input(4)

count = 0

while alarm == 1:
		if count == 60:
			buzz.play()
			sleep(10.1)
			buzz.stop()
			alarm = 0
		else:
			count = count + 1
			time.sleep(1)
			alarm = gpio.input(4)
As before I have not tested this

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 4:50 pm
by biosboy4
I really appreciate all the help guys. However, due to my absolute newness to programming, I'm having a difficult time following.

Basically, I am tying to make the alarm sound ONLY when the lightsensor is blocked for a solid/consecutive 60 seconds.

Here is my current draft of code:

Code: Select all

from gpiozero import LightSensor 
from time import sleep 
import pygame
import RPi.GPIO as gpio





ldr = LightSensor(4)

pygame.init()
pygame.mixer.init()



buzz = pygame.mixer.Sound('/home/pi/Downloads/Buzz2.ogg')
length = buzz.get_length()

sleep (0.1)

if ldr.value < 0.8:
    alarm = 1
else:
    alarm = 0

count = 0

while alarm == 1:
      if count == 7:
         buzz.play()
         sleep(10.1)
         buzz.stop()
         alarm = 0
      else:
         count = count + 1
         sleep(1)
         alarm = gpio.input(4)
Thanks again guys; I apologize for my noobishness. This is the first code I have ever tried to write, lol.

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 7:49 pm
by biosboy4
It seems like it would be a lot easier like this, right? (Although it isn't working, the shell is just chillin when I run it..)

Code: Select all

from gpiozero import LightSensor 
from time import sleep 
import pygame
import RPi.GPIO as gpio

ldr = LightSensor(4)

pygame.init()
pygame.mixer.init()

buzz = pygame.mixer.Sound('/home/pi/Downloads/Buzz2.ogg')
length = buzz.get_length()

sleep (0.1)

while ldr.value < 0.9:

     if ldr.value < 0.9:

        sleep(7.1)
        buzz.play()
        buzz.stop()

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 9:01 pm
by pcmanbob
Ok seeing as you could not get the code to work a have tested this and it works as per your request.

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.IN)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.LOW)

while True:
	alarm = GPIO.input(4)
	print "alarm = " , alarm
	count = 0
	time.sleep(1)
	while alarm == 1:
		print "count = " , count
		if count == 10:			# set number of seconds delay before alarm sounds
			print "alarm on"
			GPIO.output(18, GPIO.HIGH)
			time.sleep(5)		# set how long alarm souns for
			print "alarm off"
			GPIO.output(18, GPIO.LOW)
			
			# for the alarm to cycle while ever the sensor input is high un-coment the line below
			#alarm = 0
			
			# for the alarm to only sound once while ever the sensor input is high un-coment the lines below
			alarm = GPIO.input(4)
			count = count + 1
			
		else:
			count = count + 1
			time.sleep(1)
			alarm = GPIO.input(4)
So this code is set so that it runs the alarm sequence when the input on GPIO 4 goes High.

so the code is tested and works in both modes.
if you want the alarm to only sound once for each input trigger then leave the code as it is, but if you want the the alarm to sound then wait for the prescribed time then sound again then you need to un-coment the line line and comment out the lines below it.

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 9:46 pm
by biosboy4
This code seems to be working except that the counter doesn't ever reset. Could it be too much ambient light on the sensor?

Code: Select all

from gpiozero import LightSensor
from time import sleep 
import RPi.GPIO as GPIO
import time
import pygame

pygame.init()
pygame.mixer.init()



buzz = pygame.mixer.Sound('/home/pi/Downloads/Buzz2.ogg')
length = buzz.get_length()




GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.IN)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.LOW)

while True:
   alarm = GPIO.input(4)
   print "alarm = " , alarm
   count = 0
   time.sleep(1)
   while alarm == 1:
      print "count = " , count
      if count == 10:         # set number of seconds delay before alarm sounds
         print "alarm on"
         GPIO.output(18, GPIO.HIGH)
         buzz.play()
         sleep (14.7)
         buzz.stop()
         time.sleep(5)      # set how long alarm souns for
         print "alarm off"
         GPIO.output(18, GPIO.LOW)
         
         # for the alarm to cycle while ever the sensor input is high un-coment the line below
         alarm = 0
         
         # for the alarm to only sound once while ever the sensor input is high un-coment the lines below
         alarm = GPIO.input(4)
         count = count + 1
         
      else:
         count = count + 1
         time.sleep(1)
         alarm = GPIO.input(4)

Re: Dynamic Timer Logic

Posted: Tue Feb 21, 2017 10:48 pm
by pcmanbob
you obviously did not read my post and understand it, you can't have both these lines set.

Code: Select all

         # for the alarm to cycle while ever the sensor input is high un-coment the line below
         alarm = 0
         
         # for the alarm to only sound once while ever the sensor input is high un-coment the lines below
         alarm = GPIO.input(4)
         count = count + 1
how simple does it have to be, these are the 2 options choose which one you want to use.

you either use it like this

Code: Select all

         # for the alarm to cycle while ever the sensor input is high un-coment the line below
         #alarm = 0
         
         # for the alarm to only sound once while ever the sensor input is high un-coment the lines below
         alarm = GPIO.input(4)
         count = count + 1
or you use it like this

Code: Select all

        # for the alarm to cycle while ever the sensor input is high un-coment the line below
         alarm = 0
         
         # for the alarm to only sound once while ever the sensor input is high un-coment the lines below
         #alarm = GPIO.input(4)
         #count = count + 1
Note the different # placings
and finally the code works when tested with a switch if you sensor is high all the time then no it will never reset, I suggest you test your sensor it see if it ever goes low.

Re: Dynamic Timer Logic

Posted: Wed Feb 22, 2017 6:46 am
by rpdom
pcmanbob wrote:you obviously did not read my post and understand it, you can't have both these lines set.

Code: Select all

         # for the alarm to cycle while ever the sensor input is high un-coment the line below
         alarm = 0
         
         # for the alarm to only sound once while ever the sensor input is high un-coment the lines below
         alarm = GPIO.input(4)
         count = count + 1
Well, you can have both those lines set, but the "alarm = GPIO.input(4)" will make the "alarm = 0" redundant. You only really need to do as the instructions say and comment or uncomment the last two lines shown there.