Arduino Uno R3 / Küchenumbau für Weihnachten

Hallo,

ich habe nach dieser Anleitung

die Küche umgebaut, die mein Sohn zu Weihnachten bekommen soll.
Mein einziges Manko ist das ich nicht verstehe warum die Uhrzeit nicht von meinem Rechner übernommen wird.
Habe versucht mich über diese Sketch und Bibiliotheken Dinge zu belesen, ich verstehe es einfach nur nicht.

#include <DS1307RTC.h>
#include <TimeLib.h>
#include <TM1637Display.h>
#include <Wire.h>

/****************************************************************
 *                                                              *
 *  IKEA DUKTIG clock/timer - Roald Hendriks, January 2018      *
 *                                                              *
 *  Hardware: Arduino Uno R3 / Arduino Nano                     *
 *            LEDstrips (in oven and over counter)              *
 *            DS3231 SDA analog pin4, SCL analog pin5           *
 *            TM1637 4 digit LED Display                        *
 *            4 buttons (+, -, start, stop)                     *
 *            Buzzer                                            *
 *                                                              *
 ****************************************************************

 Revision History
  
    Date  	By	What
  20180124      RH      v1.0 Final version, data cleanup for instructable

 */

// Segments for showing BEEP
const uint8_t SEG_BEEP[] = {
	SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, // B
	SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,                 // E
	SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,                 // E
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,                 // P
	};


// Definition of all digital pins

// ***** Button input pins *****
// + Button
const int PlusButtonPin = 13;

// - Button
const int MinusButtonPin = 12;

// Start Button
const int StartButtonPin = 11;

// Stop Button
const int StopButtonPin = 10;

// ***** Output pins *****
// Buzzer
const int BuzzerPin = 4;

// LED-strip in oven
const int OvenLEDPin = 5;

// TM1637 clock pin
const int TM1637CLKPin = 8;

// TM1637 data pin
const int TM1637DataPin = 7;



// PWM LED brightness
const int intLEDBrightness = 200;

// Create TM1637 object
TM1637Display display(TM1637CLKPin,TM1637DataPin);

// Create RTC time object
tmElements_t RTC_time;

// Create RTC timer objects to hold the timer
// prevTimes is used to determine whether the timer has changed
tmElements_t Timer, prevTimer;

int PlusButtonState        = LOW;    // the current  reading from the plus  input pin
int lastPlusButtonState    = LOW;    // the previous reading from the plus  input pin
int MinusButtonState       = LOW;    // the current  reading from the minus input pin
int lastMinusButtonState   = LOW;    // the previous reading from the minus input pin
int StartButtonState       = LOW;    // the current  reading from the start input pin
int lastStartButtonState   = LOW;    // the previous reading from the start input pin
int StopButtonState        = LOW;    // the current  reading from the stop  input pin
int lastStopButtonState    = LOW;    // the previous reading from the stop  input pin

long lastPlusDebounceTime  =   0;    // the last time the plus  output pin was toggled
long lastMinusDebounceTime =   0;    // the last time the minus output pin was toggled
long lastStartDebounceTime =   0;    // the last time the start output pin was toggled
long lastStopDebounceTime  =   0;    // the last time the stop  output pin was toggled

long debounceDelay         =  50;    // the debounce time in milliseconds

int intState;                         // current  state

// All states
#define ShowTime  1
#define SetTimer  2
#define CountDown 3
#define Beeping   4

#define On  HIGH
#define Off LOW

time_t systime; // holds current time for diff calculation

// While setting the timer, a list with gradually increasing steps will be used
// The numbers represent seconds
int TimerSet[] = {5, 10, 15, 20, 25, 30, 40, 50, 60, 75, 90, 105, 120, 135, 150, 165, 180, 210, 240, 270, 300, 330, 360, 420, 480, 540, 600};

// For colon
uint8_t segto;

void setup()
// Mandatory setup function
{   
  // Enable serial monitoring
  Serial.begin(19200);
  
  // Sync to DS3231
  setSyncProvider(RTC.get);
  setSyncInterval(10);
  
  // Send time to serial
  RTC.read(RTC_time);
  OutputTimeToSerial();
  
  // Set Timer to 00:00
  Timer.Minute = 0;
  Timer.Second = 0;
  // Set previous timer to timer
  prevTimer = Timer;
  
  // Initialize the button pins as inputs:
  pinMode(PlusButtonPin,  INPUT_PULLUP); 
  pinMode(MinusButtonPin, INPUT_PULLUP); 
  pinMode(StartButtonPin, INPUT_PULLUP); 
  pinMode(StopButtonPin,  INPUT_PULLUP);
  
  // Initialize the output pins:
  pinMode(BuzzerPin,  OUTPUT);
  pinMode(OvenLEDPin, OUTPUT);
  
  // Set initial mode
  intState = ShowTime;
  
  // Set default brightness to 10 on a scale of 15
  display.setBrightness(0x0a);
}

