Ok I have spent hours trying to Get this to work. All I wanna do is use # keys 0-9 as toggle switches. Should work like this... EX: If i press # "1" key LED turns on and stays on. Press # "1" key again LED turns off and stays off. Then rinse and repeat.
I'm a newbie so go easy on me, I'm starting with getting 1 to work first. Whats wrong?
/*
serialControl Toggle Switch ver. 1.0
*/
// Use pins 5 through 13 as the digital outputs
int output1 = 13;
int output2 = 12;
int output3 = 11;
int output4 = 10;
int output5 = 9;
int output6 = 8;
int output7 = 7;
int output8 = 6;
//int buttonPressTime = 250; // Number of milliseconds to hold outputs on
//____________________________________________________________________________________
int button1PushCounter = 0; // counter for the number of button presses
int button1State = 0; // current state of the button
int lastButton1State = 0; // previous state of the button
int button2PushCounter = 0; // counter for the number of button presses
int button2State = 0; // current state of the button
int lastButton2State = 0; // previous state of the button
int button3PushCounter = 0; // counter for the number of button presses
int button3State = 0; // current state of the button
int lastButton3State = 0; // previous state of the button
int button4PushCounter = 0; // counter for the number of button presses
int button4State = 0; // current state of the button
int lastButton4State = 0; // previous state of the button
int button5PushCounter = 0; // counter for the number of button presses
int button5State = 0; // current state of the button
int lastButton5State = 0; // previous state of the button
int button6PushCounter = 0; // counter for the number of button presses
int button6State = 0; // current state of the button
int lastButton6State = 0; // previous state of the button
int button7PushCounter = 0; // counter for the number of button presses
int button7State = 0; // current state of the button
int lastButton7State = 0; // previous state of the button
int button8PushCounter = 0; // counter for the number of button presses
int button8State = 0; // current state of the button
int lastButton8State = 0; // previous state of the button
/**
* Initial configuration
*/
void setup()
{
// Open the serial connection to listen for commands from the host
Serial.begin(115200);
// Set up the pins as outputs
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
pinMode(output5, OUTPUT);
pinMode(output6, OUTPUT);
pinMode(output7, OUTPUT);
pinMode(output8, OUTPUT);
// Make sure the outputs are all set LOW initially
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
digitalWrite(output4, LOW);
digitalWrite(output5, LOW);
digitalWrite(output6, LOW);
digitalWrite(output7, LOW);
digitalWrite(output8, LOW);
}
/**
* Main program loop
*/
void loop()
{
byte val;
val = 0;
// Check if a value has been sent by the host
if(Serial.available()) {
val = Serial.read();
//button1State = (val);
if (button1State != lastButton1State) {
if (button1State == '1') {
button1PushCounter++;
Serial.print("number of button pushes: ");
Serial.println(button1PushCounter, DEC);
}
else {
Serial.println("1 off");
digitalWrite(output1, LOW);
lastButton1State = button1State;
if (button1PushCounter == 0) {
digitalWrite(output1, LOW);
}
else {
digitalWrite(output1, HIGH);
}
}
}
}
}