Pages: [1]   Go Down
Author Topic: Need help with a timer  (Read 821 times)
0 Members and 1 Guest are viewing this topic.
Kansas
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
        Utility.SetLocalTime(newLocalTime);
 
        /*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);
        Thread.Sleep(Timeout.Infinite);
 
    
}
void setup(){
 
  Serial.begin(9600);  
  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
  digitalWrite(13,HIGH);
  dtAlarms.delay(1000);
  digitalWrite(13,LOW);
 }
 
}

}

And this is what it threw back at me..
Code:
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  smiley-wink
Thanks a lot, and any help will be greatly appreciated!!

Also note that im only trying to control one relay at this time.
« Last Edit: September 15, 2012, 03:07:13 am by MTB92 » Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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).

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Kansas
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

http://hacking.majenko.co.uk/finite-state-machine
Logged

Pages: [1]   Go Up
Jump to: