Code: Select all
// This program reads the analogue value of the resistor paths when the buttons are // pressed. Each button and combination of buttons generates a unique analogue
// voltage reading on pin A0. This analogue voltage is then used to identify which // button or combination of buttons are pressed at any given moment. The state of // the buttons is printed into the serial window.
int analogButtonPIN = 0; //pin the buttons and resistors are connected to
int ButtonValue = 0; //value for reading analogue value on pin A0
void setup()
{
Serial.begin(9600); //start serial and set baud rate
}
void loop()
{
ButtonValue = analogRead (analogButtonPIN); //read value on poteniometer
if (ButtonValue >700 && ButtonValue <706){
Serial.println ("switch one is pressed");
}
else if (ButtonValue >511 && ButtonValue <517){
Serial.println ("switch two is pressed");
}
else if (ButtonValue >428 && ButtonValue <434){
Serial.println ("switch three is pressed");
}
else if (ButtonValue >367 && ButtonValue <373){
Serial.println ("switch four is pressed");
}
else if (ButtonValue >778 && ButtonValue <784){
Serial.println ("switches one and two are pressed");
}
else if (ButtonValue >760 && ButtonValue <766){
Serial.println ("switches one and three are pressed");
}
else if (ButtonValue >749 && ButtonValue <755){
Serial.println ("switches one and four are pressed");
}
else if (ButtonValue >647 && ButtonValue <653){
Serial.println ("switches two and three are pressed");
}
else if (ButtonValue >623 && ButtonValue <629){
Serial.println ("switches two and four are pressed");
}
else if (ButtonValue >575 && ButtonValue <581){
Serial.println ("switches three and four are pressed");
}
else if (ButtonValue >814 && ButtonValue <820){
Serial.println ("switches one, two and three are pressed");
}
else if (ButtonValue >807 && ButtonValue <813){
Serial.println ("switches one, two and four are pressed");
}
else if (ButtonValue >793 && ButtonValue <799){
Serial.println ("switches one, three and four are pressed");
}
else if (ButtonValue >712 && ButtonValue <718){
Serial.println ("switches two, three and four are pressed");
}
else if (ButtonValue >835 && ButtonValue <841){
Serial.println ("all switches are pressed");
}
delay (200);
//slow down the refresh of analog input reading and print of switch state
}