OK, I'm working on a project where I want to have multiple toggle switches create a "code" to determine further actions. This is essentially 5-bit communication process that will create output with a 32-bit range of outcomes from 00000 to 11111.
I'm testing the method with three switches, so if the first and third switch are HIGH/on it will read 101. Second and third, 011 etc. I'm not versed enough to know if there is a better method here, but is there a better way to do this?
// Define the pins being used
int pin_switch = 10;
int pin_switch2 = 11;
int pin_switch3 = 12;
// create int for 3-digit code
int code = 000;
void setup()
{
Serial.begin(9600);
pinMode(pin_switch, INPUT);
pinMode(pin_switch2, INPUT);
pinMode(pin_switch3, INPUT);
}
void loop()
{
if pin_switch == HIGH;
{code += 100;}
if pin_switch2 == HIGH;
{code += 10;}
if pin_switch3 == HIGH;
{code += 1;}
Serial.print(code);
// operate based on code output
if code == 101;
{do 101 stuff}
if code == 001
{do 001 stuff}
// reset code for next loop so numbers don't keep rising
code = 000;
}