void Beep(void)
// Make a beeping sound
{ // Show BEEP in display
  display.setSegments(SEG_BEEP);
  // Beep on for 500 milliseconds
  digitalWrite(BuzzerPin,HIGH);
  delay(500);
  // Beep off for 500 milliseconds
  digitalWrite(BuzzerPin,LOW);
  delay(500);
  // Beep on for 500 milliseconds
  digitalWrite(BuzzerPin,HIGH);
  delay(500);
  // Beep off for 500 milliseconds
  digitalWrite(BuzzerPin,LOW);
  delay(500);
  // Beep on for 1 second
  digitalWrite(BuzzerPin,HIGH);
  delay(1000);
  // Beep off
  digitalWrite(BuzzerPin,LOW);
  // After the beep, show time again
  intState = ShowTime;
}

void CountdownTimer()
// Decrease timer by 1 second
{
  int intSeconds;
  // Convert current timer time to seconds
  intSeconds = Timer.Minute * 60 + Timer.Second;
  
  // Decrease by 1
  intSeconds--;
  
  // Update timer
  Timer.Minute = intSeconds / 60;
  Timer.Second = intSeconds % 60;
}

void DecreaseTimer()
// Decrease timer using standard list
{ int intSeconds, intIndex;
  // Convert current timer time to seconds
  intSeconds = Timer.Minute * 60 + Timer.Second;

  // Loop through standard list and find the first value larger than the current number of seconds
  for (int i=26; i >= 0; i--){
      // Check whether list value is larger than current timer setting
      if(TimerSet[i] < intSeconds)
        { // Set Timer, minutes is simply dividing by 60, seconds are the rest after division
          Timer.Minute = TimerSet[i] / 60;
          Timer.Second = TimerSet[i] % 60;
          // Break out of for loop
          break;
        }
   } 
}

void IncreaseTimer()
// Increase timer using standard list
{ int intSeconds, intIndex;
  // Convert current timer time to seconds
  intSeconds = Timer.Minute * 60 + Timer.Second;

  // Loop through standard list and find the first value smaller than the current number of seconds
  for (int i=0; i <= 26; i++){
      // Check whether list value is smaller than current timer setting
      if(TimerSet[i] > intSeconds)
        { // Set Timer, minutes is simply dividing by 60, seconds are the rest after division
          Timer.Minute = TimerSet[i] / 60;
          Timer.Second = TimerSet[i] % 60;
          // Break out of for loop
          break;
        }
   } 
}

void OutputTimeToSerial()
// Send time to Serial-output
{
  Serial.print("Clock: ");
  Serial.print(RTC_time.Hour);
  Serial.print(":");
  Serial.print(RTC_time.Minute);
  Serial.print(":");
  Serial.println(RTC_time.Second);
}

void OutputTimerToSerial()
// Send timer to Serial-output
{
  Serial.print("Timer: ");
  Serial.print(Timer.Hour);
  Serial.print(":");
  Serial.print(Timer.Minute);
  Serial.print(":");
  Serial.println(Timer.Second);
}

void SetLight(int LEDPin, boolean State)
// Set LED strip to On or Off (state HIGH or LOW)
{
  if (State==Off) {
    digitalWrite(LEDPin, State);
  }
  else {
    // For switching on use a 'smooth' approach
    for (int i=0; i <= intLEDBrightness; i++) {
      analogWrite(LEDPin, i);
      delay(3);
    }
  }
}

 
void ShowTimeOnDisplay(void)
// Show current time on the display
{ // Convert time to an integer
  RTC.read(RTC_time);
  int intNumber;
  intNumber = RTC_time.Hour * 100 + RTC_time.Minute;
  display.showNumberDec(intNumber,true);
  // For blinking the colon:
  // show colon for even seconds
  // hide colon for odd seconds
  if ((RTC_time.Second%2) == 0) {
    segto = 0x80 | display.encodeDigit((intNumber/100)%10);
    display.setSegments(&segto, 1, 1);
  }
}

