0

I'm having an issue with my project. My project is 8 leds and a potentiometer on an arduino being read and controlled by a raspberry pi running python.

when I'm using a potentiometer the signal is received by my raspberry pi with python which then tells a set of leds to either turn on or off. However my leds flash on my low threshold when they shouldn't, the leds on my higher threshold work as expected and stay on till my pot is out of that range. How can I fix this?

Here is my script!

from nanpy import Arduino
from nanpy import serial_manager
from time import sleep

serial_manager.connect('/dev/ttyUSB0')

POT = 0
LED1 = 13
LED2 = 12
LED3 = 11
LED4 = 10
LED5 = 9
LED6 = 8
LED7 = 7
LED8 = 6

Arduino.pinMode(POT, Arduino.INPUT)
Arduino.pinMode(13, Arduino.OUTPUT)
Arduino.pinMode(12, Arduino.OUTPUT)
Arduino.pinMode(11, Arduino.OUTPUT)
Arduino.pinMode(10, Arduino.OUTPUT)
Arduino.pinMode(9, Arduino.OUTPUT)
Arduino.pinMode(8, Arduino.OUTPUT)
Arduino.pinMode(7, Arduino.OUTPUT)
Arduino.pinMode(6, Arduino.OUTPUT)

print "starting"
while True:
    pot = Arduino.analogRead(POT)
#this is the section that is blinking when it should stay solid
   if pot < 450:
        Arduino.digitalWrite(LED1, Arduino.HIGH)
        Arduino.digitalWrite(LED2, Arduino.HIGH)
        Arduino.digitalWrite(LED3, Arduino.HIGH)
        Arduino.digitalWrite(LED4, Arduino.HIGH)
        Arduino.digitalWrite(LED5, Arduino.LOW)
        Arduino.digitalWrite(LED6, Arduino.LOW)
        Arduino.digitalWrite(LED7, Arduino.LOW)
        Arduino.digitalWrite(LED8, Arduino.LOW)
        sleep(.5)
# end of section
   if pot < 650 > 450:
        Arduino.digitalWrite(LED1, Arduino.LOW)
        Arduino.digitalWrite(LED2, Arduino.LOW)
        Arduino.digitalWrite(LED3, Arduino.LOW)
        Arduino.digitalWrite(LED4, Arduino.LOW)
        Arduino.digitalWrite(LED5, Arduino.LOW)
        Arduino.digitalWrite(LED6, Arduino.LOW)
        Arduino.digitalWrite(LED7, Arduino.LOW)
        Arduino.digitalWrite(LED8, Arduino.LOW)
        sleep(.1)
    if pot > 650:
        Arduino.digitalWrite(LED1, Arduino.LOW)
        Arduino.digitalWrite(LED2, Arduino.LOW)
        Arduino.digitalWrite(LED3, Arduino.LOW)
        Arduino.digitalWrite(LED4, Arduino.LOW)
        Arduino.digitalWrite(LED5, Arduino.HIGH)
        Arduino.digitalWrite(LED6, Arduino.HIGH)
        Arduino.digitalWrite(LED7, Arduino.HIGH)
        Arduino.digitalWrite(LED8, Arduino.HIGH)
        sleep(.5)
    print pot
1
  • Implement hysteresis. Commented Feb 2, 2016 at 12:25

3 Answers 3

1

Your second condition is written wrongly. It should be this way:

elif pot >= 450 and pot <= 650:
    # do stuff

Your code should be such that only an option is chosen at any time. There should be only one 'if' condition at the top and the rest should be 'elif'.

2
  • Yes, except it's python, so - if (pot < 650 and port > 450): Commented Feb 2, 2016 at 21:55
  • @Kingsley Python! Can't believe I totally missed that Commented Feb 2, 2016 at 22:20
0

The problem is you're using multiple if statements, whereas the if-blocks should be mutually exclusive, so elif would be better. It's not really clear what you intend in the 2nd clause either, since it's a typo(?) - not valid python syntax anyway.

if (pot <= 450):
    Arduino.digitalWrite(LED1, Arduino.HIGH)
    ...
    time.sleep(0.5)
elif (pot > 450 and pot <= 650):
    Arduino.digitalWrite(LED1, Arduino.LOW)
    ...
    time.sleep(0.1)
else # pot > 650
    Arduino.digitalWrite(LED1, Arduino.LOW)
    ...
    time.sleep(0.5)

I think your LEDs were flashing because both the first and second if clauses were evaluating to True, thus both code segments were being executed (were only the first four LEDs flashing?) Using if/elif/else instead, ensures only ever one clause is run.

If I might make a suggestion, assuming you want an LED bar graph based on the POT reading...

The analogRead() will give you a result 0-1023. Graphed over 8 LEDs, this means one more LED should be illuminated roughly per 128 units. Since you want a full bar when rounding makes the value near 1023, maybe something like 120 is a better divisor (I just guessed this, it's probably calculable).

A simple loop over your LEDs, summing units over each iteration gives an easy way of turning the LEDs on or off.

Arduino.analogRead(POT)
led_bar_per_units = 120   # needs a better name

### illuminate the LED-bar
bar_length = 0
for led in (LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8):
    bar_length += led_per_bar_units
    if (bar_length <= pot):
        Arduino.digitalWrite(led, Arduino.HIGH)
    else:
        Arduino.digitalWrite(led, Arduino.LOW)
time.sleep(0.5)

EDIT: I know you don't need brackets around python if statements. I've been doing C/C++ code for so long I find bracketed-clauses easier to read, and thus code my python this way too.

0

I figured it out. Here is my new script.

from nanpy import Arduino
from nanpy import serial_manager
from time import sleep

serial_manager.connect('/dev/ttyUSB0')

POT = 0
LED1 = 13
LED2 = 12
LED3 = 11
LED4 = 10
LED5 = 9
LED6 = 8
LED7 = 7
LED8 = 6

Arduino.pinMode(POT, Arduino.INPUT)
Arduino.pinMode(13, Arduino.OUTPUT)
Arduino.pinMode(12, Arduino.OUTPUT)
Arduino.pinMode(11, Arduino.OUTPUT)
Arduino.pinMode(10, Arduino.OUTPUT)
Arduino.pinMode(9, Arduino.OUTPUT)
Arduino.pinMode(8, Arduino.OUTPUT)
Arduino.pinMode(7, Arduino.OUTPUT)
Arduino.pinMode(6, Arduino.OUTPUT)

print "starting"
while True:
    pot = Arduino.analogRead(POT)
    if pot < 450:
        Arduino.digitalWrite(LED1, Arduino.HIGH)
        Arduino.digitalWrite(LED2, Arduino.HIGH)
        Arduino.digitalWrite(LED3, Arduino.HIGH)
        Arduino.digitalWrite(LED4, Arduino.HIGH)
        Arduino.digitalWrite(LED5, Arduino.LOW)
        Arduino.digitalWrite(LED6, Arduino.LOW)
        Arduino.digitalWrite(LED7, Arduino.LOW)
        Arduino.digitalWrite(LED8, Arduino.LOW)
        sleep(.01)
    elif pot > 650:
        Arduino.digitalWrite(LED1, Arduino.LOW)
        Arduino.digitalWrite(LED2, Arduino.LOW)
        Arduino.digitalWrite(LED3, Arduino.LOW)
        Arduino.digitalWrite(LED4, Arduino.LOW)
        Arduino.digitalWrite(LED5, Arduino.HIGH)
        Arduino.digitalWrite(LED6, Arduino.HIGH)
        Arduino.digitalWrite(LED7, Arduino.HIGH)
        Arduino.digitalWrite(LED8, Arduino.HIGH)
        sleep(.01)
    else:
        Arduino.digitalWrite(LED1, Arduino.LOW)
        Arduino.digitalWrite(LED2, Arduino.LOW)
        Arduino.digitalWrite(LED3, Arduino.LOW)
        Arduino.digitalWrite(LED4, Arduino.LOW)
        Arduino.digitalWrite(LED5, Arduino.LOW)
        Arduino.digitalWrite(LED6, Arduino.LOW)
        Arduino.digitalWrite(LED7, Arduino.LOW)
        Arduino.digitalWrite(LED8, Arduino.LOW)
        sleep(.01)
    print pot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.