i need help

how to make seketch of multiple push buttons for multiple relays to Switch on/off

read the button
if needed, remember the state
switch relay depending on state
repeat for next button

You might want to have a look at the debounce and the state-change detection examples that come with the IDE.

Also check out the digitalWrite and digitalRead references.

Lastly you can not directly connect relays to the Arduino without running the risk of blowing it up. The safest way is probably using relay boards; alternatives are e.g. the use of transistors or fets (don't forget flyback diodes in that case).

Gul-Khan123456:
I want switch on/off relay with push button .I want only seketch of Arduino ide

Wish granted. Now start writing code :wink: It's not going to write itself.

And a small tip for buttons/switches, grab a library like Bounce2 :slight_smile:

septillion:
And a small tip for buttons/switches, grab a library like Bounce2 :slight_smile:

In my opinion one should not use a library for this as a beginner. Admitted, it's probably (no experience with it) easy but one does not learn from it :slight_smile:

Start from scratch and once you have encountered the problems and solved them, switch to a library.

Gul-Khan123456:
I want switch on/off relay with push button .I want only seketch of Arduino ide

I gave you the pointers. Try something using a button and the onboard led, next add a relay and another button.

If you get stuck, show us the code (using code tags), explain what you expect it to do and what it actually does and we will help further.

Have a look at Planning and Implementing a Program

...R

sterretje:
In my opinion one should not use a library for this as a beginner.

I agree if you want to learn stuff. But after that, so much cleaner to grab a library (or make on if all the libraries are not to taste).

But in this case it doesn't seem to really learn something, just to achieve something. Then it's just as simple to just grab a library just like everybody does for serial and PWM etc. Nobody learns about that so it seems...

I have seketch of single push button debounce to turn one relay .how to make the changes in seketch for 4 push button and 4 relay to on/off .

here is the code
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledState);
}

void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

// set the LED:
digitalWrite(ledPin, ledState);

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

Arrays.
The simplest way is to use arrays.

And code tags.

@Gul-Khan123456, why have you started a new Thread when you were already getting advice about the same project in your other Thread? And with the same title that tells no one what you want help with.

Also please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

…R

Thanks AWOL . but how to make the PinMode for relay switch

I'd make it an OUTPUT.
But don't forget that you're not going to be driving the relay directly from the output pin; you'll need some buffering to avoid damage to the pin.

I have a relay module of 5 v .but I cant understand how to make the changes in multiple debounce code for my four relay to switch on by push button .
here is code of multiple denounce

[ #define DEBOUNCE 10 // button debouncer, how many ms to debounce, 5+ ms is usually plenty

// here is where we define the buttons that we’ll use. button “1” is the first, button “6” is the 6th, etc
byte buttons = {4, 5, 6, 7, 8, 9};

// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)

// we will track if a button is just pressed, just released, or ‘currently pressed’
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
void setup() {
byte i;

// set up serial port
Serial.begin(9600);
Serial.print(“Button checker with “);
Serial.print(NUMBUTTONS, DEC);
Serial.println(” buttons”);
// pin13 LED
pinMode(13, OUTPUT);

// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons*, INPUT);*
_ digitalWrite(buttons*, HIGH);_
_
}_
_
}*_

void check_switches()
{
* static byte previousstate[NUMBUTTONS];*
* static byte currentstate[NUMBUTTONS];*
* static long lasttime;*
* byte index;*
* if (millis() < lasttime) {*
* lasttime = millis(); // we wrapped around, lets just try again*
* }*

* if ((lasttime + DEBOUNCE) > millis()) {*
* return; // not enough time has passed to debounce*
* }*
* // ok we have waited DEBOUNCE milliseconds, lets reset the timer*
* lasttime = millis();*

* for (index = 0; index < NUMBUTTONS; index++) {*
* justpressed[index] = 0; // when we start, we clear out the “just” indicators*
* justreleased[index] = 0;*
* currentstate[index] = digitalRead(buttons[index]); // read the button*
* if (currentstate[index] == previousstate[index]) {*
* if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {*
* // just pressed*
* justpressed[index] = 1;*
* }*
* else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {*
* // just released*
* justreleased[index] = 1;*
* }*
* pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed*
* }*
* //Serial.println(pressed[index], DEC);*
* previousstate[index] = currentstate[index]; // keep a running tally of the buttons*
* }*
}