void ShowTimerOnDisplay(void)
// Show timer on the display
{ // Convert timer to an integer
  int intNumber;
  intNumber = Timer.Minute * 100 + Timer.Second;
  // Show on display
  display.showNumberDec(intNumber,true);
  // Show colon
  segto = 0x80 | display.encodeDigit((intNumber/100)%10);
  display.setSegments(&segto, 1, 1);
}

void loop(void)
// The mandatory loop
{
  
  // read the state of the minus switch into a local variable:
  int readMinusPin = digitalRead(MinusButtonPin);
  // read the state of the plus  switch into a local variable:
  int readPlusPin  = digitalRead(PlusButtonPin);
  // read the state of the start switch into a local variable:
  int readStartPin = digitalRead(StartButtonPin);
  // read the state of the stop  switch into a local variable:
  int readStopPin  = digitalRead(StopButtonPin);
  
  // If the plus switch changed, due to noise or pressing:
  if (readPlusPin != lastPlusButtonState) {
    // reset the debouncing timer
    lastPlusDebounceTime = millis();
  }

  // If the minus switch changed, due to noise or pressing:
  if (readMinusPin != lastMinusButtonState) {
    // reset the debouncing timer
    lastMinusDebounceTime = millis();
  }

  // If the start switch changed, due to noise or pressing:
  if (readStartPin != lastStartButtonState) {
    // reset the debouncing timer
    lastStartDebounceTime = millis();
  }

  // If the stop switch changed, due to noise or pressing:
  if (readStopPin != lastStopButtonState) {
    // reset the debouncing timer
    lastStopDebounceTime = millis();
  }

    if ((millis() - lastPlusDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
      if (readPlusPin == LOW){
        switch(intState) {
          case ShowTime:
           // Change mode to SetTimer
           intState = SetTimer;
           delay(200);
           // Increase timer (from 00:00 to 00:05)
           IncreaseTimer();
           // Show timer
           ShowTimerOnDisplay();           
           break;
          case SetTimer:
           delay(200);
           // Increase the timer to smallest standard setting larger than current timer
           IncreaseTimer();
           break;
          case CountDown:
           delay(200);
           // Increase the timer to smallest standard setting larger than current timer
           IncreaseTimer();
           break;
        };
      }
    }
    
     if ((millis() - lastMinusDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
      if (readMinusPin == LOW){
        switch(intState) {
          case ShowTime:
           break;
          case SetTimer:
           delay(200);
           // Increase the timer to largest standard setting smaller than current timer
           DecreaseTimer();
           break;
          case CountDown:
           delay(200);
           // Increase the timer to largest standard setting smaller than current timer
           DecreaseTimer();
           break;
        };
      }
    }
 

    if ((millis() - lastStartDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
      if (readStartPin == LOW){
        switch(intState) {
          case ShowTime:
           // Ignore
           break;
          case SetTimer:
           // Start countdown
           intState = CountDown;
           // Oven light on
           SetLight(OvenLEDPin, On);
           break;
          case CountDown:
           // Ignore
           break;
        };
      }
    }
    
     if ((millis() - lastStopDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (readStopPin != StopButtonState) {
      StopButtonState = readStopPin;

      if (readStopPin == LOW){
        switch(intState) {
          case ShowTime:
           // Ignore
           break;
          case SetTimer:
           // Show time
           intState = ShowTime;
           // Reset timer
           Timer.Minute = 0;
           Timer.Second = 0;
           break;
          case CountDown:
           // Show time
           intState = ShowTime;
           // Reset timer
           Timer.Minute = 0;
           Timer.Second = 0;
        };
      };
    };
    }


  // Based on current state, do ...
  switch(intState) {
    case ShowTime:
     // ShowTime on Display, but only when the time has changed
     if (systime != now()) { // wait for new second to do anything
        // Update systime
        systime = now();
        // ShowTime on Display
        ShowTimeOnDisplay();
     }
     // Oven light off
     SetLight(OvenLEDPin, Off);
     break;
    case SetTimer:
     // Only update display when the timer has changed
     if (Timer.Minute != prevTimer.Minute || Timer.Second != prevTimer.Second) {
       prevTimer = Timer;
       // ShowTimer on Display
       ShowTimerOnDisplay();
     }
     // Oven light off
     SetLight(OvenLEDPin, Off);
     break;
    case CountDown:
     // Correct Timer, but only when the time has changed
     if (systime != now()) { // wait for new second to do anything
        systime = now();
        // ShowTimer on Display
        ShowTimerOnDisplay();
        // And of course, count down the timer
        CountdownTimer();
     }
     if (Timer.Minute == 0 && Timer.Second == 0) {
       // Delay so 00:01 is shown
       delay(900);
       intState = Beeping;
     }
     
     break;
    case Beeping:
     // Well, BEEP!!!
     Beep();
     break;
  }

  lastPlusButtonState  = readPlusPin;
  lastMinusButtonState = readMinusPin;
  lastStartButtonState = readStartPin;
  lastStopButtonState  = readStopPin;

}

Das ist das was drauf geladen ist, ich musste ganz am Anfang schon die Zeile Time.h in TimeLib.h ändern sonst hatte er schon geschimpft.

Bissel viel und sehr kurzfristig ich weiss, aber vllt. erbahrmt sich jemand und hilft mir (:slight_smile:

Vielen Dank

Hallo,
lade mal das Beispiel Simple auf den UNO darin wird die Uhrzeit vom PC verwendet.
Musst Du nur einmal machen
Welches RTC hast Du denn

im Kommentar steht was von DS3231
Lib hast Du eine andere verwendet

Du kannst auch die Zeile mit ins Setup nehmen

  // Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

Heinz

Hallo,

ich habe den DS3231 RTC genau.
Wie ich habe eine andere Lib verwendet? liegt darin der Fehler?

Wenn ich das Bsp Simple lade, über die Obverfläche "Neue Skizze importieren" oder mit Rechtsklick auf das Programm "Datein in Sketch importieren"? Da kommt sehr viel an Vorschlägen wenn ich bei dem Reiter rechts auf "Beispiele" gehe und dort nach Simple suche.

Die Zeile mit aufnehmen, wäre egal wo die in der Progammierung steht?

Vielen Dank

Hallo,
ich kenne die Lib nicht die Du verwendet hast . Kann sein das die kompatibel ist. Schau Dir mal die Beispiele an wie man die Uhr setzen kann eventuell geht das ja mit der Zeile. Ansonsten gibt es sicher sowas wie clock.set().

In der IDE Datei Beispiele dann findest Du in der Liste die Ordner für die Libs. Das wird es bei Dir einen geben für DS1307RTC. wenn du den auswählst werden die Beispiele angezeigt.

Wenn die Lib nicht kompatibel ist kannst Du die nicht einfach austauschen, kann sein das Dein Sketch dann nicht mehr läuft und der compiler bockt.
Heinz

Nachtrag es wird hier im Forum aber sicher einen geben der die Lib kennt

Ich versteh halt kaum was du schreibst :confused: Hatte große Hoffnung das es wirklich alles soweit fertig ist,das ich es nur "aufspielen" muss :confused:

Du kannst auch die Zeile mit ins Setup nehmen

  // Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

Das da, wäre egal wo es steht an welcher Stelle oder?

Hallo,

ich nehme mal an direkt hinter das Seriell.begin()

Bei mir sieht das leider anders aus,da ich den Webeditor nutze.

Da kommen bei mir 6 Vorschläge:

DS3231_osciillar_test

DS3231_set

DS3231_test

echo_time

jetzt

set_echo

Habe es direkt darunter gesetzt da kommt dann "Time.h Keine solche Datei oder Verzeichnis"

Dann helf ich mal.
Du benutzt die Lib von Paul Stoffregen.
Die Timelib greift auf dioe DS1307 lib von ihm zurück.

Es gibt für die Uhr einen fertigen Sketch:

Der sollte auch lokal in den examples sein.

Diesen Sketch einmal laden und ausführen, dann ist die Uhr fertig mit dem Datum / Zeit der Complierung gefüttert.

Hallo,

also meinst du das was dort steht alles kopieren, in der "Hauptskizze" rechts dann eine neue "Registerkarte" hinzufügen und es dort reinkopieren, verifizieren lassen und dann aufs Arduino laden?

Oder muss da eine neue Skizze erstellt werden und das dann nacheinander hochladen? Verstehe hier eh nicht was womit gemeint ist, ob Skizze der "Hauptordner" ist oder so, keine Ahnung.

Bitte verzeiht meine Fragerei und Unwissenheit.

Den folgenden Code

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

mit dem kopieren-Button


oben rechts am Codefenster hier raus kopieren dann in der IDE ein neues fenster aufmachen,
dort auf bearbeiten, alles markieren, dann auf einfügen
Dann einmal hochladen.
Uhr gestellt.

Habe nun eine neue Registerkarte aufgemacht und es eingefügt, nun kommt der selbe Fehler mit der Time.h... beim überprüfen oder ist das egal und ich lade es einfach so hoch?

NEIN!
Keine Registerkarte!

DATEI - NEU

Verzeihung, aber nun nochmal ich habe kein "Datei" Button zur Auswahl...

neue Skizze
(du musst schon sagen, das Du den webeditor benutzt. Vorausgesetzt wird eher die lokale Arduino-IDE)

Hatte ich oben schonmal geschrieben.

Habe das nun gemacht gehabt, dann blinkte aber die Doppelpunkte nichtmehr, dachte vllt den Strom trennen. Nun ist das Display dunkel.

Wusste nicht das hier Vorrausetzungen zu erfüllen sind, wenn man Hilfe benötigt.

Ich habe Hilfe gegeben - vollständig.
Damit ist das Thema für mich durch.

Danke dennoch und schöne Weihnachten.

Wenn jemand weiss warum die Uhr dann einfriert und was ich dann noch dagegen machen kann, damit das gelöste Problem auch wirken kann, wäre ich weiterhin dankbar und auch hier verschwunden.

Habe es nochmal komplett neu hochgeladen und diesen Zusatz von "my_xy_projekt"

Möchte eure Nerven nicht weiter strapazieren.

letzter Edit: Habe den Arduino nochmal zurückgesetzt und erst das Programm drauf geladen, dann diese zusätzliche Sache zum Uhren stellen, aber wieder bleibt die digitale Uhr stehen und es tut sich nichts mehr.
Ich denke ich habe eure Hilfe genauso umgesetzt, wenn auch mit großen Verständnisshindernissen, dennoch geht es nicht wirklich.

Das habe ich verstanden, nun mehrfach auch so gemacht und dennoch friert die Uhr ein und es geht nichts mehr von dem Programm (siehe Zeit einstellen oder Start/Stop etc.)

Die lokale Variante: Ja Geld ausgegeben um hier nicht als "Nutznießer" dazustehen und dennoch geht es nicht. Weder zu öffnen über "Datei-öffnen" noch sonst irgendwie.

Ich habe das Gefühl ihr denkt ich möchte euch ärgern, dem ist nicht so. Es ist fakt was ich schreibe, sollte dafür keine Lösung da sein ist es auch in Ordnung. Aber stellt mich nicht hin als würde ich euch mutwillig verarschen wollen.

Am Ende werde ich es wohl so machen, Nachts 0 Uhr aufstehen, die Batterie der RTC entfernen (dann ist ja 0 Uhr) wieder rein machen und dann stimmt die Zeit ja auch, mehr oder weniger.

Danke dennoch euch allen.

Nachtrag: Ich kann heute keine Beiträge mehr verfassen,daher muss ich es über das editieren hier machen.
Der Stand ist das auch in dem Monitor nichts außer komische Zeichen ( Vierecke auf der Spitze stehend mit einem Fragezeichen darin)angezeigt werden, ich aber grundsätzlich auf dem Display welches verbaut wurde die Zeit angezeigt haben möchte.

Nachtrag die 2te.

*@#23

Ja das ging schon die ganze Zeit, also ist bei mir auch durchgelaufen etc. aber eben die Zeit auf dem Display das verbaut ist hat sich nicht geändert und dort ist die Zeiut eingefroren.
Das mit dem umstellen des Codes auf 115200 im Monitor werde ich machen, wenn die Server wieder gehen.
Was ist den dann der folge Schritt, weil bis dahin war auch bei mir alles wie du es gesagt hast, bis auf den Code halt.

#24

Hallo Heinz, verstehe ich das richtig sollte dann in dem Monitor mit dem umgestellten Code die richtige Zeit angezeigt werden, lade ich das Hauptprogramm nochmal und dann geht die Uhr auf dem Display (was verbaut ist) richtig?*

In #10 hat my_xy_projekt Dir beschrieben wie Du den Code einfügst.
Ansonsten: wenn die Datei die Endung ".ino" hat, sollte es mit Doppelklick funktionieren.
Notfalls: Datei->Öffnen... Deine Datei auswählen

Hallo,
wenn Du die lokale IDE nutzt musst Du die Libs installieren sonst fehlt da sicher was. Aber es sollte mit dem webeditor ja auch gehen.

Du must halt nur einmal den SetTime auf den Ardoino laden und dann sollte die Zeit richtig angezeigt werden.

Nachtrag die wird auf dem Monitor angezeigt nicht auf dem Display

Dann nimmst Du wieder dein original Programm und lädst das .
Eventuell hast Du darin erwas ungewollt geändert , lade dir noch mal das Original herunter.

Heinz