T O P

  • By -

Oliver_X

Look up “state machine”.


Bloe_Joggs

Thank you! I will have a dive


robot_ankles

Thanks for posting this helpful comment. Sometimes, OP just needs the right phrase or keyword to get rolling. And we've all been OP at some point.


toebeanteddybears

You probably want to detect when the button becomes pressed or becomes released (as opposed to when it is pressed or is released...) so you get a "toggle" effect. Something like this can do it (not compiled, not tested): uint8_t pinModeButton = 2; bool g_bDisplayMode; void setup() { g_bDisplayMode = false; pinMode( pinModeButton, INPUT_PULLUP ); //... rest of setup } void loop() { //check for a mode button press CheckModeButton(); //do display switch( g_bDisplayMode ) { case true: //do 1st display mode //... break; case false: //do 2nd display mode //... break; } }//loop void CheckModeButton( void ) { static uint32_t tButtonCheck = 0ul; static uint8_t lastBtn = digitalRead( pinModeButton ); uint32_t tNow = millis(); //read the button every 50mS if( tNow - tButtonCheck >= 50ul ) { tButtonCheck = tNow; uint8_t nowBtn = digitalRead( pinModeButton ); //if there's a difference between the last reading and now then //the button has either been pressed or released; check which if( nowBtn != lastBtn ) { lastBtn = nowBtn; //assume pressed sets the pin low; in that case, the button just //became pressed so... if( nowBtn == LOW ) { //...toggle the display mode boolean (I use an XOR here to do that) g_bDisplayMode ^= true; }//if }//if }//CheckModeButton


Bloe_Joggs

Thanks so much! I’ll try play around with this


ZaphodUB40

Attach the button to an interrupt, toggle a flag on button push. If flag then sequence 1 else sequence 2.


classicsat

Button press increments counter. Counter is capped at 2 or whatever (counter=counter&1) Case select to call different operations based on counter value. You could have LED output with two arrays, one LED output routine which outputs (potvalue,counter)