Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By Jokkepappa
#39838 So i was messing with some code and wanted to try to make menu system and oled display. I got it working, but with so much stuff module is getting bit slow.

Image

There are 5 buttons that can be used. only programmed stuff on 3. Enter up and down. Buttons are connected electically like this:
Image
Circuit simulator: http://lushprojects.com/circuitjs/circuitjs.html

Here is video of the menu working.
https://www.dropbox.com/s/aq7gced5vn5dl ... 8.mov?dl=0
Using 2ms delays around the code, but it still is so slow.
code for refrence:
Code: Select allphyButton = "none"
menuLevel = 0
menuRow = 1
cooldown = 2
oledcls
goto [ajastin]
wait

[ajastin]

if cooldown > 0 then goto [away]
analogPin = ""
ai analogPin
analogPin = analogPin & "   "

if analogPin < 350 then gosub [leftdown]
if analogPin > 350 then gosub [rightup]

if analogPin < 20 then phyButton = "none"
if analogPin > 1000 then phyButton = "enter"

if phyButton == "enter" then goto [pressEnter]
if phyButton == "left" then goto [pressLeft]
if phyButton == "right" then goto [pressRight]
if phyButton == "up" then goto [pressUp]
if phyButton == "down" then goto [pressDown]
if menuLevel = 0 then goto [drawOledMain]
delay 2
goto [ajastin]

[pressEnter]
if menuRow = 7 then goto [back]
if menuLevel = 0 then goto [drawOledMenuOne]
delay 2
goto [ajastin]

[back]
menuLevel = 0
menuRow = 0
delay 2
oledcls
goto [ajastin]
[pressLeft]

delay 2
goto [ajastin]
[pressRight]

delay 2
goto [ajastin]
[pressUp]
oledprint "   " 0 menuRow
menuRow = menuRow - 1
oledprint "-> " 0 menuRow
delay 2
goto [ajastin]
[pressDown]

oledprint "   " 0 menuRow
menuRow = menuRow + 1
oledprint "-> " 0 menuRow
delay 2
goto [ajastin]
[drawOledMain]
oledprint cooldown 0 2
oledprint analogPin 0 0
oledprint phyButton 0 1
goto [ajastin]
[drawOledMenuOne]
oledprint "      Menu    " 0 0
oledprint "-> Item 1   " 0 1
delay 1
oledprint "   Item 2   " 0 2
delay 1
oledprint "   Item 3   " 0 3
delay 1
oledprint "   Item 4   " 0 4
delay 1
oledprint "   Item 5   " 0 5
delay 1
oledprint "   Item 6   " 0 6
delay 1
oledprint "   Exit   " 0 7
delay 1
menuLevel = 1
delay 2
goto [ajastin]
[away]
cooldown = cooldown - 1
goto [ajastin]
wait

[leftdown]
if analogPin < 20 then return
cooldown = 2
if analogPin > 1000 then return
if analogPin > 280 then phyButton = "down"
if analogPin < 280 then phyButton = "left"
return

[rightup]
if analogPin < 20 then return
cooldown = 2
if analogPin > 1000 then return
if analogPin > 480 then phyButton = "up"
if analogPin < 480 then phyButton = "right"
return
User avatar
By viscomjim
#39853 Hello Jokkepappa, this is very cool. First of all, thanks for posting code and examples. This is very beneficial to all and a great way to learn.

I am running your code in my head to get a grip, and I wonder what the purpose of "cooldown" is. Could you please explain it for me? Sorry for this type of question, but still learning here.

Thanks again!!!!
User avatar
By Mmiscool
#39865 It's really interesting what you are doing with the anolog input. I will hopefully take some time this week end to did in to this as that seems like a great technique to exploit give more inputs to the device.

Hope to see some more projects like this. :-)
User avatar
By Jokkepappa
#39874
viscomjim wrote:Hello Jokkepappa, this is very cool. First of all, thanks for posting code and examples. This is very beneficial to all and a great way to learn.

I am running your code in my head to get a grip, and I wonder what the purpose of "cooldown" is. Could you please explain it for me? Sorry for this type of question, but still learning here.

Thanks again!!!!


cooldown is basically for debouncing buttons. let's say the module would run code 1000 times a second. thats 1kHz clock. so when you press button down for more than 1ms it wuould detect it as more than one press. It also helps with the signal coming from button because it's not perfect but looks more like this:
Image

in a nutshell: IF button state changes then ignore any other presses for x amount of time.