Relay help required

Hi

I’m brand new here and a total newbie to any sort of programming, although I am learning… small steps

I have a program I need to create to control some relays.

On a button press I need some relays to run in sequence. If the button is re-pressed in that time it would need to start the sequence from the beginning.

Now, this sounds simple even to me but believe me I’ve spent 4 days now playing with blink without delay to try and figure it out.

I’ve planned out the relay on times in the attached.

Any help in this code would be massively appreciated, I don’t even min paying for this if anyone works freelance?

On a button press I need some relays to run in sequence.

Relays don't run. They hold still. They can be toggled on or off. "In sequence" might mean something to you, but doesn't to us. Presumably, a sequence defines the order that the pins should be changed AND the time between changes. But, we shouldn't have to guess.

Have you got a switch wired up? Can you tell when it becomes (not is) pressed?

When the switch BECOMES pressed, you change the state of some pins AND you record when you did that.

Periodically, you check to see if a pin has been in the require state long enough. If it has, you change it's state. Maybe that means recording when that happened. Maybe not. In your picture, it doesn't seem to matter.

The state change detection and blink without delay examples have everything you need.

Try something, and post the code here. We are perfectly willing to discuss the validity of the code you come up with, and to explain why it works, or doesn't work, and to suggest improvements.

Hi Paul,

Yes you are correct, they toggle on and off. I did say I'm a complete newbie at this.

I'll look at the state change detection.

Thanks for the dressing down, and a helpful point in the right direction.

Hi

Heres what I have so far for a single pin.

It doesn’t work

/*
  State change detection (edge detection)

 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.

 This example shows how to detect when a button or button changes from off to on
 and on to off.

 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)

 created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/ButtonStateChange

 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
const long interval = 1000;           // interval at which to blink (milliseconds)

// 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
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, 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 the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:

  unsigned long currentMillis = millis();
    
    if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;  
  
  if (buttonPushCounter % 4 == 0) {
 
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  
  }
  }
}

It doesn't work

It does something.

You have the code set up to do something only when the number of presses is 0, 4, 8, etc.

How IS the switch wired?

Hi Paul

I have the switch as follows

pin 2 to switch +5v to 10k resistor to switch

Thanks for your help Lee

leegrears: Hi Paul

I have the switch as follows

pin 2 to switch +5v to 10k resistor to switch

Thanks for your help Lee

The wiring in the sketch is different.

I'll change this around the morning and have ago.

Thanks Lee

I'll change this around the morning and have ago.

Get rid of the external resistor. Change the pinMode() call to make the type INPUT_PULLUP. Connect one leg to pin 2. Connect the other leg to ground. Now, when the switch is pressed, the pin will read LOW. When the switch is not pressed, the pin will read HIGH. Just like the top of the switch.