LED Finder

Place code snippets and demo code here
Post Reply
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

LED Finder

Post by Electroguard »

The gpio of onboard LEDs can vary from one device manufacturer to another - for instance one wemos footprint ESP32 manufacturer uses gpio2 whereas another wemos footprint ESP32 manufacturer uses gpio22... but neither of the device pinouts mentions the LED gpio pin.
The aim of this script is to give it a list of available pins in pins$ for it to sequence through in turn.
It offers a simple no-brainer means of finding an onboard LED if one is available.
The current pin number is shown in the wlog window.
Each pin is blinked 'blinks' number of times.
If an LED is seen blinking, pressing the gpio0 button will 'stop' the sequencing.
Subsequent button presses will toggle the LED.

Code: [Local Link Removed for Guests]

'LED finder - blinks all specified gpio pins in turn to find any connected LEDs
'Enter all potential LED gpio's into pins$
'Current pin number is shown in the wlog window
'Press gpio0 button quickly when the LED is blinking to stop sequencing  
'Further button presses will toggle the LED
pins$="1,2,3,4,5,10,12,13,14,15,16,17,18,19,21,22,23,25,26,27,33,34,35,36,39"
ledpin=val(word$(pins$,1,","))  
pin.mode ledpin, output
buttonpin=0
pin.mode buttonpin, input, pullup
interrupt buttonpin, pressed
debounce=100 '
blinkrate=150
blinks=5
pins=word.count(pins$,",")
c=0
stop=0
status$=""
do while (c<pins) and (stop=0)
 pin.mode ledpin, special
 c=c+1
 gosub changepin
 gosub blink
loop
wlog "LED search ended"
wait

changepin:
 ledpin=val(word$(pins$,c,","))
 pin.mode ledpin, output
 wlog word$(pins$,c,",")
return

blink:
for b=1 to (blinks*2)
if stop=1 then exit for
 if pin(ledpin)=1 then pin(ledpin)=0 else pin(ledpin)=1 
 pause blinkrate
next b
return

pressed:
stop=1
if pin(buttonpin)=0 then 
 if pin(ledpin)=1 then 
  pin(ledpin)=0
  status$="Off"
 else
  pin(ledpin)=1 
  status$="On"
 endif
 wlog "pin " + str$(ledpin) + " " + status$
endif   
pause debounce
return

end  '---------- End -------------

Post Reply