Murphyboy
Posts: 4
Joined: Fri May 19, 2017 4:01 pm

Code Issues. Using GPIO's

Fri May 19, 2017 4:25 pm

Hello,

I am struggling creating the code to control some LED's.

What am I trying to do?

Well, I want to be able to control a single LED, Using three push buttons which controls the time period the LED remains illuminated for then switches off.

For example: one button does 10 seconds, the next does 20 second and the third does 30 seconds (For example)

I have managed to control the LED to flash at a certain time period and have managed to illuminate the LED using the push button, but I just cant piece these together. Or find the information I need.

After a few hours of trying I just don't quite know what I'm doing.

Would anyone be able to help me with this script or point me in the rough direction

Cheers

Josh

User avatar
joan
Posts: 12816
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Code Issues. Using GPIO's

Fri May 19, 2017 6:04 pm

Please post your code.

Murphyboy
Posts: 4
Joined: Fri May 19, 2017 4:01 pm

Re: Code Issues. Using GPIO's

Fri May 19, 2017 6:27 pm

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)

While True:
if GPIO.input (24) :
GPIO.output (23, True)
else:
GPIO.output (23, False)

time.sleep (0.1)


But I cant work out how to make it so the LED only stay on for a set time period rather than just being a light switch effect. If that makes sense?

B.Goode
Posts: 4689
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Code Issues. Using GPIO's

Fri May 19, 2017 6:45 pm

The syntax (meaning) of a Python script depends on indentation.

To maintain indentation and thus the meaning of your script it needs to be posted using the mark-up provided by the grey [ Code ] button at the top of the message edit screen.

It already looks strange that you define 24 as an output but immediately read its value as an input.

You don't seem to make any attempt to use Sleep to introduce a 10 sec delay?

Murphyboy
Posts: 4
Joined: Fri May 19, 2017 4:01 pm

Re: Code Issues. Using GPIO's

Fri May 19, 2017 7:39 pm

Hmmm, I think I have got myself in a bit of a pickle.

Thank you for attempting to help, I will revaluate what I have done. As I seem to of mixed up a few numbers here. And ill repost in reply the correct format.

Very new to the software/programming world. Usually more hardware based but trying to broaden my horizons.

Cheers

Josh

alphanumeric
Posts: 1273
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Code Issues. Using GPIO's

Fri May 19, 2017 8:23 pm

Maybe this will help, https://www.sunfounder.com/learn/Super_ ... rrypi.html
Yeah, don't use the same GPIO pin for an input and output.

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

Re: Code Issues. Using GPIO's

Fri May 19, 2017 8:37 pm

Hi.

so here an example of how you could do it. assuming switches on gpio 24 and 26 and LED on gpio 23

Code: Select all

while true:

	if GPIO.input(24) == True:
		GPIO.output(23, True)
		time.sleep(10)
		GPIO.output(23, False)
		
	if GPIO.input(26) == True:
		GPIO.output(23, True)
		time.sleep(30)
		GPIO.output(23, False)
		
	time.sleep(0.1)
so the if statements look to see if button is pressed, if it is then it turns the LED on then you create your desired on period by adding a time.sleep before turning the LED off.
so you see by changing the sleep time between turning the LED on and off you control how long the LED is on for.

the last time.sleep(0.1) is just to slow your program a fraction because if no button is pressed the loop will execute very fast and uses a lot of cpu time which warms up your Pi.

hope this helps
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

alphanumeric
Posts: 1273
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Code Issues. Using GPIO's

Fri May 19, 2017 8:49 pm

I use a GPIO pin to shut down my Pi when I press a button connected to it, this is my code.

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)  
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_OFF)  

def switchpress(channel):  
    os.system("sudo shutdown now -P")

GPIO.add_event_detect(16, GPIO.FALLING, callback = switchpress, bouncetime = 2000) 

I have my own pull up resister installed which is why I used GPIO.PUD_OFF. To use the internal pull up resister it is GPIO.PUD_UP

Murphyboy
Posts: 4
Joined: Fri May 19, 2017 4:01 pm

Re: Code Issues. Using GPIO's

Mon May 29, 2017 5:44 pm

Thanks for the replies, I appreciate the help!

After getting the LED to work, I have developed the project to wanting to have the LED light at a certain time of day and go out at another set point.

Below is as far as I got but cant seem to get it running right. Can anyone see my error as I cant seem to get it to run?

Hopefully it will post correctly as I used the code function.

Code: Select all

import datetime, time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

def main():

Pin = 18     #GPIO24
GPIO.setup(pin, GPIO.OUT)

while(True):
now = datetime.datetime.now()
today3pm = now.replace(hour=15, minute=0, second=0, microsecond=0)
today6pm = now.replace(hour=18, minute=0, second=0, microsecond=0)
turnOn = now>today3pm
turnOff = now>today6pm

if (turnOn==True):
GPIO.output (pin, True)
if (turnoff==True):
GPIO.output(pin, False)

time.sleep(15)

main()
Cheers

Josh

B.Goode
Posts: 4689
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Code Issues. Using GPIO's

Mon May 29, 2017 5:54 pm

A couple of observations:

Code: Select all

Pin = 18     #GPIO24
But you have specified BCM numbering in your setmode call. So won't Pin = 18 refer to gpio18? Maybe having the led wrongly wired is the only error?

There is no indentation in your script, so it is impossible to verify the logic.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 78 guests