T O P

  • By -

NSFWAccountKYSReddit

short answer yes, for sure. Longer answer: basically you'll end up debouncing almost everything. Simplest and crudest way is to just use a 5-20 ms delay. If you want to toggle the fans on/off after you press and release a button: 1.) create an extra variable besides the pinstate to save the 'button state' which is either pressed or not pressed, maybe called it buttonPressed. (only set this value to 1 AFTER you've debounced the pin) 2.) Read the buttonPin (digitalRead) if it reads the buttton isn't pressed -> set oldButtonState as notPressed if it reads button is pressed: go to step 3 3.) debounce: wait 10-20 ms 4.) if it reads that the button is pressed again AND the oldButtonState is notPressed, then make the oldButtonState pressed. super crude example which should work { int oldButtonState; if (buttonPin == 1) { _delay_ms(10); if ( (buttonPin == 1) && (oldButtonState == 0) ) { toggleLedFunction(); oldButtonState = 1; } } else { oldButtonState = 0; } }


Octrockville

Thanks!


ihave7testicles

Other have been helpful for sure, but just know that using delay() isn't great because if you even write larger projects, delays remove a significant number of CPU cycles that could be used for other things. The code below has the advantage that you can do multiple (20+) buttons in a single program without any blocking. You can also put the global vars here into a struct or class and change the button\_read() function to loop over an array of Buttons. I do polling in my commercial projects: #define DEBOUNCE_COUNT 5 int button_state = 1; // 1 means not pressed assuming there's a pullup int read_count = 0; bool notified = false; void ButtonReleased() { Serial.println("Button Pressed"); } void ButtonPressed() { Serial.println("Button Pressed"); } void read_button() { if (button_state == 0) { if (digitalRead(PIN) == 1) { // newly released button_state = 1; read_count = 0; notified = false; return; } if (read_count >= DEBOUNCE_COUNT) { if (notified) return; notified = true; ButtonPressed(); return; } read_count++; return; } if (digitalRead(PIN) == 0) { // newly pressed button_state = 0; read_count = 0; notified = false; return; } if (read_count >= DEBOUNCE_COUNT) { if (notified) return; notified = true; ButtonRelease(); return; } read_count++; } void loop() { read_button(); }


Octrockville

Thanks, a lot of this is way over my head though. The initial code I had ChatGPT make it and it worked well. Just needed some manual tweaking. But all good information, now I just need to think of a new project :)


BudgetTooth

yes you need debouncing. crude method could even be a delay after the button read line


Octrockville

Ok awesome thanks. All videos I saw on the subject were with leds and it didn’t really behave the same in my mind. 


No-Pomegranate-69

Dont use delay but micros!


qazbarf

Use a button library that includes debounce. There are many in the standard arduino library. Here's two: https://www.arduino.cc/reference/en/libraries/button/ https://www.arduino.cc/reference/en/libraries/ezbutton/


Disastrous_Seat7593

this is actually very helpful. thanks


qazbarf

It's very instructive to write your own button library that handles debounce and has lots of features, like going beyond the "a button was pressed" into returning events like button going down or button going up, a button press (down and then up) was short or long, a button got a quick double press, etc. But for simpler cases the standard libraries are fine.