Код для поиска

Using Four Push Buttons to Toggle Four Relays with an Arduino

Using Four Push Buttons to Toggle Four Relays with an Arduino

You will learn how to control a 4-channel relay with four push buttons, with push-on and push-off functionality for each relay, using the provided Arduino code (available for download).

This code is designed to toggle the light. If you want to use push-and-hold to turn on and release to turn off, see this code.

Изображения

Использование 4 кнопок переключения push-ON и push-OFF для управления 4 реле с Arduino
Использование 4 кнопок переключения push-ON и push-OFF для управления 4 реле с Arduino
Использование 4 кнопок с включением и выключением для переключения 4 реле с Arduino
Использование 4 кнопок с включением и выключением для переключения 4 реле с Arduino
181-Using four push-button, push-on/push-off toggles to control four relays with an Arduino.
Язык: C++
/*
 *  Arduino code to push-on and push-off 4 relays
 *  when button pushed, relay ON and stay ON
 *  push again to make it OFF and stay OFF
 * This can be done with 4 push buttons to control 4 relays.
 *  
 * written by Ahmad Shamshiri for Robojax.com 
 * on December 11, 2018 at 19:39 in Ajax, Ontario, Canada

Watch video instruction for this code:https://youtu.be/3EZecphNdAI
Get this code and other Arduino codes from Robojax.com
 * 
  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
const int pushButton[] ={2,3,4,5};// define push button inputs
const int relayPin[]={11,10,9,8};// output pins where 4 relays will be connected
String relayNames[] ={"CH1", "CH2","CH3","CH4"};// Just put name for 4 relays
int pushed[] ={0,0,0,0};// status of each buttons
int relayStatus[] ={HIGH,HIGH,HIGH,HIGH};// initial status of relay


void setup() {
  // Robojax.com 4-Relay-4-Push button 20181211
  Serial.begin(9600);// initialize serial monitor 
  for(int i=0; i<4; i++)
  {
    pinMode(pushButton[i], INPUT_PULLUP); 
    pinMode(relayPin[i], OUTPUT);   
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
  }

// Robojax.com 4-Relay-4-Push button 20181211
}

void loop() {
// Robojax.com 4-Relay-4-Push button 20181211

  for(int i=0; i<4; i++)
  {
     int  val = digitalRead(pushButton[i]);   
    if(val == HIGH && relayStatus[i] == LOW){
  
      pushed[i] = 1-pushed[i];
      delay(100);
    }// if   

  relayStatus[i] = val;

      if(pushed[i] == HIGH){
        Serial.print(relayNames[i]);
        Serial.println(" ON");
        digitalWrite(relayPin[i], LOW); 
       
      }else{
        Serial.print(relayNames[i]);
        Serial.println(" OFF");
        digitalWrite(relayPin[i], HIGH);
   
      }// else   
 
  }// for 
    Serial.println("=="); 
  delay(100);
// Robojax.com 4-Relay-4-Push button 20181211  
}// loop end

Ресурсы и ссылки

Файлы📁

Нет доступных файлов.