Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* sketch
- turn on a fan using a relay and a button
- */
- int pinButton1 = 2;
- int pinButton2 = 3;
- int Relay1 = 6;
- int Relay2 = 7;
- int Relay3 = 8;
- int Relay4 = 9;
- long debouncing_time = 500; //Debouncing Time in Milliseconds
- volatile unsigned long last_micros;
- boolean button1 = false;
- boolean button2 = false;
- void setup() {
- pinMode(pinButton1, INPUT);
- pinMode(pinButton2, INPUT);
- pinMode(Relay1, OUTPUT);
- pinMode(Relay2, OUTPUT);
- pinMode(Relay3, OUTPUT);
- pinMode(Relay4, OUTPUT);
- digitalWrite(Relay1, LOW);
- digitalWrite(Relay2, LOW);
- digitalWrite(Relay3, LOW);
- digitalWrite(Relay4, LOW);
- attachInterrupt(digitalPinToInterrupt(pinButton1), debounceInterruptButton1, RISING);
- attachInterrupt(digitalPinToInterrupt(pinButton2), debounceInterruptButton2, RISING);
- }
- void loop() {
- if(button1){
- button1 = false;
- button2 = false;
- digitalWrite(Relay1, HIGH);
- digitalWrite(Relay2, HIGH);
- delay(1000);
- digitalWrite(Relay1, LOW);
- digitalWrite(Relay2, LOW);
- }else if(button2){
- button1 = false;
- button2 = false;
- digitalWrite(Relay3, HIGH);
- digitalWrite(Relay4, HIGH);
- delay(1000);
- digitalWrite(Relay3, LOW);
- digitalWrite(Relay4, LOW);
- }
- }
- void debounceInterruptButton1() {
- if((long)(micros() - last_micros) >= debouncing_time * 1000) {
- preButton1();
- last_micros = micros();
- }
- }
- void debounceInterruptButton2() {
- if((long)(micros() - last_micros) >= debouncing_time * 1000) {
- preButton2();
- last_micros = micros();
- }
- }
- void preButton1(){
- if(!button2)
- button1 = true;
- }
- void preButton2(){
- if(!button1)
- button2 = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement