Help with programming a timing device

I need some help please programming this timer

It needs 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.

Read and understand the blink without delay() example.

You will probably want a real-time-clock module. This can be done with millis(), but it won't be accurate unless you measure how accurate millis() is on your particular board and make adjustments.

The basic logic is:

setup() {
  make a note of the sketch start time;
  start relay 1;
  start relay 2;
}

loop() {
  deal with relay 1;
  deal with relay 2;
}

start_relay_1() {
  turn relay 1 on;
  note that we turned relay 1 on at the sketch start time
}

deal_with_relay_1() {
  if we have finished with relay 1, return.

  if the time since the sketch start time is 48 hours, then
    turn relay 1 off
    note that we have finished with relay 1
    return

  if relay 1 is off, and it's half hour since we turned it off
    turn relay 1 on
    note that we turned relay 1 on at the current time
    return

  if relay 1 is on, and its an hour since we turned it on
    turn relay 1 off
    note that we turned relay 1 off at the current time
    return

  otherwise
    do nothing
}

Thank you for you help this far.

This is where I'm (stuck) at.

const int ledPin = 4; // the number of the LED pin
const int ledPin2 = 5;

// Variables will change :
int ledState = LOW; // ledState used to set the LED
int ledState2 = LOW;
int counter=0;
int counter2=0;

// 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
unsigned long previousMillis2 = 0;

// constants won't change :
const long interval1 = 1000; // interval at which to blink (milliseconds)
const long interval12 = 1000;

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}

void loop()
{

unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();

if(currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// 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);
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState2);

}
}

Wignposs:
This is where I'm (stuck) at.

You need to tell us what your code is actually doing and what it should do.

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor.

...R

  unsigned long currentMillis = millis();
  unsigned long currentMillis2 = millis();

Why do you need two copies of now?

Meaningful names ARE important. previousMillis doesn't tell us WHAT happened then. lastRelay1ToggleTime does.

// if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH ;
    else
      ledState = LOW;

or

   ledState = !ledState;

Though why the hell the variable is called ledState when it contains the state of a relay is a damned mystery. Same with ledPin that holds the number of a pin that a relay is attached to.

Sorry to be a pain but none of your help has helped me yet.

I can get the relay to come on and off but need help setting the time on and off. and also counting the times they do. then turning off once finished.

Thank you

Wignposs:
Sorry to be a pain but none of your help has helped me yet.

Then please post your code again, to show how you've applied the help. In code tags.

How do i put it in code tags? code button </>
?

Read the two sticky posts at the top of the forum.

Thanks for your help but to me your all talking in code. I'll just go sort it all manually without the arduino

Wignposs:
but to me your all talking in code.

Well this is a programming forum :slight_smile:

...R

Is there any where i can get somebody to write this code? im sure it would only take a few minutes for somebody who knows how to.

Wignposs:
Is there any where i can get somebody to write this code? im sure it would only take a few minutes for somebody who knows how to.

Sure! Go on "gigs and collaborations".

http://forum.arduino.cc/index.php?board=26.0

I hope this is homework and you turn it in and get asked to explain it. It probably works but I may have made an oops somewhere.

const unsigned long MINUTE = 60000UL;
const unsigned long HOUR = MINUTE * 60UL;

class myRelay {
    byte ledPin;
    byte relayPin;
    int relayState;
    unsigned long onDuration;
    unsigned long offDuration;
    unsigned long lastToggleTime;
    unsigned long runTimeHours;

  public :

    myRelay (byte RELAY_PIN, byte LED_PIN, unsigned long ON_DURATION, unsigned long OFF_DURATION, unsigned long RUN_TIME_HOURS)
    {
      relayPin = RELAY_PIN;
      ledPin = LED_PIN;
      onDuration = ON_DURATION;
      offDuration = OFF_DURATION;
      runTimeHours = RUN_TIME_HOURS;
    }

    void begin()
    {
      pinMode(relayPin, OUTPUT);
      pinMode(ledPin, OUTPUT);
      relayState = HIGH;
      digitalWrite(relayPin, relayState);
      lastToggleTime = millis();
      runTimeHours += lastToggleTime;
    }

    void update()
    {
      unsigned long currentTime = millis();

      if (currentTime < runTimeHours)
      {
        if (relayState == HIGH && currentTime - lastToggleTime >= onDuration)
        {
          relayState = LOW;
          digitalWrite(relayPin, relayState);
          lastToggleTime = currentTime;
        }
        else if (relayState == LOW && currentTime - lastToggleTime >= offDuration)
        {
          relayState = HIGH;
          digitalWrite(relayPin, relayState);
          lastToggleTime = currentTime;
        }
      }
      else
      {
       digitalWrite(relayPin, LOW);
       digitalWrite(ledPin, HIGH);
      }
    }

};

myRelay WhatchamacallitController[] =
{
  {2, 7, HOUR * 1UL, HOUR / 2UL, HOUR * 48UL},
  {3, 8, HOUR * 2UL, HOUR / 2UL, HOUR * 24UL}
};

void setup()
{
  WhatchamacallitController[0].begin();
  WhatchamacallitController[1].begin();
}

void loop()
{
  WhatchamacallitController[0].update();
  WhatchamacallitController[1].update();
}

Thank you so much, your a legend.

I reading through the code and get confused on what pin my 2 relays connect to?

No not home work. A once off project. Thank you

I have

Relay1 connected to pin 4
relay2 connected to pin 5

finished led1 to pin6

finished led2 to pun 7

myRelay WhatchamacallitController[] =
{
  {2, 7, HOUR * 1UL, HOUR / 2UL, HOUR * 48UL},
  {3, 8, HOUR * 2UL, HOUR / 2UL, HOUR * 24UL}
};

This is creating two instances of the class called myRelay.

The myRelay constructor:

    myRelay (byte RELAY_PIN, byte LED_PIN, unsigned long ON_DURATION, unsigned long OFF_DURATION, unsigned long RUN_TIME_HOURS)

So, the two relays are to be connected to pins 2 and 3, and the two LEDs are to be connected to pins 7 and 8.

Or, change the code to reflect where you actually connected them.

Ok now i understand a little better. Thank you so much.

Thank you again. I would like to send you some beers or something to thank you.