Go Down

Topic: Arduino resets when sent the time. (Read 2 times) previous topic - next topic

dakROLO

I am trying to sync the time on the arduino to the time on my comp.

Here is what I am typing in the terminal.
Quote

TZ_adjust=-7; sudo echo "T$(($(date +%s)+60*60*$TZ_adjust))" >/dev/cu.usbmodem1411

Here is the code.

Quote






#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h> 
#include <TimeAlarms.h>

#define I2C_ADDR    0x27
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

#define TIME_MSG_LEN  11   
#define TIME_HEADER  'T'
#define TIME_REQUEST  7

int n = 1;
int onePin =  A11;
int twoPin =  A12;
int threePin =  A13;
int fourPin =  A14;
int temperaturePIN = 8;

LiquidCrystal_I2C   lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);



void setup()
{
  Serial.begin(9600);
  Alarm.alarmRepeat(10,29,50, onAlarm);
  Alarm.alarmRepeat(20,29,50, offAlarm);
  lcd.begin (20,4);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();
  setSyncProvider( requestSync);
  lcd.print("Waiting for sync message");
  
  lcd.clear();
  lcd.print("Dakota & Briana King"); 
  lcd.setCursor ( 0, 1 );
  lcd.print("Reef Tank");
  lcd.setCursor ( 0, 2 );
  lcd.print("Controller");
  lcd.setCursor ( 0, 3 );
  lcd.write("5 sec Start");
  Alarm.delay(5000);
  lcd.clear();
  lcd.home ();
  lcd.print("Time                ");
  Alarm.delay(1000);
  lcd.setCursor ( 0, 1 );
  lcd.print("Temp.In/    Out/    ");
}

void loop()
{
  float temperature = getVoltage(temperaturePIN);
  temperature = (temperature - .5) * 100;
  temperature = (temperature * 9.0 / 5.0) + 32.0;
  lcd.setCursor ( 16, 1);
  lcd.print(temperature, 1);
  lcd.setCursor (0,0);
  if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus() == timeNotSet)
    {lcd.print("Waiting for Sync");
    } else {
    digitalClockDisplay(); } 
  Alarm.delay(1000);
}

void onAlarm()
{
  lcd.setCursor ( 0, 2 );
  lcd.print("Turning on Lights   ");
  Alarm.delay(1000);
  lcd.setCursor ( 0, 3 ); 
  lcd.write("Blue Lights On");
  analogWrite(onePin, 0);
  analogWrite(threePin, 0);
  Alarm.delay(360000);
  lcd.setCursor ( 0, 3 );
  lcd.print("All Lights On       ");
  analogWrite(twoPin, 0);
  analogWrite(fourPin, 0);
}

void offAlarm()
{
  lcd.setCursor ( 0, 2 );
  lcd.print("Turning off Lights  ");
  Alarm.delay(1000);
  lcd.setCursor ( 0, 3 );
  lcd.print("White Lights Off    ");
  analogWrite(twoPin, 10);
  analogWrite(fourPin, 10);
  Alarm.delay(360000);
  lcd.setCursor ( 0, 3 ); 
  lcd.write("Blue Lights Off     ");
  analogWrite(onePin, 10);
  analogWrite(threePin, 10);
}

float getVoltage(int pin){
  return (analogRead(pin) * .004882814);
}

void digitalClockDisplay(){
  lcd.print(hourFormat12());
  printDigits(minute());
  printDigits(second());
  lcd.print(" ");
  lcd.print(day());
  lcd.print(" ");
  lcd.print(month());
  lcd.print(" ");
  lcd.print(year());
}

void printDigits(int digits){
  lcd.print(":");
  if(digits < 10)
    lcd.print('0');
  lcd.print(digits);
}

void processSyncMessage() {
  while(Serial.available() >=  TIME_MSG_LEN ){ 
    char c = Serial.read() ;
    lcd.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') ;   
        }
      }   
      setTime(pctime);  
    }  
  }
}

time_t requestSync()
{
  lcd.write(TIME_REQUEST); 
  return 0;
}



PaulS

Have you verified that the script output is correct before sending it to the serial port?

What, exactly, does the script output look like?

Those don't look much like code tags.

dakROLO

I am unsure of what you mean. But the code i put in the terminal is from the Playground library. I have only changed the usb port to match my computer. I got it to work once when i waited to enter the password till it was to the loop function. But i want to be able to do this with out the arduino resetting

DOD666

Hello dakROLO,

I had the same thing the Arduino resetting when sending data to it.

For me switching off DTR solved that.

greetings

Jens

dakROLO

I have the mega. What is the easiest software way of doing this. I am using a mac.

dakROLO

