arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Set and reset led on-off time by checking status of led using timer

Mon Jul 15, 2019 11:03 am

Hi,

I am getting a binary series of 0 and 1 as input.I want to make program for raspberry pi 3 using timer in python. Here timer checks status of 0's and 1's. I want led to turn on for 10 seconds when 1 is detected and if again it detects 1 in between 10 seconds this 10 sec time reset and again set to 10 sec.And if in between 10 sec it only detects 0's then led will turn off and so on.

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Set and reset led on-off time by checking status of led using timer

Mon Jul 15, 2019 12:48 pm

Here's an example of a 10 second counter triggered by a push button switch in this case, which when triggered starts counting down to zero , but if the button is pressed again timer is reset to 10.

Instead of the push button you could use your input just remove the pul-lup, and you can add your LED output instead of the prints statements.

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
start = 0

def my_callback(channel):
    global start
    print("Input detected")
    start = 10 + (time.time())
    

GPIO.add_event_detect(21, GPIO.FALLING, callback=my_callback)


while True:
    if start ==0:
        print("waitng for trigger")
     
    else:
        now = time.time()
        seconds = start - now
        if seconds > 0:
            print (int(seconds))
            
        else:
            print ("time up")
            start = 0
    time.sleep(0.2)
    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: Set and reset led on-off time by checking status of led using timer

Tue Jul 16, 2019 11:27 am

Hi pcmanbob,
Thanks for the reply.
I forgot to mention that incoming data is random hex data program compares one data with next data as per that if next data is greater than or less than previous data it sets flag as 1 otherwise 0. I want my led to start while flag 1 is detected. My led is on pin number 12. Here as from flag detected i have to make led on for 10 sec or off as per i said in my question.How can i call callback function because i have no input like you have used button on pin 21.
Last edited by arpitbt on Tue Jul 16, 2019 12:00 pm, edited 1 time in total.

pcmanbob
Posts: 6934
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Set and reset led on-off time by checking status of led using timer

Tue Jul 16, 2019 11:43 am

Then you simply need to write your program to compare the hex data and then output a 1 or 0 to a variable and check that variable instead of the gpio.

if you already know how to connect and use an LED then you simple need to add the code to my example program in place of the print statements.

EDIT.....Something like this

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
start = 0
   
while True: 
    # put your code for receiving the hex code and doing what  processing you want here
    # |
    # |
    # |
    #  make the result of your processing output a 1 or 0 to the variable hexans 
    
    if hexans ==1:
        start = 10 + (time.time())

    if start ==0:
        print("waitng for trigger")
     
    else:
        now = time.time()
        seconds = start - now
        if seconds > 0:
            print (int(seconds))
            GPIO.output(18, GPIO.HIGH)
            
        else:
            print ("time up")
            GPIO.output(18, GPIO.LOW)
            start = 0
    time.sleep(0.2)
    
untested.

of course the hex download and processing may affect the timing of your LED on/off depending how long it takes.
Last edited by pcmanbob on Tue Jul 16, 2019 12:03 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”