Hi, could anybody please let me know how the following two lines work? I know != means not equal to but this is the first time I see |=
button_state = digitalRead(SW_1)<<0;
button_state |= digitalRead(SW_2)<<1;
Hi, could anybody please let me know how the following two lines work? I know != means not equal to but this is the first time I see |=
button_state = digitalRead(SW_1)<<0;
button_state |= digitalRead(SW_2)<<1;
| is bitwise OR.
The single upward bar ( | ) performs a bitwise OR between the two values, so
1 | 1 equals 1
0 | 1 equals 1
1 | 0 equals 1
0 | 0 equals 0
Adding the = to the front of it causes the result to be put in the variable on the left of the test
Thanks. So, here it means do the bitwise OR operation between
button_state and (digitalRead(SW_2) << 1) and store the result which is either 1 or 0 to button_state?
What these two lines actually mean? I cannot understand why <<0 and <<1 are being used.
button_state = digitalRead(SW_1)<<0;
button_state |= digitalRead(SW_2)<<1;
button_state = digitalRead(SW_1)<<0;
button_state |= digitalRead(SW_2)<<1;
The first line reads pin SW_1, returning a HIGH or LOW, which will be an integer value of 1 or 0, respectively.
The <<0 shifts the integer left 0 bits (basically does nothing, but makes the lines of code look consistent).
The result is then combined with the (presumably) integer value of button_state and stored in button_state.
The second line reads pin SW_2, shifts the value left 1 bit, and then does the bitwise OR with button_state, storing the result in button_state.
The result will be that the values read in from both digital inputs are now stored in button_state, in the lower two bits of button_state. If button_state originally contained zero, the result would be:
SW_1 == LOW SW_2 == LOW button_state == 0
SW_1 == HIGH SW_2 == LOW button_state == 1 (00000001 in binary)
SW_1 == LOW SW_2 == HIGH button_state == 2 (00000010 in binary)
SW_1 == HIGH SW_2 == HIGH button_state == 3 (00000011 in binary)
This is a common technique for storing multiple inputs into a single integer. Makes it very easy to test for a complex set of conditions on the inputs without having to do a bunch of individual tests, as an example the follow two if statements would be equivalent:
if ((digitalRead(SW_1) == LOW) && (digitalRead(SW_2) == HIGH))
if (button_state == 2)
It would also be very easy to have a switch:case statement that could perform a different action based on each possible combination:
switch (button_state) {
case 0:
//some code
break;
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
//some code
break;
default:
break;
}
bbqq:
What these two lines actually mean?
They combine the state of two inputs into one numeric value:
0 : Both = LOW
1 : SW_1 = HIGH
2 : SW_2 = HIGH
3 : Both = HIGH
david_2018:
It would also be very easy to have a switch:case statement that could perform a different action based on each possible combination:
Yes, that is a good way of doing it. However, I would have switch case default to zero:
switch (button_state) {
case 1:
//SW_1 HIGH
break;
case 2:
//SW_2 HIGH
break;
case 3:
//Both HIGH
break;
default:
//Both LOW
break;
}
Danois90:
Yes, that is a good way of doing it.
We haven't seen the whole program. I doubt that the "it" that it does is the best way of doing it. I am highly skeptical. Is it some kind of state change variable? Then it's likely that combining inputs into a single variable in this way is an obtuse way of detecting change. I suppose they are trying to "funnel" the change into a single test. I can't imaging that being very clean code. The end result - everyone has to peer closely to try and understand something that is basically much simpler than it has been made.
I think what has been overlooked is that the inputs to the button_state variable come directly from the digital inputs. That is pre-state change detection. This can only work where there really isn't any state change detection. With push buttons, the on-to-off transition is ignored, as far as any switch case goes. Here, it would pass into the switch case for handling. That doesn't seem right to me. I don't see how it really simplifies anything, unless the buttons are really toggle switches.
Also to be expressive, the variable should be pluralized, 'buttons_state".