Go Down

Topic: Parallax LCD Countdown Timer (Read 1 time) previous topic - next topic

dragonlady92

Hi! I really new to Arduino and I don't know how to go about this school project :smiley-eek-blue: : I need to display a countdown clock on my Parallax Serial LCD screen. I've gotten as far as being able to write to the LCD screen, but I don't know how to structure the timer. Here is what I've got so far: Thanks for your help!

const int TxPin = 6;

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {
   
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
 
  mySerial.begin(9600);
  delay(100);
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  delay(5);                               // Required delay
  mySerial.print("BEGIN TEST");   // First line
  mySerial.write(12);                 // Clear     
  delay(1000);                        // Wait 1 second

}
void loop() {
}

robtillaart


Simplest clock based upon what you posted  (I added symbolic defines for some magic numbers)
Code: [Select]

const int TxPin = 6;

#define CLEAR 12
#define BACKLIGHTON 17

#include <SoftwareSerial.h>

SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup()
{
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
 
  mySerial.begin(9600);
  delay(100);

  mySerial.write(CLEAR );     
  mySerial.write(BACKLIGHTON );   
  delay(5);                               // Required delay
  mySerial.print("BEGIN TEST");   // First line
  delay(1000);

  mySerial.write(CLEAR );
}

void loop()
{
  unsigned long now = millis(); // internal clock since startup

  int hours = 0;
  int minutes = 0;
  int seconds = 0;

  mySerial.write(CLEAR); 
  MySerial.print(now);

  delay(1000);
}

But that clock is displaying in milliseconds, now your turn to make hours, minutes and seconds of it.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dragonlady92

thanks! I've made a little progress but i'm still not there yet:

Code: [Select]

const int TxPin = 6;

#define CLEAR 12
#define BACKLIGHTON 17

#include <SoftwareSerial.h>

SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup()
{
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
 
  mySerial.begin(9600);
  delay(100);

  mySerial.write(CLEAR );     
  mySerial.write(BACKLIGHTON );   
  delay(5);                               // Required delay
  mySerial.print("BEGIN TEST");   // First line
  delay(1000);

  mySerial.write(CLEAR );
}

void loop()
{
  float hours,minutes,seconds;
  unsigned long now = millis(); // internal clock since startup

  //int hours = 0;
  //int minutes = 0;
  //int seconds = 0;

  mySerial.write(CLEAR); 
 
  mySerial.print(hours,now/3600000);
  mySerial.write("h ");
  mySerial.print(minutes,now/60000);
  mySerial.write("m ");
  mySerial.print(seconds,now/1000);
  mySerial.write("s ");

 
  delay(1000);
}


it displays "0h 0m 0s" but doesn't quite act how i'd like it to. I was hoping that dividing the uptime by 1000, it would give me the number of seconds elasped, but instead it just endlessly prints 0's. any thoughts?

robtillaart

you must separate the math from the displaying part,
think how you would convert 6000 seconds in hours and minutes and seconds with pen/paper

next step in code
Code: [Select]

void loop()
{
  float hours,minutes,seconds;
  unsigned long now = millis(); // internal clock since startup
 
  unsigned long ts = now/1000;  // convert milliseconds to (temporary) seconds
  int hours = ts /3600;  // calculate the whole hours in ts
  ts = ts - hours * 3600;  // subtract the hours from ts leaving minutes and seconds
 
  // fill in the right formulas for minutes and seconds and correct the variable ts if needed
  int minutes = ...
  ts = ...
  int seconds = ...
  ts = ...

  mySerial.write(CLEAR); 
  mySerial.print(hours);
  mySerial.print("h ");
  mySerial.print(minutes);
  mySerial.print("m ");
  mySerial.print(seconds);
  mySerial.print("s ");
}


write - writes only one byte
print - writes a string or a number (under the hood it calls write for every char or digit)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dragonlady92

Awesome!! It now displays the time as 'xh xm xs'... however the time starts at 1m 6s? it doesn't take that long to start up... This is what I have so far:

Code: [Select]

const int TxPin = 6;

#define CLEAR 12
#define BACKLIGHTON 17
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup()
{
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  mySerial.begin(9600);
  delay(100);
  mySerial.write(CLEAR );     
  mySerial.write(BACKLIGHTON );   
  delay(5);                        // Required delay
  mySerial.print("BEGIN TEST");    // First line
  delay(1000);
  mySerial.write(CLEAR );

}

void loop()
{

  //float hours, minutes, seconds;
  unsigned long now = millis();    // internal clock since startup
 
  unsigned long ts = now/1000;     // convert milliseconds to (temporary) seconds
  int hours = ts /3600;            // calculate the hours in ts
  ts = ts - hours * 3600;          // subtract the hours from ts leaving minutes and seconds
  int minutes = ts /60;            // calculate the minutes in ts
  ts = ts - minutes * 60;          // subtract the minutes from ts leaving seconds
  int seconds = ts;
  ts = ts - seconds * 1;


  mySerial.write(CLEAR); 
  mySerial.print(hours);
  mySerial.print("h ");
  mySerial.print(minutes);
  mySerial.print("m ");
  mySerial.print(seconds);
  mySerial.print("s ");

  delay(1000);
}

Go Up