assuming that was a correct way to write this and use switch/case (it's not) - what would you expect this to do if you have the following order T1 < T2 < T3 < T4 ➜ which 'case' would be executed in your mind? just one (which one, all the conditions are true) or all of them? also you have twice the case (T3 <T4):... so which one should the compiler pick ?
the "right" way would be more something like this with an else only if you want the conditions to be exclusive from one another and in priority order
if (T1 < T2) {
...
}
else if (T2 < T3) {
...
}
else if (T3 < T4) {
...
}
The goal is to use an alternate way to decode an analog switch button.. 4 buttons and resistors create a voltage divider attached to the analog port. I read the value and compare the absolute value of the reading - the expected one foe each button. The
Only one case should return a true value.
I know that the if / else if do return a proper value, I had done it.
At the end, this small code is a test to lean if I could use the switch case with different boolean variable for another project.
so there are 5 possible voltages (including no button pressed).
there are 5 thresholds that the measured analog voltages is compared to in priority order to determine which switch is pressed.
comparing the input, val, to various thresholds, checking from one end (if val < T4, it's also < T3)
int val = analogRead(ANALOG_PIN);
if (T4 > val)
SW = 1;
else if (T3 > val)
SW = 2;
else if (T2 > val)
SW =3;
else if (T1 > val)
SW = 4;
else
SW = 0;
there are various possible resistor/button configurations
What is the analogRead value if NO buttons are pressed?
If button 1 is pressed?
If button 2 is pressed?
If button 3 is pressed?
If button 4 is pressed?