Pages: [1]   Go Down
Author Topic: Another way to set the Time with the Arduino Time library  (Read 766 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and thanks to everyone who can help me finishing my project.

First of all, im very new to Arduino but im trying and testing several examples and made it possible to to dimm 3 LED´s (to mix colours) with 1 Poti and 1 Button and stored them to EEprom to be able to read the last state after a powerloss.

Its not much but it feels great for a beginner.  smiley-grin

All this will be used in a self createt Pond Lighting which will hopefully be able to change colour, fade in and out to simlulate a sunrise, clouds, moonlight etc... Maybe ill add some Sensors like Temp to measure the Water and some more.

Later on, when i think im able to use it, ill use a Shield to access everything via LAN or WLAN. It depends on my skill, which will hopefully increase.

But right now im messing around with time.

Im useing the example code from the Playground.

Code:
#include <Time.h> 

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message

// T1262347200  //noon Jan 1 2010

void setup()  {
  Serial.begin(9600);
}

void loop(){   
  if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus() == timeNotSet)
    Serial.println("waiting for sync message");
  else   
      digitalClockDisplay(); 
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of header & 10 ASCII digits
    char c = Serial.read() ;
    Serial.print(c); 
    if( c == TIME_HEADER ) {     
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){ 
        c = Serial.read();         
        if( c >= '0' && c <= '9'){ 
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number   
        }
      } 
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    } 
  }
}

I used "setTime(hr,min,sec,day,month,yr); // another way to set the time" in void lood to set the Time and it worked.
Afterwards i tryed to move it to "void processSyncMessage()" and added a pushcounter and a button and also a potentiometer and map to map the values to the required values.

I added some lines with Serial.print to see what happens but nothing worked.

Can someone help me with some lines of code which would help to set the time with a button and a potentiometer?

I know the time is not realy accurate but for testing and learning it will work.

Thanks for ur time.
Logged

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

Quote
Can someone help me with some lines of code which would help to set the time with a button and a potentiometer?
I think it would be easier to set the time using a switch and a potentiometer. But, you really need to define the requirements. What is the switch state (change) going to tell you? What is the potentiometer value going to tell you?

Quote
I added some lines with Serial.print to see what happens but nothing worked.
Being a newbie, we'll let you get away with this this one time. No more, though.

The code did exactly what you told it to do. That may not be what you wanted it to do. You need to describe what it did, what you wanted it to do, and how those two differ.

Looking at your processSyncMessage() function, though, it appears that you've fallen into the usual trap of assuming that serial data transmission is nearly instantaneous. It is not. The Arduino can read data from the buffer far faster than the data can get into the buffer. The PC is sending a message sssslllloooowwwwllllyyyy. You are reading it quickly, and run out of stuff to read before the PC has finished sending. Yet, you assume that you have enough data to set the time/date. You do not.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used the Switch to go to the next step and the Potentiometer to change the values.

It worked in my sketch for the LED´s...

A Counter counted the pushes of the button and wrote the Values of the poti to the pwm output to the LED´s
step by step for each colour and  sended the Values to the Serial Monitor and later to the LCD. The last time the
button was pressed, the counter returned to 0 and saved the Values to the EEPROM, but this 1 has to be changed (later).
So it will not be stored everytime the lightcolour will be changed.

My Idea was to send HR to the Serial Monitor and the mapped values of the Poti, when the button is pressed once, the value is stored and
we continue with the minutes, day etc...

This is the code i used:

Code:
poti = analog.Read(A15);

if (buttonPushCounter == 6) {
  buttonPushCounter = 0;
}
  lastButtonState = buttonState;
  if (buttonPushCounter == 0) {
    maphr = map(poti, 0, 1023, 0, 23);
    Serial.print("HR ");
    Serial.println(maphr);
    digitalWrite(ledPin, HIGH);      //just an indicator to ensure we are at 0
  } else {
   digitalWrite(ledPin, LOW);
  }
  if (buttonPushCounter == 1) {
    mapmin = map(poti, 0, 1023, 00, 59);
    Serial.print("MIN ");
    Serial.println(mapmin);
  }
 
  if (buttonPushCounter == 2) {
    mapday = map(poti, 0, 1023, 1, 31);
    Serial.print("DAY ");
    Serial.println(mapday);
  }
 
  if (buttonPushCounter == 3) {
    mapmonth = map(poti 0, 1023, 1, 12);
    Serial.print("MONTH ");
    Serial.println(mapmonth);
  }
 
  if (buttonPushCounter == 4) {
    mapyear = map(poti 0, 1023, 2011, 2200)    //enough time for me :-), maybe it will be changed
    Serial.print("Year ");                     //to a counter which will be controlled via the poti
    Serial.println(mapyear);
  }
 
  if (buttonPushCounter == 5)  {
    setTime(maphr,mapmin,00,mapday,mapmonth,mapyear);
    buttonPushCounter = 0;
  }
Logged

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

Quote
This is the code i used:
OK. Is there a problem?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put the code into "void processSyncMessage()" which is called, when the time is not set. and it doesnt work.


I expect that i get "Waiting for Sync waiting for sync message" from void loop and HR, MIN etc...
via the Serial Monitor and that im able to adjust the time.

