Hi, I am currently trying to create a simple clock but I'm quite new to the whole code process of it, I think the setting up of it should be simple, I have a Cytron LCD keypad Shield, Cytron.io - Simplifying Digital Making, and an Arduino Uno, I really need help making the code, I've looked at examples online to try and give me guidance but I cannot seem to find anything to make it compile properly. Can anyone give me a hand, it would be much appreciated
Sure heres my code so far, It is really irrelevant to what i want to do, but I think its a good base to start my code:
// include the library code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("LCD Key Shield");
lcd.setCursor(0,1);
lcd.print("Press Key:");
}
void loop() {
int x;
x = analogRead (0);
lcd.setCursor(10,1);
if (x < 100) {
lcd.print ("Right ");
}
else if (x < 200) {
lcd.print ("Up ");
}
else if (x < 400){
lcd.print ("Down ");
}
else if (x < 600){
lcd.print ("Left ");
}
else if (x < 800){
lcd.print ("Select");
}
}
See if this works, ALSO as of Arduino 1.0, the LCD library changed, just pointing that out. Upgrade to 1.0.2
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int clockIN = A0; //CHANGED
int x; //CHANGED
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("LCD Key Shield");
lcd.setCursor(0,1);
lcd.print("Press Key:");
}
void loop() {
x = analogRead(clockIN);
lcd.setCursor(10,1);
if (x < 100) {
lcd.print ("Right ");
}
else if (x < 200 && x > 100) {
lcd.print ("Up ");
}
else if (x < 400 && x > 200){
lcd.print ("Down ");
}
else if (x < 600 && x > 400){
lcd.print ("Left ");
}
else if (x < 800 && x > 600){
lcd.print ("Select");
}
else{/*not needed but good to have*/}
}
// include the library code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("LCD Key Shield");
lcd.setCursor(0,1);
lcd.print("Press Key:");
}
void loop() {
int x;
x = analogRead (0);
lcd.setCursor(10,1);
if (x < 100) {
lcd.print ("Right ");
}
else if (x < 200) {
lcd.print ("Up ");
}
else if (x < 400){
lcd.print ("Down ");
}
else if (x < 600){
lcd.print ("Left ");
}
else if (x < 800){
lcd.print ("Select");
}
}
The Arduinos have no idea of "time" in the sense of day/month/date/hours etc. They can only do elapsed or "stop watch time". If you need day/date type time you need an RTC or you have to send from your PC (or what ever).
sorry for the repost. Okay, when i plug it in and upload my code to the arduino, it just displays on the front "LCD Key Shield" underneath it says "press key" then according to what button i press on the keypad it will read left right up down or select, does anyone know the code i could use to make it display the time it would be greatly helpful
What you show looks like no more than a button tester, and has nothing to do with telling the time.
If you just want elapsed time since power-on. You need to count the milliseconds. I'm sure there are examples of that in the Arduino IDE. The Helloworld LCD sketch is a start.
If you want to tell the time, you need a clock. I recommend the DS1307.This will cost a couple of dollars. There are plenty of public domain sketches that will do all you need.
Here are some basic code to display time without RTC
// include the library code:
#include <LiquidCrystal.h>
#include <DateTime.h>
#define DEBOUNCE_TIME 40 // switch must be pressed at least this number of ms
#define CLOCK_ROW 0
#define CLOCK_COL 0
#define NO_SWITCH 255
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void blinkingCursor(boolean enable){
if(enable)
lcd.command(0xf); // blink cursor
else
lcd.command(0xc); // just enable the display
}
void setup(){
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("LCD Key Shield");
}
void loop(){
if(DateTime.available()) { // update clocks if time has been synced
digitalClockDisplay( ); // update digital clock
}
if(DateTime.available()== false)
{
DateTime.sync(1230768000); // if clock was never set, start at Jan 1 2009
DateTime.available();
}
delay(100);
}
void digitalClockDisplay(){
// digital clock display of current date and time
lcd.setCursor( CLOCK_COL, CLOCK_ROW); lcd.clear();
if(DateTime.Hour < 10)
lcd.print(' ');
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
}
void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits,DEC);
}
the information that is sent is a standard way of encoding date and time, sometimes called unix time (see Unix time - Wikipedia)
This time format is supported by most operating systems and programming languages. The download for the DateTime library includes a Processing sketch that gets the time from your computer and sends this to the Arduino sketch.
The header is there to provide a way for the sketch to recognize the start of the time packet.
I have some simple code that I use to fake a clock for checking displays & stuff.
I put in the time I want it to start at, download it, and then 2 seconds before I reach the time I start the serial monitor.
Tracks the official US time from the internet really well. http://www.time.gov/timezone.cgi?Eastern/d/-5/java
Could easily add reading some buttons for setting the time if one wanted to take it further.
unsigned long currentmicros = 0;
unsigned long nextmicros = 0;
unsigned long interval = 10000;
byte tens_hours = 1;
byte ones_hours = 8;
byte tens_minutes = 5;
byte ones_minutes = 4;
byte tens_seconds = 3;
byte ones_seconds = 0;
byte tenths = 0;
byte hundredths= 0;
byte prior_seconds = 0;
void setup()
{
Serial.begin(57600);
nextmicros = micros();
}
void loop()
{
currentmicros = micros(); // read the time.
if ((currentmicros - nextmicros) >= interval) // 10 milliseconds have gone by
{
hundredths = hundredths +1;
if (hundredths == 10){
hundredths = 0;
tenths = tenths +1;
}
if (tenths == 10){
tenths = 0;
ones_seconds = ones_seconds +1;
}
if (ones_seconds == 10){
ones_seconds = 0;
tens_seconds = tens_seconds +1;
}
if (tens_seconds == 6){
tens_seconds = 0;
ones_minutes = ones_minutes +1;
}
if (ones_minutes == 10){
ones_minutes = 0;
tens_minutes = tens_minutes +1;
}
if (tens_minutes == 6){
tens_minutes = 0;
ones_hours = ones_hours +1;
}
if (ones_hours == 10){
ones_hours = 0;
tens_hours = tens_hours +1;
}
if ((tens_hours == 2) && (ones_hours == 4)){
ones_hours = 0;
tens_hours = 0;
}
nextmicros = nextmicros + interval; // update for the next comparison
} // end time interval check
// counters are all updated now, send to display
if (prior_seconds != ones_seconds){
Serial.print (tens_hours, DEC);
Serial.print (" ");
Serial.print (ones_hours, DEC);
Serial.print (" : ");
Serial.print (tens_minutes, DEC);
Serial.print (" ");
Serial.print (ones_minutes, DEC);
Serial.print (" : ");
Serial.print (tens_seconds, DEC);
Serial.print (" ");
Serial.println (ones_seconds, DEC);
prior_seconds = ones_seconds; // show time update once/second
} // end one second passing check
// do other stuff in the meantime ...
} // end void loop
Sorry if I'm new to this, I've tried both codes, the first gives me errors, where as the second gives me a blank display, I was thinkings of doing something more along these lines, I know it wont be accurate without the realtime clock, but I think i can make it close enough
totaltime=millis()/1000;
mins
hours
sec = totaltime %60;
//math goes here display HH:MIN:SEC
C:\Users\Mark\Documents\Arduino\libraries\DateTime\DateTime.cpp: In member function 'void DateTimeClass::setTime(time_t)':
C:\Users\Mark\Documents\Arduino\libraries\DateTime\DateTime.cpp:28: error: 'millis' was not declared in this scope
C:\Users\Mark\Documents\Arduino\libraries\DateTime\DateTime.cpp: In member function 'time_t DateTimeClass::now()':
C:\Users\Mark\Documents\Arduino\libraries\DateTime\DateTime.cpp:43: error: 'millis' was not declared in this scope
Okay, Ive gotten my code to work now, I''m sorry if I'm reposting, but does anyone know how to get the keypad buttons to adjust the time that way?, E.g pressing left button to adjust hour, right button to adjust minute, and top button to adjust second?
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
//Intro code
lcd.print("Loading...");
delay(1000);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//Setting LCD to second line
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("Dec, 04, 2012");//Insert Date
}
// Set Variables
int x=0;
int a=0;
int y=0;
int z=0;
int initialHours = 07;//defines the hour you want
int initialMins = 53;//defines minutes
int initialSecs = 00;//defines seconds
int secspassed()
{
x = initialHours*3600;
x = x+(initialMins*60);
x = x+initialSecs;
x = x+(millis()/1000);
return x;
}
int hours()
{
y = secspassed();
y = y/3600;
y = y%24;
return y;
}
int mins()
{
z = secspassed();
z = z/60;
z = z%60;
return z;
}
int secs()
{
a = secspassed();
a = a%60;
return a;
}
void loop(){
digitalClockDisplay();
}
void printDigits(byte digits){
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
char sep()
{
x = millis()/1000;
if(x%2==0)
{
lcd.print(":");
}
else {
lcd.print(" ");
}
}
void digitalClockDisplay(){
lcd.setCursor(0,1);
printDigits(
hours());
sep();
printDigits(mins());
sep();
printDigits(secs());
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
What keypad, you mean the Keyboard or your LCD has built-"on" buttons?
If it is the keyboard then, look them up in an Ascii table. If they are built in then it should say what they are on the print the LCD came with.
I've been running my simple clock code for almost 48 hours now, am seeing less than 2 seconds difference from official US time.
So less than a second a day, 6 minutes a year, guess that's not too bad.