void loop() {
* check_switches(); // when we check the switches we’ll get the current state*

* for (byte i = 0; i < NUMBUTTONS; i++) {*
_ if (justpressed*) {
Serial.print(i, DEC);
Serial.println(" Just pressed"); _
// remember, check_switches() will CLEAR the ‘just pressed’ flag*

* }*
_ if (justreleased*) {
Serial.print(i, DEC);
Serial.println(" Just released");_

// remember, check_switches() will CLEAR the ‘just pressed’ flag*

* }*
_ if (pressed*) {
Serial.print(i, DEC);
Serial.println(" pressed");
// is the button pressed down at this moment*

* }
}
} ]*_

Do you remember putting that code in italics?

No, I thought not - that's why we put code in code tags.
Here's a spare pair you can use - [code][/code] - you can edit your previous post, and put the code inside them, so people can read what you wrote.

 // This even handier macro lets us determine how big the array up above is, even if you don't use "byte" as the data type.
 #define NUMBUTTONS (sizeof(buttons) / sizeof(buttons[0]))

To make the changes in code and how to initialize 4 digital output pins in code for relay to switch on and off by push buttons

I have a relay module of 5 v .but I cant understand how to make the changes in multiple debounce code for my four relay to switch on by push button .make the changes in my code .how to make digital output pins for 4 relays to switch and using push buttons

here is code of multiple denounce

[#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty 
  
// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc 
 byte buttons[] = {4, 5, 6, 7, 8, 9};  
  
 // This handy macro lets us determine how big the array up above is, by checking the size 
 #define NUMBUTTONS sizeof(buttons) 
  
 // we will track if a button is just pressed, just released, or 'currently pressed'  
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS]; 
  void setup() { 
   byte i; 
   
  // set up serial port 
   Serial.begin(9600); 
  Serial.print("Button checker with "); 
  Serial.print(NUMBUTTONS, DEC); 
  Serial.println(" buttons"); 
 // pin13 LED 
  pinMode(13, OUTPUT); 
   
   // Make input & enable pull-up resistors on switch pins 
 for (i=0; i< NUMBUTTONS; i++) { 
     pinMode(buttons[i], INPUT); 
     digitalWrite(buttons[i], HIGH); 
   } 
 } 
   
 void check_switches() 
 { 
   static byte previousstate[NUMBUTTONS]; 
   static byte currentstate[NUMBUTTONS]; 
   static long lasttime; 
   byte index; 
   if (millis() < lasttime) { 
     lasttime = millis(); // we wrapped around, lets just try again 
  } 
   
   if ((lasttime + DEBOUNCE) > millis()) { 
     return; // not enough time has passed to debounce 
  } 
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer 
   lasttime = millis(); 
   
   for (index = 0; index < NUMBUTTONS; index++) { 
     justpressed[index] = 0;       // when we start, we clear out the "just" indicators 
     justreleased[index] = 0; 
       currentstate[index] = digitalRead(buttons[index]);   // read the button 
    if (currentstate[index] == previousstate[index]) { 
       if ((pressed[index] == LOW) && (currentstate[index] == LOW)) { 
           // just pressed 
           justpressed[index] = 1; 
      } 
       else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) { 
           // just released 
           justreleased[index] = 1; 
     } 
       pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed 
    } 
     //Serial.println(pressed[index], DEC); 
     previousstate[index] = currentstate[index];   // keep a running tally of the buttons 
   } 
 } 
 
 void loop() { 
  check_switches();      // when we check the switches we'll get the current state 
   
   for (byte i = 0; i < NUMBUTTONS; i++) { 
   if (justpressed[i]) { 
      Serial.print(i, DEC); 
       Serial.println(" Just pressed");  
       // remember, check_switches() will CLEAR the 'just pressed' flag 
    } 
    if (justreleased[i]) { 
     Serial.print(i, DEC); 
       Serial.println(" Just released"); 
       // remember, check_switches() will CLEAR the 'just pressed' flag 
     } 
     if (pressed[i]) { 
       Serial.print(i, DEC); 
       Serial.println(" pressed"); 
      // is the button pressed down at this moment 
     } 
  } 
 }

Not checked it all but

static long lasttime; is an issue. you should use unsigned long

Also

  if ((lasttime + DEBOUNCE) > millis()) {

Should be written this way to not overflow

if (millis()-lasttime < DEBOUNCE) {

Some feedback:

// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)

this does work only because you have bytes in your array. the right macro would be

// This handy expression lets us determine how big the array up above is, by checking the size
const int NUMBUTTONS = sizeof(buttons)/sizeof(buttons[0]);

it’s better to define as a const int rather than a macro because we do some maths there.

  // Make input & enable pull-up resistors on switch pins
  for (i = 0; i < NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }

could be written

  // Make input & enable pull-up resistors on switch pins
  for (i = 0; i < NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT_PULLUP);
  }

How to make OUTPUT PinMode for switch led or relay

pinMode()