Go Down

Topic: Need help with a timer (Read 1 time) previous topic - next topic


Sep 15, 2012, 10:00 am Last Edit: Sep 15, 2012, 10:07 am by MTB92 Reason: 1
Hi all! I picked up a 4 channel relay board and have been playing around with it lately. I have an arduino uno and got everything wired up, and was able to flash a 120v ac light bulb. Then i stumbled across using your keyboard to turn it on and off.. cool! i was really excited. Now im wanting to actually use this thing and make a timer to control 2(for now) 120v ac plug ins--water pump and lights for my aquaponics system. I have been at this all day, and am slowly removing compiling errors. Unfortunately, I have zero-to-none knowledge in the programming field, and im jumping in to something pretty big here, i know that. I need the light on for x amount of hours and water pump to turn on for 15 mins, off for 15, on for 15 and so on..
Here is what i haved cooked up.
Code: [Select]

#include <Time.h>
#include <DateTime.h>
#include <TimeAlarms.h>
#define RELAY_PIN 13

AlarmID_t myTimer;

void OnTimer(AlarmID_t Sender){
 // add code here to execute when the timer triggers
   Serial.println("timer triggered");
   pinMode(RELAY_PIN, OUTPUT);
   Serial.begin(9600); // open serial
  //Change the time to the current time (else will default to 01/01/2009)
       DateTime newLocalTime = new DateTime(2012,8,12,20,04,00);

       /*Creates a timer that calls the OnTimer method immediately after creation
        and then calls it every 900000 milliseconds (15 minutes or every 900 seconds)*/
       System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, 0, 900000);

void setup(){

 pinMode(13,OUTPUT); // we will flash the LED on and off each second in loop

void loop(){

 myTimer = dtAlarms.createTimer( AlarmHMS(0, 5, 0), &OnTimer); // create a timer with period: 0 hrs, 5 minutes, 0 seconds
 DateTime.sync(0);   // Start the clock  


 dtAlarms.delay(1000);  // call dtAlarms.delay instead of delay so the alarms can be serviced



And this is what it threw back at me..
Code: [Select]

jay.cpp: In function 'void OnTimer(AlarmID_t)':
jay:17: error: expected `;' before 'newLocalTime'
jay:18: error: 'Utility' was not declared in this scope
jay:18: error: 'newLocalTime' was not declared in this scope
jay:22: error: 'System' was not declared in this scope
jay:22: error: expected `;' before 'timer'
jay:23: error: 'Thread' was not declared in this scope
jay:23: error: 'Timeout' was not declared in this scope
jay.cpp: In function 'void loop()':
jay:37: error: 'dtAlarms' was not declared in this scope
jay:42: error: 'dtAlarms' was not declared in this scope
jay.cpp: At global scope:
jay:48: error: expected declaration before '}' token

What a first impression to make for you guys  ;)
Thanks a lot, and any help will be greatly appreciated!!

Also note that im only trying to control one relay at this time.


Most of those errors are because you didn't initialize the classes you are using.

Utility, System, Thread, Timeout, dtAlarms, etc... haven't been initialized.
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Nick Gammon

Code: [Select]

void OnTimer(AlarmID_t Sender){
  // add code here to execute when the timer triggers
    Serial.println("timer triggered");
    pinMode(RELAY_PIN, OUTPUT);
    Serial.begin(9600); // open serial

This won't be the reason for your errors, but I wouldn't be doing a Serial.begin here, especially after a Serial.println. That should be in setup, and only there.
Please post technical questions on the forum, not by personal message. Thanks!

More info:


That certainly does not look like Arduino code. It looks a lot more like Windows code. I think you are going about this project all wrong.


Can anybody help me get this going in the right direction? I dont want it completed by somebody. I want to figure it out. But i do need at least a little help? Anybody?


I think to get you started in the right direction you need to look at the sample sketch "blink without delay" and read the article by Majenko about finite state machines.



Go Up