I need 2 pin inputs to determine 4 possible states. I came up with what I think straight forward test to determine the state.
switchstate = 00;
if (digitalRead(11))
{
switchstate = switchstate | 00000001;
}
if (digitalRead(12))
{
switchstate = switchstate | 00000010;
}
// swichstate should have a value between 0 and 3. It does not! Value is 0, 1, 8, and 9. Here is code for Arduino Uno. You can run it. Can anyone explain the 8 and 9? Following this code is a much lengthier solution which provides the correct switchstate.
I would prefer to use the above code. I can by changing 8 and 9 with if to 2 and 3.
// INPUT SWITCHES FOR MODE!!!!
byte switchstate = 0;
void setup()
{
// initialize digital pin 13 as an output.
pinMode(11, INPUT);
pinMode(12, INPUT);
Serial.begin(9600);
}
void loop()
{
// Want to determine 1 of 4 states based on input pins 11 and 12
switchstate = 00;
Serial.println("Initial State of Switchset");
Serial.println(switchstate);
if (digitalRead(11))
{
switchstate = switchstate | 00000001;
Serial.println("PIN 11 is HIGH");
Serial.println(switchstate);
delay(1000);
}
if (digitalRead(12))
{
switchstate = switchstate | 00000010;
Serial.println("PIN 12 is HIGH");
Serial.println(switchstate);
delay(1000);
}
Serial.println("Final State of Switchset");
Serial.println(switchstate);
delay(1000);
}
Code that works.
// INPUT SWITCHES FOR MODE!!!!
byte switchstate = 0;
void setup()
{
// initialize digital pin 13 as an output.
pinMode(11, INPUT);
pinMode(12, INPUT);
Serial.begin(9600);
}
void loop()
{
if (!digitalRead(11) & !digitalRead(12))
{
switchstate = 0;
}
if (digitalRead(11) & !digitalRead(12))
{
switchstate = 1;
}
if (!digitalRead(11) & digitalRead(12))
{
switchstate = 2;
}
if (digitalRead(11) & digitalRead(12))
{
switchstate = 3;
}
Serial.print("Switchstate ");
Serial.println(switchstate);
delay(1000);
}