I can set the time now why does it show like wierd charecters before each digit of time?

PaulS

Quote
I can set the time now why does it show like wierd charecters before each digit of time?

Because your code is wrong.

dakROLO

I can see that but which part of the time is wrong. I pulled it from the time example.

PaulS

Quote
I can see that

That puts you one up on us. We can't see your code.

dakROLO

It not showing in the first post?

PaulS

Quote
It not showing in the first post?

If you replaced the text in the first post, don't expect me to scroll back and find it. Changing something in an earlier post, after someone has commented on it, is rude.

dakROLO

I have not changed any thing. Ut here it is again.




#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>

#define I2C_ADDR    0x27
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

#define TIME_MSG_LEN  11   
#define TIME_HEADER  'T'
#define TIME_REQUEST  7

int n = 1;
int onePin =  A11;
int twoPin =  A12;
int threePin =  A13;
int fourPin =  A14;
int temperaturePIN = 8;

LiquidCrystal_I2C   lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);



void setup()
{
  Serial.begin(9600);
  Alarm.alarmRepeat(10,29,50, onAlarm);
  Alarm.alarmRepeat(20,29,50, offAlarm);
  lcd.begin (20,4);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();
  setSyncProvider( requestSync);
  lcd.print("Waiting for sync message");
 
  lcd.clear();
  lcd.print("Dakota & Briana King"); 
  lcd.setCursor ( 0, 1 );
  lcd.print("Reef Tank");
  lcd.setCursor ( 0, 2 );
  lcd.print("Controller");
  lcd.setCursor ( 0, 3 );
  lcd.write("5 sec Start");
  Alarm.delay(5000);
  lcd.clear();
  lcd.home ();
  lcd.print("Time                ");
  Alarm.delay(1000);
  lcd.setCursor ( 0, 1 );
  lcd.print("Temp.In/    Out/    ");
}

void loop()
{
  float temperature = getVoltage(temperaturePIN);
  temperature = (temperature - .5) * 100;
  temperature = (temperature * 9.0 / 5.0) + 32.0;
  lcd.setCursor ( 16, 1);
  lcd.print(temperature, 1);
  lcd.setCursor (0,0);
  if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus() == timeNotSet)
    {lcd.print("Waiting for Sync");
    } else {
    digitalClockDisplay(); }
  Alarm.delay(1000);
}

void onAlarm()
{
  lcd.setCursor ( 0, 2 );
  lcd.print("Turning on Lights   ");
  Alarm.delay(1000);
  lcd.setCursor ( 0, 3 ); 
  lcd.write("Blue Lights On");
  analogWrite(onePin, 0);
  analogWrite(threePin, 0);
  Alarm.delay(360000);
  lcd.setCursor ( 0, 3 );
  lcd.print("All Lights On       ");
  analogWrite(twoPin, 0);
  analogWrite(fourPin, 0);
}

void offAlarm()
{
  lcd.setCursor ( 0, 2 );
  lcd.print("Turning off Lights  ");
  Alarm.delay(1000);
  lcd.setCursor ( 0, 3 );
  lcd.print("White Lights Off    ");
  analogWrite(twoPin, 10);
  analogWrite(fourPin, 10);
  Alarm.delay(360000);
  lcd.setCursor ( 0, 3 ); 
  lcd.write("Blue Lights Off     ");
  analogWrite(onePin, 10);
  analogWrite(threePin, 10);
}

float getVoltage(int pin){
  return (analogRead(pin) * .004882814);
}

void digitalClockDisplay(){
  lcd.print(hourFormat12());
  printDigits(minute());
  printDigits(second());
  lcd.print(" ");
  lcd.print(day());
  lcd.print(" ");
  lcd.print(month());
  lcd.print(" ");
  lcd.print(year());
}

void printDigits(int digits){
  lcd.print(":");
  if(digits < 10)
    lcd.print('0');
  lcd.print(digits);
}

void processSyncMessage() {
  while(Serial.available() >=  TIME_MSG_LEN ){ 
    char c = Serial.read() ;
    lcd.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') ;   
        }
      }   
      setTime(pctime); 
    } 
  }
}

time_t requestSync()
{
  lcd.write(TIME_REQUEST); 
  return 0;
}

PaulS

Code: [Select]
  lcd.print(":");
  if(digits < 10)
    lcd.print('0');

Why the inconsistency? Single or double quotes everywhere. If the single quotes are what result in the "wierd charecters", don't use them.

dakROLO

Thank you. I will try this.

This code came from the time.alarm example in playground

dakROLO

I tried this and nothing changed. It shows this charecter before each hour, minute, second, day, month, and year.  It doesnt do it right away but after a while.

Go Up