New to arduino programming, need help please

Hi, i am trying to get a relay to turn on for an hour then turn off. I would like this to automatically start as soon as the arduino has power.

I am using a Nano V3.0. Here is my code so far that i have tried to put together with bits from the internet.

Please ignore the parts for indication leds and the 5 second time(this needs to be 1 hour)

//Arduino Nano V3.0---ATmega328

//** Turns Relay on for ONE Hour Then Turns It Off

//**Libraries
#include <elapsedMillis.h>

int PowerLed = 2;
int OnLed = 3;
int RelayOne = 4;
//int OnSwitch = 1;

void setup()
{
pinMode(PowerLed, OUTPUT);
pinMode(OnLed, OUTPUT);
pinMode(RelayOne, OUTPUT);
//pinMode(OnSwitch, INPUT); //Initiates Arduino Reset Switch When Pressed
}

elapsedMillis timer0;

#define interval 5000 // the interval in mS
boolean timer0Fired;

// the setup routine runs once when you press reset:

pinMode(RelayOne, OUTPUT);
digitalWrite(RelayOne, HIGH);
timer0Fired = false;
timer0 = 0; // clear the timer at the end of startup
}

void loop() {
if ((!timer0Fired) && (timer0 > interval)) {
timer0Fired = true; // don't execute this again
digitalWrite(RelayOne, LOW); // turn Relay off after 5 sec
}

I hope someone can advise me :~

Does the system have to do anything after one hour has passed?

no, it will have the power disconnected after the one hour period until it is needed again

Does the arduino need to perform other tasks in the mean time?

No other tasks at all. It needs to switch a relay on which will be connected to a pump, run for 1 hour then disconnect the relay after 1 hour

I have this code that compiles ok

//Arduino Nano V3.0---ATmega328

//** Turns Relay on for ONE Hour Then Turns It Off

//**Libraries

int PowerLed = 2;
int OnLed = 3;
int RelayOne = 4;
//int OnSwitch = 1;

void setup()
{
//Enable Serial comms
Serial.begin(9600);

//Pin Outputs
pinMode(PowerLed, OUTPUT);
pinMode(OnLed, OUTPUT);
pinMode(RelayOne, OUTPUT);
//pinMode(OnSwitch, INPUT); //Initiates Arduino Reset Switch When Pressed

// the setup routine runs once when you press reset:

pinMode(RelayOne, OUTPUT);
digitalWrite(RelayOne, HIGH);
delay(60 * 60 * 1000);
digitalWrite(RelayOne, LOW);

}

void loop()
{
}

This:

  delay(60 * 60 * 1000);

would be better as:

  delay(60UL * 60UL * 1000UL);

To avoid overflow issues, although actually one of the numbers having a UL suffix would do.

Then swap out one of the 60s for 1 and test it! Unless you want to watch it for an hour of course.

Thanks. The second code is working with an led wired up instead of a relay. As soon as my relays turn up, i can crack on with it and add features like pump on indication led and power indication led

I have added a section so that an led turns on when the relay is triggered and goes off when relay disconnects. It compiles and uploads. When i run the Arduino, it doesn't turn off after its stated time of one minute.

Could someone please check it over for me

//Arduino Nano V3.0---ATmega328

//** Turns Relay on for ONE Hour Then Turns It Off

//**Libraries
#include <elapsedMillis.h>

int PowerLed = 2;
int OnLed = 3;
int RelayOne = 4;
//int OnSwitch = 1;

boolean running = false;

void setup() {
//Enable Serial comms
Serial.begin(9600);

//Pin Outputs
pinMode(PowerLed, OUTPUT);
pinMode(OnLed, OUTPUT);
pinMode(RelayOne, OUTPUT);
//pinMode(OnSwitch, INPUT); //Initiates Arduino Reset Switch When Pressed
}

void loop()
{

byte buttonState = digitalRead(RelayOne); // read the state of the pushbutton value
// check if the pushbutton is pressed.
if (buttonState == HIGH) { // if it is, the buttonState is HIGH:
digitalWrite(OnLed, HIGH); // turn LED on:
}
else {
digitalWrite(OnLed, LOW); // turn LED off:

// the setup routine runs once when you press reset
pinMode(RelayOne, OUTPUT);
digitalWrite(RelayOne, HIGH);
delay(60UL * 1UL * 1000UL);
digitalWrite(RelayOne, LOW);
}
}

Could someone please check it over for me

Could someone learn to post code correctly?

http://forum.arduino.cc/index.php?topic=97455.0

//**********Arduino Nano V3.0---ATmega328**********

//** Turns Relay on for ONE Hour Then Turns It Off

//**Libraries
#include <elapsedMillis.h>

int PowerLed = 2;
int OnLed = 3;
int RelayOne = 4;
//int OnSwitch = 1;

void setup() {             
//Enable Serial comms
Serial.begin(9600);

//Pin Outputs
  pinMode(PowerLed, OUTPUT);   
  pinMode(OnLed, OUTPUT);  
  pinMode(RelayOne, OUTPUT); 
  //pinMode(OnSwitch, INPUT); //Initiates Arduino Reset Switch When Pressed
}

void loop() 
{
int buttonState = 0;

buttonState = digitalRead(RelayOne);        // read the state of the pushbutton value
                                            // check if the pushbutton is pressed.                                          
  if (buttonState == HIGH) {                // if it is, the buttonState is HIGH:
    digitalWrite(OnLed, HIGH);              // turn LED on:    
 } 
  if (buttonState == LOW) { 
    digitalWrite(OnLed, LOW);               // turn LED off:
  }
{
  
// the setup routine runs once when you press reset
  pinMode(RelayOne, OUTPUT);
  digitalWrite(RelayOne, HIGH);
  delay(2000);
  digitalWrite(RelayOne, LOW);
    } 
}
// the setup routine runs once when you press reset

Talk about a useless assed comment. Why is this in loop()?

  pinMode(RelayOne, OUTPUT);

Why is this in loop()?

{
  
// the setup routine runs once when you press reset
  pinMode(RelayOne, OUTPUT);
  digitalWrite(RelayOne, HIGH);
  delay(2000);
  digitalWrite(RelayOne, LOW);
    }

What are these curly braces for?

Perhaps you need a delay() after turning the relay off, before you turn it on again on the next pass through loop(), so that you can actually see it change to off.

There is NOTHING in that code that is concerned about time, so the comment at the top is misleading.

Why the bad attitude when someone asks for help? Mistakes in the coding are bound to happen and that is the whole point in asking for someone to look it over. I am currently trying to learn to code so useless assed comments on my lame attempt at code are not appreciated. The genuine help is appreciated

robster84:
useless assed comments

You may not like his style but @PaulS's comments are usually accurate.

Obviously the compiler doesn't care where you put comments or whether they make sense. But using sensible comments is very helpful to humans reading code - mostly, helpful for you. If you make a practice of having accurate comments you can often see flaws in your logic.

Update your code and show us the new version in a new post - i.e. leave the earlier post unchanged.

...R