vikash23
Posts: 11
Joined: Tue Oct 18, 2016 9:05 pm

python function code for blink LED issue

Sat Oct 22, 2016 9:59 pm

Hi,

I am trying to blink an LED using the function.

Below is my code

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
speed = raw_input ("Enter length of each blink (second):")
times = raw_input ("Enter total number of times to blink:")
print "Speed:" , speed
print "Times:" , times
def blink(speed,times):
while len(times)== 1:
GPIO.output(11,1)## Switch on pin 11
time.sleep(speed)## Wait
GPIO.output(11,0)## Switch off pin 11
time.sleep(speed)## Wait
print "Done"
return
GPIO.cleanup()

i get speed and time by user input and use blink function.

i get output for the speed and time but my LED is not blinking

I checked the connection using a simple LED blink program and it works.

I think i have made a mistake with while loop. can any one please help me to resolve.

zmd
Posts: 5
Joined: Thu Oct 20, 2016 9:01 am

Re: python function code for blink LED issue

Sun Oct 23, 2016 6:16 am

You are defining blink function but you are not calling it.

Additionally, time.sleep() function requires parameter as an int (not str). You need to convert str parameters that you get from the user to int in order to call it properly (you can use int() function for converting)

Next time please use code tag to format your code. It is not indented. It is hard to follow in this form.

User avatar
Bottersnike
Posts: 52
Joined: Wed Apr 06, 2016 7:17 am
Contact: Website

Re: python function code for blink LED issue

Sun Oct 23, 2016 8:50 am

I'm on my phone so I can't test this, but I think the following should work:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
speed = input("Enter length of each blink (second): ")
times = input("Enter total number of times to blink: ")
print "Speed:", speed
print "Times:", times
def blink(speed, times):
    for i in range(times):
        GPIO.output(11,1)## Switch on pin 11
        time.sleep(speed / 2)## Wait
        GPIO.output(11,0)## Switch off pin 11
        time.sleep(speed / 2)## Wait
    print "Done"
    return

blink(speed, times)
GPIO.cleanup()
What I changed:
  • I replaced the raw_inputs with inputs so that it became an integer.
  • I replaced the while loop with a for loop so that it only loops n times.
  • I made it sleep for speed/2 so that the length of the whole pulse lasted for speed.
  • Called blink
There are a few more edits that I need to make but my phone isn't working. I'll get them in in about four hours from now.
Last edited by Bottersnike on Sun Oct 23, 2016 2:37 pm, edited 1 time in total.
Micro:Pi is an (almost) fully fledged IDE for programming a BBC Micro:Bit using C++. Go check it out at https://bottersnike.github.io/Micro-Pi

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: python function code for blink LED issue

Sun Oct 23, 2016 9:02 am

Unless I'm missing something, you've both defined a "blink" function, but never called it.

Given that you seem to only want to run the function once after getting the inputs, I don't think it needs to be a separate function at all.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.


vikash23
Posts: 11
Joined: Tue Oct 18, 2016 9:05 pm

Re: python function code for blink LED issue

Sun Oct 23, 2016 7:17 pm

Thanks all for your reply.

I am sorry as I was new to the forum and I am not aware of how to use code tag.

I forgot to call the blink function

But my intention is to use the blink function using a while loop.

is for loop a must when using a function ? why can't i get the times as input and use while loop.

Example:

times = 5
while no of times is less than 10 do the below function.

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

Re: python function code for blink LED issue

Sun Oct 23, 2016 7:52 pm

vikash23 wrote:
is for loop a must when using a function ?
No.

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: python function code for blink LED issue

Sun Oct 23, 2016 9:20 pm

Bottersnike wrote:I'm on my phone so I can't test this, but I think the following should work:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
speed = input("Enter length of each blink (second): ")
times = input("Enter total number of times to blink: ")
print "Speed:", speed
print "Times:", times
def blink(speed, times):
    for i in range(times):
        GPIO.output(11,1)## Switch on pin 11
        time.sleep(speed / 2)## Wait
        GPIO.output(11,0)## Switch off pin 11
        time.sleep(speed / 2)## Wait
    print "Done"
    return

blink(speed, times)
GPIO.cleanup()
What I changed:
  • I replaced the raw_inputs with inputs so that it became an integer.
  • I replaced the while loop with a for loop so that it only loops n times.
  • I made it sleep for speed/2 so that the length of the whole pulse lasted for speed.
  • Called blink
There are a few more edits that I need to make but my phone isn't working. I'll get them in in about four hours from now.
It's not wise to use input with python 2. At the input prompt if you enter this your user folder gets deleted (so DON'T really try it).
eval("__import__('shutil').rmtree('/home/pi')")

instead use raw_input and then cast to the value you need

Code: Select all

value = raw_input("Enter a number:")
value = int(value)
print value, type(value)

value = raw_input("Enter a decimal number:")
value = float(value)
print value, type(value)
I know it will only be an issue if someone want to do something malicious, but why start a bad habit.

vikash23
Posts: 11
Joined: Tue Oct 18, 2016 9:05 pm

Re: python function code for blink LED issue

Sun Oct 30, 2016 9:59 pm

Hi,

I have tried the blink LED using while loop and for loop.

Now I am trying to interface HMC6352 to pi using I2C.

While using i2C detect on SMbus 1 i found address 21 which is 0010 0001 in binary.

But on the datasheet it says 0x42 for write (0100 0010)
0x43 for read (0100 0011)

basically the binary value of 21 is left shifted 1 and the last value 0 or 1 determines it is read or write to slave device..

datasheet says use command 0x41 to get data.

below is the code which i tried and modified from understanding Arduino programming

import smbus
import time
cmd = 0x41
DEVICE_ADDR = 0x21
bus = smbus.SMBus(1)
def data2():
data1 = bus.read_byte_data(DEVICE_ADDR, cmd)
data3 = bus.read_byte_data(DEVICE_ADDR, 1)
data4 = bus.read_byte_data(DEVICE_ADDR, 2)
d=(data3<<8)+data4
d=d/10.0
return d
while True:
db=data2()
print db
time.sleep(1)

please ignore improper variables as i want to do it quick. below is the output which i got. when i turn my accelerator the code breaks and shows some error.

7.1
77.1
77.1
77.1
77.1
77.1
77.1
77.1
51.4
51.4
77.1
77.1
77.1
77.1
77.1
77.1
77.1
77.1
77.1
77.1
77.1
0.0
77.1
77.1
77.1

Traceback (most recent call last):
File "/home/pi/i2c.py", line 14, in <module>
db=data2()
File "/home/pi/i2c.py", line 7, in data2
data1 = bus.read_byte_data(DEVICE_ADDR, cmd)
IOError: [Errno 5] Input/output error

can anyone help me how to solve it and what this error means.

Return to “Python”