help required for a newbie

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
GEOFOR
Posts: 2
Joined: Sat May 22, 2021 3:34 pm

help required for a newbie

Post by GEOFOR » Sat May 22, 2021 3:45 pm

Can anyone see why the led "led_coming" illuminates before the timer reaches 450? I am new to this and I cant see why it switches on when there appears to be no instruction to do it.

Once adjbell and commutator go high, the normal led should stay on until the timer reaches 450 then it is supposed to go out and the led coming led goes on however, the coming led switches in while the count is still very low.

I would be grateful for any help or suggestions

Code: Select all

import time
import machine
import utime
# set up LEDs, buttons and switches
bell = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
adjbell = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN)
commutator = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
cancel = machine.Pin(9, machine.Pin.IN, machine.Pin.PULL_DOWN)
sealcancel = machine.Pin(8, machine.Pin.IN, machine.Pin.PULL_DOWN)
led_normal = machine.Pin(12, machine.Pin.OUT)
led_normal.high()
led_coming = machine.Pin(13, machine.Pin.OUT)
led_coming.low()
led_going = machine.Pin(11, machine.Pin.OUT)
led_going.low()
elapsed = 0
# end of setup section

# Normal state

while adjbell.value() == 1:
     
     if elapsed <= 450 and adjbell.value() == 1 and commutator.value() == 1:
     
        elapsed = (elapsed + 1)
        print(elapsed)
        print("ordinary bell")
        print (adjbell.value(),commutator.value())
        led_normal.high()
        led_coming.low()
     else:       
        if elapsed > 450 and adjbell.value() == 1 and commutator.value() == 1:
            print("long bell")
            led_coming.high()
 

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: help required for a newbie

Post by SpotlightKid » Sat May 22, 2021 5:33 pm

Are you sure the "coming" LED is initially off? It could be that is is wired to "active low", i.e. it lights up when the pin value is 0. To test this, just insert a "while True: pass" loop after your setup statements and see whether the LED is already lit up.

Another possible reason is that 450 loop iterations without any delay within the loop is over incredibly fast (probably nanoseconds). Too fast for the human eye to discern.

BTW, your code could be written much more concise (and imho clearer) by taking into consideration:

* You can set an initial value when setting up an output pin.
* Pin class instance can be called directly, e.g. pin(1) or val = pin().
* Your if conditions are redundant.
* You don't need to set the "normal" and "coming" led pins in every loop iteration if elapsed is <= 450 You already set them before the loop and elapsed is never reset, so they should stay at that value.
* You can use the += operator.
* utime and time are the same modules and are unused here.

Also take not that the while loop and thus your program exits should the adjbell ever be low (which could be immediately). Are you sure that's intended?

Code: Select all

import machine

# set up LEDs, buttons and switches
bell = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
adjbell = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN)
commutator = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
cancel = machine.Pin(9, machine.Pin.IN, machine.Pin.PULL_DOWN)
sealcancel = machine.Pin(8, machine.Pin.IN, machine.Pin.PULL_DOWN)

led_normal = machine.Pin(12, machine.Pin.OUT, value=1)
led_coming = machine.Pin(13, machine.Pin.OUT, value=0)
led_going = machine.Pin(11, machine.Pin.OUT, value=0)

elapsed = 0
# end of setup section

# Normal state

while adjbell():
    if commutator():
        if elapsed <= 450:
            elapsed += elapsed + 1
            print(elapsed)
            print("ordinary bell:", adjbell(), commutator())
        else:
            print("long bell")
            led_coming(1)
The code still doesn't make much sense except for testing but I wanted to keep it equivalent to what you wrote.

Post Reply