//Turn on LED and Relay using Arduino controlled by a button //define the pin constants int ledPin = 22; int button = 23; int relay = 24; //define button state booleans boolean changed; boolean oldState; void setup(){ //set the led pin to output pinMode(ledPin, OUTPUT); //set the button to input pinMode(button, INPUT); //set the relay to output pinMode(relay, OUTPUT); //Initilize serial communication Serial.begin(57600); //assume in the beginning that the button has changed changed = true; //remember the old button state oldState = false; } void loop(){ //read and store the button state boolean State = digitalRead(button); //check if the button state has changed if(State != oldState){ changed = true; oldState = State; } //If there is a change, send the status over Serial if(changed){ Serial.print("The button status is: "); Serial.println(State); changed = false; } //set the led to the button state digitalWrite(ledPin,State); //set the relay to the button state digitalWrite(relay,State); }