But i get only the message "Waiting for Sync waiting for sync message" from void loop.
Logged

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

Snippets-r-us.com is down the internet a ways. Here, we need to see all of your code. They must be more clairvoyant down there.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I Used the example as given from the Playground and mentioned above.
I hoped i can manage to edit the code and make it suitable for my projrct.

Here is the complete stuff.


Code:
#include <Time.h> 

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message

int ledPin = 13;
int poti = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int buttonState = 0;
int maphr = 0;
int mapmin = 0;
int mapday = 0;
int mapmonth = 0;
int mapyear = 0;

// T1262347200  //noon Jan 1 2010

void setup()  {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop(){   
  if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus() == timeNotSet)
    Serial.println("waiting for sync message");
  else   
      digitalClockDisplay(); 
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  poti = analogRead(A15);

if (buttonPushCounter == 6) {
  buttonPushCounter = 0;
}
  lastButtonState = buttonState;
  if (buttonPushCounter == 0) {
    maphr = map(poti, 0, 1023, 0, 23);
    Serial.print("HR ");
    Serial.println(maphr);
    digitalWrite(ledPin, HIGH);      //just an indicator to ensure we are at 0
  } else {
   digitalWrite(ledPin, LOW);
  }
  if (buttonPushCounter == 1) {
    mapmin = map(poti, 0, 1023, 00, 59);
    Serial.print("MIN ");
    Serial.println(mapmin);
  }
 
  if (buttonPushCounter == 2) {
    mapday = map(poti, 0, 1023, 1, 31);
    Serial.print("DAY ");
    Serial.println(mapday);
  }
 
  if (buttonPushCounter == 3) {
    mapmonth = map(poti, 0, 1023, 1, 12);
    Serial.print("MONTH ");
    Serial.println(mapmonth);
  }
 
  if (buttonPushCounter == 4) {
    mapyear = map(poti, 0, 1023, 2011, 2200);    //enough time for me :-), maybe it will be changed
    Serial.print("Year ");                     //to a counter which will be controlled via the poti
    Serial.println(mapyear);
  }
 
  if (buttonPushCounter == 5)  {
    setTime(maphr,mapmin,00,mapday,mapmonth,mapyear);
    buttonPushCounter = 0;
  }
}
Logged

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

buttonPushCounter is initialized to 0. Where is it ever incremented? Where is the state of the switch read?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, forgot this part as i rewrote everything.
Didnt save it cause it didnt work.

The Code is right now as following, but it still doesnt work.
And it should print "HR" also without the incremention via the buttonPin.
But thanks. I think my search for the missing part would have took a long time
for me.

Code:
#include <Time.h> 

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message

const int  buttonPin = 52;

int ledPin = 13;
int poti = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int buttonState = 0;
int maphr = 0;
int mapmin = 0;
int mapday = 0;
int mapmonth = 0;
int mapyear = 0;

// T1262347200  //noon Jan 1 2010

void setup()  {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop(){   
  if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus() == timeNotSet)
    Serial.println("waiting for sync message");
  else   
      digitalClockDisplay(); 
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  poti = analogRead(A15);
 
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
 
  buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
  }

if (buttonPushCounter == 6) {
  buttonPushCounter = 0;
}
  lastButtonState = buttonState;
  if (buttonPushCounter == 0) {
    maphr = map(poti, 0, 1023, 0, 23);
    Serial.print("HR ");
    Serial.println(maphr);
    digitalWrite(ledPin, HIGH);      //just an indicator to ensure we are at 0
  } else {
   digitalWrite(ledPin, LOW);
  }
  if (buttonPushCounter == 1) {
    mapmin = map(poti, 0, 1023, 00, 59);
    Serial.print("MIN ");
    Serial.println(mapmin);
  }
 
  if (buttonPushCounter == 2) {
    mapday = map(poti, 0, 1023, 1, 31);
    Serial.print("DAY ");
    Serial.println(mapday);
  }
 
  if (buttonPushCounter == 3) {
    mapmonth = map(poti, 0, 1023, 1, 12);
    Serial.print("MONTH ");
    Serial.println(mapmonth);
  }
 
  if (buttonPushCounter == 4) {
    mapyear = map(poti, 0, 1023, 2011, 2200);    //enough time for me :-), maybe it will be changed
    Serial.print("Year ");                     //to a counter which will be controlled via the poti
    Serial.println(mapyear);
  }
 
  if (buttonPushCounter == 5)  {
    setTime(maphr,mapmin,00,mapday,mapmonth,mapyear);
    buttonPushCounter = 0;
  }
}
Logged

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

Quote
And it should print "HR" also without the incremention via the buttonPin.
That assumes that something is sending the Arduino serial data. Is there something doing that?

If so, why don't you ever read that data?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And how to get the code working as i want it to work?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Was playing a litle bit with the sketch and it didnt work.
There was a jumper cable missing....

Right now the sketch is doing what i want to have.

Your help was not directly what i was searching for, but i think it was the way it should be.
You pointed me in the right direction.

Thanks a lot.

Ill write some comments in the code and post it when im done.
Logged

Pages: [1]   Go Up
Jump to: