Hi! I really new to Arduino and I don't know how to go about this school project : 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!
Simplest clock based upon what you posted (I added symbolic defines for some magic numbers)
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.
thanks! I've made a little progress but i'm still not there yet:
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?
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
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)
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:
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);
}
I figured out a way to make the time start at 0h 0m 0s, but it displays the millis on the next line. which is not ideal. I also put an alarm that goes off after 30 minutes, which is what i need for my project, but I wonder if there is a way to make it count down from 30 minutes instead though?
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 /1;
 ts = ts - seconds * 1;
 mySerial.write(CLEAR);Â
 delay(5);
 mySerial.print(hours);
 mySerial.print("h ");
 mySerial.print(minutes);
 mySerial.print("m ");
 mySerial.print(seconds);
 mySerial.print("s ");
 mySerial.write(148);       //carraige return
 mySerial.print(now);       //display current millis, otherwise the time starts at 1m6s?!
 delay(1000);
Â
 if(minutes == 30)
 {
 mySerial.write(220);
 mySerial.write("BOOM");Â
 }
Â
}
it displays the millis on the next line. which is not ideal.
Where do you want it displayed?
mySerial.write(148); //carraige return
mySerial.print(now); //display current millis, otherwise the time starts at 1m6s?!
Does this carriage return push millis (actually "now") to the next line? Is this what you consider to be not ideal?
I wonder if there is a way to make it count down from 30 minutes instead though?
To do this, can you figure out the starting time, add 30 minutes and then subtract the current time from the starting time? You'll have to change your "Boom" criterion.