-1

I have a loop that takes a three way switch input and selects an option on power up of a camera:

# Set GPIO input
switchColorOne = pyb.Pin("P9", pyb.Pin.IN, pyb.Pin.PULL_UP)
switchColorTwo = pyb.Pin("P7", pyb.Pin.IN, pyb.Pin.PULL_UP)

#Set color pallete by switch
if switchColorOne.value() == 0:
    sensor.set_pixformat(sensor.RGB565)
elif switchColorTwo.value() == 0:
    sensor.set_pixformat(sensor.GRAYSCALE)
else:
    sensor.set_color_palette(sensor.PALETTE_IRONBOW)
    sensor.set_pixformat(sensor.RGB565)

I would like to take the input from a single push button to cycle through the three options during the switch, preferably with a while loop so it can happen continually. I can't figure out how to make this happen, do I need a debouncer, can I use a for loop to iterate through different lines of code?

ceng
  • 128
  • 3
  • 17

2 Answers2

0

You said above was button is in off state, You can try like this.

switchColorOne = pyb.Pin("P9", pyb.Pin.IN, pyb.Pin.PULL_DOWN)
switchColorTwo = pyb.Pin("P7", pyb.Pin.IN, pyb.Pin.PULL_DOWN)

#Set color pallete by switch
if switchColorOne.value() == 1:
    sensor.set_pixformat(sensor.RGB565)
elif switchColorTwo.value() == 1:
    sensor.set_pixformat(sensor.GRAYSCALE)
else:
    sensor.set_color_palette(sensor.PALETTE_IRONBOW)
    sensor.set_pixformat(sensor.RGB565)````
Siddharth
  • 70
  • 11
  • That isn't going to work, at all. They'll just have the same problem they already have ... the conditions are just hanging around, they get immediately passed up right after making the pins and there is nothing to bring the code back to those conditions on button press. – OneMadGypsy May 26 '21 at 05:05
0

The problem is you are just creating arbitrary conditions and your program has no way to know to go back to them when you press a button. If you want to make this work in a non-blocking manner you should use interrupts

C1 = pyb.Pin("P9", pyb.Pin.IN, pyb.Pin.PULL_UP)
C2 = pyb.Pin("P7", pyb.Pin.IN, pyb.Pin.PULL_UP)

sensor.set_color_palette(sensor.PALETTE_IRONBOW)
sensor.set_pixformat(sensor.RGB565)

def update(pin):
    globals C1, C2, sensor #you might not even need this line

    if   pin is C1: sensor.set_pixformat(sensor.RGB565)
    elif pin is C2: sensor.set_pixformat(sensor.GRAYSCALE)


C1.irq(update, pyb.Pin.IRQ_FALLING)
C2.irq(update, pyb.Pin.IRQ_FALLING)

You may have to switch to IRQ_RISING or pull your pins down. This gets you in the ballpark, though.

OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26