/*
* This is the Arduino code for 2 push button to push ON and Push OFF 2 relays and 2 AC bulb
* the output pen 10 is connected to relay
* watch video instruction on video https://youtu.be/7CCSRs5bvH0
* Be carefull working with AC is dangrous. Disconnect from AC power when working and wear protective gloves when touch the AC components.
*
* Written by Ahmad Shamshiri for Robojax Video, www.Robojax.com
* Date: Dec 14, 2017, in Ajax, Ontario, Canada
* 2 button added Feb 09, 2018 for 1 button at 08:51
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purpose only.
* http://robojax.com/learn/arduino/
*
*/
int pbuttonPin = 1;// connect output to push button
int relayPin = 2;// Connected to relay (LED)
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
int pbuttonPin2 = 3;// connect output to push button 2
int relayPin2 = 4;// Connected to relay 2 (LED)
int val2 = 0; // push value from pin 3
int lightON2 = 0;//light 2 status
int pushed2 = 0;//push 2 status
int led = 0; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
// Robojax.com code and video tutorial for push button ON and OFF
//Serial.begin(9600);
pinMode(pbuttonPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(pbuttonPin2, INPUT);
pinMode(relayPin2, OUTPUT);
pinMode(led, OUTPUT);
//digitalWrite(relayPin, HIGH);
//digitalWrite(relayPin2, HIGH);
}
void loop() {
// Robojax.com code and video tutorial for push button ON and OFF (2 relay)
val = digitalRead(pbuttonPin);// read the push button value
val2 = digitalRead(pbuttonPin2);// read the push button 2 value
{
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// Start of Relay 1 ********** /
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
// Robojax.com code and video tutorial for push button ON and OFF (2 relay)
lightON = val;
if(pushed == HIGH){
//Serial.println("Light ON");
digitalWrite(relayPin, HIGH);
}else{
//Serial.println("Light OFF");
digitalWrite(relayPin, LOW);
}
// End of Relay 1 ********** /
// Start of Relay 2 ********** /
if(val2 == HIGH && lightON2 == LOW){
pushed2 = 1-pushed2;
delay(100);
}
lightON2 = val2;
if(pushed2 == HIGH){
//Serial.println("Light ON");
digitalWrite(relayPin2, HIGH);
}else{
//Serial.println("Light OFF");
digitalWrite(relayPin2, LOW);
}
// End of Relay 2 ********** /
delay(100);
}