Basically I have three push buttons that correspond to pins 1 to 3 and want to set an efficient code that sets a certain variable into the index whose digitalRead is equal to one. Let us assume that only one button can be pressed at a time.
void loop()
{
if(digitalRead(1) == 1)
{
Variable = 1;
}
if(digitalRead(2) == 1)
{
Variable = 2;
}
if(digitalRead(3) == 1)
{
Variable = 3;
}
}
Is there are more efficient way to do this, like for example using other loops?
I can't wrap around my head with doing this concept using the other loops such as for, do while, while and switch because the nature of the idea feels so unconventional for their uses.
Variablein your code? Can the values be arbitrary, or are they pin numbers, or are they indices of the pressed buttons? You could use a for loop to go other an array of pins and assign the variable with the element of a second array. I'm not sure, what the actual goal is here, so it is difficult to suggest a good way. – chrisl Mar 02 '21 at 14:23