12/9/2023 0 Comments Arduino push button ledYou mentioned that your circuit will have multiple push-buttons. The implemented logic is flawed, I'm afraid, and due to the chattering, the value of selection rapidly increases, 'telling' the switch-case to light up the LEDs. The most obvious reason why you will see all LEDs lighting up is that you are facing contact chatter with no debouncing routine for your switch.You are declaring a pin connected to the ADC module as an input, which will lead to erratic behavior.Ĭonst int pinButton = 3 // used 3 as an example here, referring to digital pin 3, not A3. NEVER use an analog pin (A3) as a digital input.The correct way to do this is by checking the status of the digital pin via digitalRead(), as follows: Why are you not using digitalRead() to check the status of the pin? You should NOT be doing this:.Get the number of elements in the arrayĬonst int numOfLeds = sizeof(led) / sizeof(led) even if it's not in a sequence of numbers (and still use a FOR loop to handle it) Using an array will simplify the program and will allow you to select any pin I always PULLUP my inputs and check for LOW state to determine a pushed button (there are tons of discussions about that so I will not go into details).Īs shown below, using an array for the led pins will simplify your code. The debouncer blocks your code, check out millis() as a way to count time while doing other tasks. Looking at your edited code there are few issues I would like to point: Where you increase the selection you should add a test for maximum and decide there if to reverse the order (selection-) or to start from zero.Īlso, you need to turn off all non selected leds. Int selection = 0 // value used for selection #define buttonPin A3 // Input Push Button I'm not amazing at programming but it looks like it should work so am I missing something really simple? ` I can turn on individual LEDS by changing the selection value and I've run the same button set up on other code so its not a hardware issue. What ends up happening is all the LEDS turn on instead of just one at at a time and the button ends up doing nothing. Each case has a specific LED bound to it so the user knows what option they have selected, the code can be seen below. I'm doing a project for uni using an Arduino and part of it involves making a a sort of menu where a pushbutton (or pushbuttons) are used to switch between cases.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |