arduino uno with relay module switching from on relay to the next

Hi, I started with arduino a while ago and im trying to build a realy module that uses one button to switch on relay 1 press button again switch off relay 1 and switch on relay two etc. at the end of all relays start over.

i have the hardware all wired up and used a basic sketch to test the relays.. BUT i dont understand much about programming. i want to learn offcource but for now i need someone to help me with this.

thank you very much in advance

The state change detection example shows how to detect a button push and count pushes. Then you can take actions based on the number of button pushes.

So if buttonPushCounter == 0, all relays off. If buttonPushCounter == 1, turn on relay 1. If buttonPushCounter == 2, turn off relay 1 turn on relay 2 and so on. A switch case statement could be used as well.

Switch debounce may be required.

i understand how it should work but i just dont understan programming language. for me its like traditional chinese.. :frowning:

Here is a simple sketch that lights LEDs in sequence depending on the number of times a button is pressed. The code uses the state change detection example to detect and count button presses and a switch case structure to choose the LED to light per the button press count. The language reference can help explain the different commands and functions used.

const byte  buttonPin = 7;    // the pin that the pushbutton is attached to
// pins for LEDs
const byte GLedPin = 3;
const byte YLedPin = 4;
const byte RLedPin = 5;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
  // initialize the button pin as a input, enable the internal pullup resistor:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LEDs as outputs:
  pinMode(GLedPin, OUTPUT);
  pinMode(YLedPin, OUTPUT);
  pinMode(RLedPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    if (buttonState == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter++;  // add one to counter
      if (buttonPushCounter > 3) // if couter over 3 reset the counter to 0 all off
      {
        buttonPushCounter = 0;
      }
      Serial.println(buttonPushCounter);
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  switch (buttonPushCounter)
  {
    case 0:
      digitalWrite(GLedPin, LOW);
      digitalWrite(YLedPin, LOW);
      digitalWrite(RLedPin, LOW);
      break;
    case 1:
      digitalWrite(GLedPin, HIGH);
      digitalWrite(YLedPin, LOW);
      digitalWrite(RLedPin, LOW);
      break;
    case 2:
      digitalWrite(GLedPin, LOW);
      digitalWrite(YLedPin, HIGH);
      digitalWrite(RLedPin, LOW);
      break;
    case 3:
      digitalWrite(GLedPin, LOW);
      digitalWrite(YLedPin, LOW);
      digitalWrite(RLedPin, HIGH);
      break;
  }
}

And the circuit:

Or, you could increment a counter with the switch then use the counter value in an if() statement to turn on or off the output of interest. When the counter reaches some *preset * number it resets itself and you start over at zero.