First Arduino Project

Hello guys!

Well as the title says, this is my very first arduino project and also my first experience programming at all, so please bear with me.

What I tried to do with this little circuit is very simple and basic:
-Press a button and wait for an hour, if the button is still pressed, activate a Relay
-If the relay is on, read a digital sensor input. If both conditions are on, wait for 24 hrs.
-After the 24 hrs, turn the relay off.
-Wait for another 24 hrs and repeat the cycle again.

So, I compiled it and it didnt gave out any error messages, but that doesnt mean it will execute as I think it would. I plan on running it on monday, but could any of you guys give the code a quick check to see if it will actually do what I intend to do? I used Jeremy Blum´s “Exploring Arduino” book for my basic training.

const int RELAY=9; //Salida hacia el reelevador
const int BUTTON=2; //Boton de presencia de Fuselaje
const int SENSOR=3; //Sensores inductivos

boolean lastButton= LOW;
boolean currentButton= LOW;
boolean relayOn= false;

void setup ()
{
pinMode (RELAY, OUTPUT);
pinMode (BUTTON, INPUT);
pinMode (SENSOR, INPUT);
}

boolean debounce (boolean last) //Debounce the button thats going to be pressed
{
boolean current= digitalRead (BUTTON);
if (last != current)
{
delay(5);
current = digitalRead (BUTTON);
}
return current;
}

void loop ()
{
currentButton = debounce (lastButton);
if(lastButton= LOW && currentButton== HIGH) //You have pressed the button that activates the circuit
{
delay (3600000); //Wait for an hour
}
lastButton=currentButton;
digitalWrite (RELAY, HIGH); //Turn the relay on

if(SENSOR== HIGH && RELAY== HIGH) //The relay is on and the sensors have become active
{
delay (86400000); //Wait 24 hours
}
digitalWrite (RELAY, LOW); //Deactivate relay
delay (86400000); //Wait additional 24 hours before scanning for conditions again
}

I would suggest changing the delays to something smaller so you know the basics of it work and you don't have to wait so long. If that doesn't work, do it again and see if you can identify a pattern.

You should almost certainly not use delay() at all. Instead use millis() to manage timing as illustrated in several things at a time.

I agree completely with @Squirt_5432, use short periods of a few seconds while you are developing the program.

The Arduino is not a good time keeper over long periods. If you want accurate 24hr timing you will need to use a Reat Real Time Clock (RTC) module.

...R

you will need to use a Reat Time Clock (RTC) module.

Is that one from Yorkshire then?

Squirt_5432: I would suggest changing the delays to something smaller so you know the basics of it work and you don't have to wait so long. If that doesn't work, do it again and see if you can identify a pattern.

Sure will, thanks!!

Robin2: You should almost certainly not use delay() at all. Instead use millis() to manage timing as illustrated in several things at a time.

I agree completely with @Squirt_5432, use short periods of a few seconds while you are developing the program.

The Arduino is not a good time keeper over long periods. If you want accurate 24hr timing you will need to use a Reat Time Clock (RTC) module.

...R

Let me take a looksie...thanks for the tip! will post back on monday to see how it worked, I dont really need the 24 hrs to be precise, its more like a shotgun approach if you would... still think I need the RTC?

Thanks!

I recently did some testing on the timer accuracy of an Arduino R3. I compared the timer with the PPS signal from a highly accurate RTC, the DS3231. The board time was quite stable at about +/- 5 parts per million, but 100 parts per million slow at room temperature. That's only a few seconds a day.

I couldn't help but wonder... isn't your finger going to get sore, holding a button down for an hour? :)

Grumpy_Mike: Is that one from Yorkshire then?

LOL

...R

aarg: I recently did some testing on the timer accuracy of an Arduino R3. I compared the timer with the PPS signal from a highly accurate RTC, the DS3231. The board time was quite stable at about +/- 5 parts per million, but 100 parts per million slow at room temperature. That's only a few seconds a day.

I couldn't help but wonder... isn't your finger going to get sore, holding a button down for an hour? :)

LOL it actually wont be me pressing down, a whole Fuselage will :) ... and thanks for the timer reference, I can absolutely spare those few seconds

Went back and changed the program using Millis, works like a charm. I´m just gonna post the final program in case a newbie like me ever needs something similar.

Thank you all!

const int Relay = 12; // the pin numbers for the Relay const int Sensor = 6; // the pin number for the Sensors const int ActivateRelay = 5000; // counter for relay activation

byte RelayState = LOW; // Relay initial status byte currentSensor = LOW; //Sensor initial status unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop() unsigned long previousRelayMillis = 0; // last time the Relay was updated

void setup() {

pinMode(Relay, OUTPUT); //Set relay as output

pinMode(Sensor, INPUT); // Set the sensor as input

}

void loop() {

currentMillis = millis(); // capture the latest value of millis() currentSensor= digitalRead(Sensor); updateRelayState(); switchRelay(); }

void updateRelayState() {

if (RelayState == LOW && previousRelayMillis == 0) { // if the Relay is off and its the first time we turn it on

if (currentMillis >= 1200000) { // After 20 minutes of the circuit being active, turn relay on

RelayState = HIGH;

previousRelayMillis += ActivateRelay; // Record time so we know relay has already been activated once

} } else {

if (currentMillis >= 86400000 && currentSensor == HIGH) { // if 24 hrs have elapsed since the circuit became active and the sensor is on

RelayState = LOW; //Turn the relay off

} } }

void switchRelay() {

digitalWrite(Relay, RelayState); }