Go Down

Topic: Simply two relay timer programmer needed :) (Read 873 times) previous topic - next topic

Wignposs

I need some help please programming an arduino nano to

Turn relay (1) on for 1 hour  then off for half hour for a period of 48 hours then stop with a led stop indicator  coming on on completion.

at the same time

Turn relay (2) on for 2 hours then off for half hour for a period of 24 hours then stop with led stop indicator coming on on completion.

thank you.

i have

relay1 pin 4

relay2 pin 5

led pin 6 (to come on once finished relay1 48hour )

led pin 7 (to come on once finished relay2 24hour )

thank you,

PaulMurrayCbr

#1
Nov 22, 2015, 04:15 am Last Edit: Nov 22, 2015, 04:31 am by PaulMurrayCbr
And this will happen only once when you turn the nano on? There's no reset button, no "ok, now it the time to start the sequence" event?

Ok, here you go. Havent tested it, but should do what you said you wanted:

Code: [Select]


class OnOff {

    const int relayPin;
    const int ledPin;
    const unsigned long onTimeMs;
    const unsigned long offTimeMs;
    const unsigned long totalTimeMs;

    enum State {
      ON, OFF, DONE
    } state;

    unsigned long startMs;
    unsigned long stateMs;

  public:
    OnOff(int relayPin, int ledPin, int onTimeMinutes, int offTimeMinutes, int totalTimeMinutes) :
      relayPin(relayPin),
      ledPin(ledPin),
      onTimeMs(onTimeMinutes * 60L * 1000L),
      offTimeMs(offTimeMinutes * 60L * 1000L),
      totalTimeMs(totalTimeMinutes * 60L * 1000L)
    {
    }

    void setup() {
      pinMode(relayPin, OUTPUT);
      pinMode(ledPin, OUTPUT);
      digitalWrite(relayPin, HIGH);
      state = ON;
      stateMs = startMs = millis();
    }

    void loop() {
      switch (state) {
        case ON:
          if (millis() - startMs >= totalTimeMs) {
            digitalWrite(relayPin, LOW);
            digitalWrite(ledPin, HIGH);
            state = DONE;
            stateMs = millis();
          }
          else if (millis() - stateMs >= onTimeMs) {
            digitalWrite(relayPin, LOW);
            state = OFF;
            stateMs = millis();
          }
          else
            ; // do nothing

          break;

        case OFF:
          if (millis() - startMs >= totalTimeMs) {
            digitalWrite(ledPin, HIGH);
            state = DONE;
            stateMs = millis();
          }
          else if (millis() - stateMs >= offTimeMs) {
            digitalWrite(relayPin, HIGH);
            state = ON;
            stateMs = millis();
          }
          else
            ; // do nothing

          break;

        case DONE:
          // do nothing
          break;

      }
    }
};

OnOff relay1(4, 6, 60, 30, 60 * 48);
OnOff relay2(5, 7, 120, 30, 60 * 24);

void setup() {
  relay1.setup();
  relay2.setup();
}

void loop() {
  relay1.loop();
  relay2.loop();
}


If you like it, Karma me :)
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

AldinT

Hello,
I need help with writing one sketch
I need to control 2 relays using serial communication.When i turn on one relay they must be on for 15 second.
Same things for second relay.
However also i need to stop relay at any moment.
Thanks
P.S. Sorry for my English

PaulS

What part of that do you need help with? The simplest advice for you to follow is "DO NOT USE DELAY()". The blink without delay example is included with the IDE for a very good reason.
The art of getting good answers lies in asking good questions.

AldinT

I know that but I still can write right program.
I dont know how to use millis().
Can you make it right my sketch.

char junk;
String inputString="";
#define RELAY_ON 0
#define RELAY_OFF 1
void setup()                 
{
 Serial.begin(9600);
  digitalWrite(4, RELAY_OFF);
  digitalWrite(5, RELAY_OFF);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
}

void loop()
{
  if(Serial.available()){
  while(Serial.available())
    {
      char inChar = (char)Serial.read();
      inputString += inChar;     
    }
    while (Serial.available() > 0) 
    { junk = Serial.read() ; }     
    if(inputString == "a"){         
      digitalWrite(4, HIGH);
      Serial.println("STOP RELAY 1");
    }else if(inputString == "A"){
       Serial.println("START RELAY 1");
      digitalWrite(4, LOW);
      delay(15000);
      digitalWrite(4,HIGH);
    }else if(inputString == "b"){
      Serial.println("STOP RELAY 2");
      digitalWrite(5, HIGH);
    }else if(inputString == "B"){
       Serial.println("START RELAY 1");   
      digitalWrite(5, LOW);
      delay(15000);
      digitalWrite(5,HIGH);
     
    }
    inputString = "";
  }
}

PaulMurrayCbr

I know that but I still can write right program.
I dont know how to use millis().
Well, there's your problem right there.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

AldinT


PaulS

How would YOU make the two relays operate and still be able to read the switch? You have a watch, a pencil and some paper.

THINK about the steps you would need to perform if I called you on the phone and said to turn the oven and coffeemaker on for 15 minutes unless the fire alarm goes off.

What are the first things that you would do?

(Forget about the Arduino and how to write the code to make it do something. Until you know what it is to do, you can't write the code. Once you know what it is to do, the code is just about done. All that is left is getting the syntax correct.)
The art of getting good answers lies in asking good questions.

Go Up