0

There are 5 push buttons each which correspond to a certain pin let us use 1 to 5 for an example. I want to set a variable, ActiveButton which will be set equal to the the only pin in the one state.

We make use of the concept that there can only be one active push button which means the pin of that would have a state of 1 and the rest would be 0. The long method for this would be to use if else and digitalRead(pin) == 1 for everything but that would be so long on code. I've considered using do while or while loops to stop until it reaches the push button with a state of 1, but I was wondering if there was a more efficient code. I was also thinking if there was something that made use of an array?

Though the idea of like having a simple array return the index which results to a corresponding value, like lets say Array[3] ==1 among all the indices from 0 to 4 feels easy to conceptualize, I believe this can be really taxing on the code and might not be able to be done in a short way.

I've also thought of possibly using switch case, where the variable would be the high state and the case values would be the digitalRead for each pin? But that would be wrong probably because I'm not sure but you can't set the switch variable to be a constant. It would be something like

switch (1)

case digitalRead(1):

case digitalRead(2):

7
  • what does this mean? but that would be so long on code ... you can use an array ... how do you expect to refer to the array elements without using a loop or multiple if- else if statements? – jsotola Mar 1 at 23:32
  • Array would definitely be used but I was thinking that if else is too long IF other codes like loops like do while, while, for can do the job with less code. – AndroidV11 Mar 1 at 23:34
  • you could use hardware ... search priority encoder – jsotola Mar 1 at 23:36
  • don't be thinking of being fancy with the code by using switch-case form instead of multiple if statements ... the compiler may actually produce the same code – jsotola Mar 1 at 23:39
  • put the button pin numbers in an array ... then you can use a for loop to read each pin and return either the lowest, or the highest pin number (or both) – jsotola Mar 1 at 23:52
2

There are 5 push buttons each which correspond to a certain pin let us use 1 to 5 for an example. I want to set a variable, ActiveButton which will be set equal to the the only pin in the one state.

Here is a short snippet of what I think, that you want. This is quite a long shot, since your question is not clear enough.

int ActiveButton = -1; // Initialize ActiveButton with -1 (no button is active)
int button_pins[] = {1, 2, 3, 4, 5}; //Array with pin numbers of the buttons

void loop(){
    for(int i=0;i < sizeof(button_pins)/sizeof(button_pins[0]); i++){
        if(digitalRead(button_pins[i])){ //if the i-th button was pressed
            ActiveButton = button_pins[i]; // assign i-th button pin number to variable
            break; // Break, since we have found our pressed button, ignore all following pins
        }
    }
}

This will assign the pin number of the pressed button to the variable. The condition in the for loop

sizeof(button_pins)/sizeof(button_pins[0])

will make the code loop over all elements of the button_pins array. sizeof(button_pins) returns the number of bytes in the button_pins array. Then we divide through the number of bytes in the first element to get the number of elements in the array.

4
  • Yes, this kind of code is exactly what I wanted. I needed to use it for lighting up LEDs differently. The problem when I tried this is my lights did not turn off when the buttons weren't pressed and only changed when another button was pressed. I did not indicate this in the original post because I already knew how to do the LED part and wanted to focus on that, but yeah I had to bring it up now. How do you make it so that the loop turns off when the pressed button is unpressed? – AndroidV11 Mar 2 at 21:23
  • That is the only problem. Everything else works. I appreciate your effort. I've tried putting an else condition to set the ActivePushButton to 0 but that didn't seem to do as I intended. For more context, after that code of yours I put a switch function for cases 1 to 5 to execute the custom five functions then the code for the custom five functions after. – AndroidV11 Mar 2 at 21:31
  • Update. I actually did this already by setting the ActiveButton equal to 0 after the switch cases. I just left this question in case you were gonna offer a different suggestion. – AndroidV11 Mar 2 at 21:44
  • Yes, I would have suggested something like that. If it now works, I don't have anything to add. – chrisl Mar 2 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.