Hey everybody! It's my 1st post, and I'm a newbie to Arduino and programming. I am a field service technician given the task of creating a "toggling" control for our labeling machines. I'm starting simple. I found some code that will let me toggle an 'on' state of each of 2 LED's in turn with a pushbutton. What I need is that, plus add a button for each LED so that, when pushed, it's LED won't get a turn and the opposite LED comes on with each push. Here is my code so far:
//Circuit: Switch to Pin2, 10k resistor to Pin2, switch to ground
//LED on pin12, LED on pin13, both with 1k resistor to groundint inPin0 = 2; // choose the input pin (for Main pushbutton)
int inPin1 = 6; // the number of the input pin
int inPin2 = 8; // the number of the input pin
int val = 0; // variable for reading the pin status
bool pushed = false;
bool greenState = true;
bool redState = false;void setup() {
pinMode(12, OUTPUT); // declare LED as output
pinMode(13, OUTPUT); // declare LED as output
pinMode(inPin0, INPUT); // declare pushbutton as input
pinMode(inPin1, INPUT); // declare pushbutton as input
pinMode(inPin2, INPUT); // declare pushbutton as input
digitalWrite(12, HIGH); // turn LED ON
digitalWrite(13, LOW); // turn LED ON
Serial.begin(9600);
}void green(bool state) {
digitalWrite(12, state ? HIGH : LOW);
greenState = state;
}void red(bool state) {
digitalWrite(13, state ? HIGH : LOW);
redState = state;
}void toggle() {
green(!greenState);
red(!redState);
printState();
}void printState() {
Serial.print("Red "); Serial.print(redState, DEC);
Serial.print(" Green "); Serial.println(greenState, DEC);
}void loop(){
// remote control
if (Serial.available() > 0) {
int b = Serial.read();
if (b == 'g') {
Serial.println("Turning on green");
red(false);
green(true);
}
if (b == 'r') {
Serial.println("Turning on red");
red(true);
green(false);
}
if (b == 's') {
printState();
}
Serial.flush();
}// button0 handler
val = digitalRead(inPin0); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
if(pushed) {
pushed = false;
Serial.println("Button0 Released");
toggle();
}
} else {
if(!pushed) {
pushed = true;
Serial.println("Button0 Pushed");
}
}}
I've added the int inPin1 and inPin2 for the 2 extra buttons, but I don't know where to go from there. I would love some help on this, as I am new and have little experience programming. Thanks!!