Musikhai:
Möchte den Arduino nämlich zu Temperaturerfassung verwenden und die Daten dann auf SD-Karte spreichern um sie später per Excel auszuwerten.
Eine Zeiteinstellfunktion ist dazu natürlich nicht falsch.
Aber damit der Arduino nach dem Einschalten schon "eine ziemlich genaue Zeit" bekommt, macht man es normalerweise so, daß wie bei einem PC eine kleine Echtzeituhrenplatine mit Pufferbatterie angeschlossen wird. Beim Starten holt sich der Arduino dann die aktuelle Uhrzeit aus dem Uhrenmodul.
/******************************************************************
* Clock and clock adjust demo by "jurs" for German Arduino Forum *
* Hardware required: keypad shield
* Additional library required: Time library must be installed
******************************************************************/
#include <LiquidCrystal.h>
#include <Time.h>
// Select the pins used on the LCD panel:
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Define some values used by the LCD panel and buttons:
enum keypadButtons {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP };
int readPadButtons()
// read the buttons from analog interface:
// returns the key currently pressed
// do not call this function directly from the sketch!
{
int adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
#define DEBOUNCETIME 10
int buttonPressed()
// this function returns true only once per single button pressed
// use this function in your program sketch
{
static boolean buttonFired;
static long buttonDownSince;
int State=readPadButtons();
if (State==btnNONE)
{ // this button is not pressed at this time
buttonFired=false;
buttonDownSince=0;
}
else if (buttonDownSince==0)
{ // button is pressed but DEBOUNCETIME did not yet started counting
buttonFired=false;
buttonDownSince=millis();
}
if (millis()-buttonDownSince>=DEBOUNCETIME && buttonFired==false)
{ // button is pressed and DEBOUNCETIME passed, so fire the button
buttonFired=true;
return(State); // return button
}
return(btnNONE); // no button fired this time
}
void setup()
{
Serial.begin(9600); // set serial interface to 9600 baud
lcd.begin(16, 2); // starts the LCD display (2 lines by 16 chars)
setTime(1357002000); // set some initial time
}
boolean inMenu; // global variable to hold the menu status
void updateDisplay()
// this function updates the display (time and menu status)
{
char time[10];
sprintf(time,"%02d:%02d:%02d",hour(),minute(),second());
lcd.setCursor(0,0);
lcd.print(time);
lcd.setCursor(0,1);
if (inMenu)
lcd.print(F("Zeit einstellen"));
else
lcd.print(F("Normalbetrieb "));
}
void checkForNewSecond()
// function will update the display once per second
{
static long lastsecond;
if (lastsecond!=now())
{
updateDisplay();
lastsecond=now();
}
}
void handleMenuButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
switch (button)
{
case btnRIGHT:
{
setTime(now()+3600);
break;
}
case btnLEFT:
{
setTime(now()-3600);
break;
}
case btnUP:
{
setTime(now()+60);
break;
}
case btnDOWN:
{
setTime(now()-60);
break;
}
}
}
void handleNormalButton(int button)
{
// nothing yet
}
void handleButtons()
// Abfrage ob eine Taste gedrückt wurde
// Umschaltung des Menüstatus
// Falls im Zeiteinstellmenü, Funktion zur Behandlung der Tasten im Einstellmenü aufrufen
// Falls im Normalbetrieb, Funktion zur Behandlung der Tasten im Normalbetrieb aufrufen
{
int curButton;
curButton=buttonPressed();
if (curButton==btnNONE) return; // nothing to do if no button is pressed
else if (curButton==btnSELECT) inMenu=!inMenu; // switch menu status
else if (inMenu) handleMenuButton(curButton); // gedrückte Tasten im Einstellmenü behandeln
else handleNormalButton(curButton); // gedrückte Tasten sonst behandeln
updateDisplay();
}
void loop()
{
checkForNewSecond(); // einmal pro Sekunde das Display updaten
handleButtons();
}
Per "Select" Button wird zwischen Zeiteinstellung und Normalbetrieb umgeschaltet.
Reicht es Dir, nur die Zeit einstellen zu können und das Datum ist egal und kann irgendwann sein? Oder brauchst Du auch noch erweiterte Möglichkeiten, um nicht nur Stunden und Minuten, sondern auch noch Tag, Monat und Jahr einstellen zu können?