Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5032

General • Re: control a set of 15 inputs that output 15 outputs on Pico

$
0
0
Didn't you want the 15 inputs, 4 bit binary output version ?

Code:

from machine import Pin,Timerimport time# 15 direct buttons, 4 outputs for binary# setup gpios for buttonskeyPins = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]buts = []for i in keyPins:    buts.append(Pin(i,Pin.IN,Pin.PULL_DOWN))# setup outputsledPins = [2,3,4,5,6,7]leds = []for i in ledPins:    leds.append(Pin(i,Pin.OUT))# clear ALL outputsfor i in range(0,len(leds)):    leds[i].low()# read buttonsdef keypadRead():    global buts    kbut = -1    # scan for pressed button     for i in range(0,len(buts)):        time.sleep(0.005) #settling time        if buts[i].value() == 1:            # store button pressed            kbut = i    if kbut != -1: # button pressed return values        return kbut    else: # if no button pressed return with -1        return -1#main loopif __name__ == '__main__':    while True:        # read buttons        kbut = keypadRead()        if kbut != -1 : # if a button has been pressed            # clear ALL outputs            for i in range(0,len(leds)):                leds[i].off()            #print(kbut+1)            # set the required output            leds[4].off()            out = "0000" + str(int(bin(kbut+1)[2:]))            out = out[-4:]            for x in range(0,4):                if out[x] == '1':                   leds[x].on()            #time.sleep(0.25)            leds[4].on()
or the 15 input, matrixed leds ?

Code:

from machine import Pin,Timerimport time# 15 direct buttons, 15 matrixed leds# setup gpios for buttonskeyPins = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]buts = []for i in keyPins:    buts.append(Pin(i,Pin.IN,Pin.PULL_DOWN))# setup outputsledRowPins = [3,4,5,6,7]ledColPins = [0,1,2]ledrow = []ledcol = []for i in ledRowPins:    ledrow.append(Pin(i,Pin.OUT))for i in ledColPins:    ledcol.append(Pin(i,Pin.OUT))# clear ALL outputsfor i in range(0,len(ledcol)):    ledcol[i].low()    for j in range(0,len(ledrow)):        ledrow[j].high()# read buttonsdef keypadRead():    global buts    kbut = -1    # scan for pressed button     for i in range(0,len(buts)):        time.sleep(0.005) #settling time        if buts[i].value() == 1:            # store button pressed            kbut = i    if kbut != -1: # button pressed return values        return kbut    else: # if no button pressed return with -1        return -1#main loopif __name__ == '__main__':    while True:        # read buttons        kbut = keypadRead()        if kbut != -1 : # if a button has been pressed            # clear ALL outputs            for i in range(0,len(ledcol)):                ledcol[i].off()                for j in range(0,len(ledrow)):                    ledrow[j].on()            # set the required ouput            krow = int((kbut)/5)            kcol = kbut - (krow*5)            print(kbut,krow,kcol)            ledcol[krow].on()            ledrow[kcol].off()

Statistics: Posted by gordon77 — Tue Sep 17, 2024 7:27 am



Viewing all articles
Browse latest Browse all 5032

Trending Articles