Hallo, ich versuche gerade meinen zweiten Code hinzubekommen... ich schaffs aber ums verrecken nicht....
Ich habe mir diesen Code aus verschiedenen zusammen genommen. Ich möchte (am Schluss) damit ein relais schalten für eine bestimmte Zeit.. Quasi eine einstellbare Eieruhr... ich bekomme aber einfach nicht hin das es mir richtig auf dem Display angezeigt wird. Würde sich jemand erbahmen und mir den Code umschreiben ? Und wenn ihr ganz gut drauf seit evtl. dazu erklären was ich falsch gemacht habe.
Im moment ist da denke ich ziemlich Chaos drin
Ich muss gestehen das ich mich noch nicht auskenne, aber ich möchte mich irgendwie auch nicht geschlagen geben.... Ich spiele da jetzt schon Tage rum und verliere ziemlich den Spass an der Geschichte.
Hoffe auf Hilfe von euch.
Danke Andy
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
int Variable1; // Create a variable to have something dynamic to show on the display
#define Start 4 // start stop button
int hours = 0;
int minutes = 0;
int seconds = 0;
boolean timeState = false;
#define encoderPinA 2 // right
#define encoderPinB 3 // left
#define encoderButton 5 // switch
//#define OLED_RESET LED_BUILTIN
int HMS = 1;
int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = false;
boolean B_set = false;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
delay(100); // This delay is needed to let the display to initialize
pinMode(Start, INPUT_PULLUP);
pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(encoderButton, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE); //pin 2
attachInterrupt(1, doEncoderB, CHANGE); //pin 3
}
void loop() {
Variable1++; // Increase variable by 1
if (Variable1 > 150) // If Variable1 is greater than 150
{
Variable1 = 0; // Set Variable1 to 0
}
// Convert Variable1 into a string, so we can change the text alignment to the right:
// It can be also used to add or remove decimal numbers.
char string[10]; // Create a character array of 10 characters
// Convert float to a string:
dtostrf(Variable1, 3, 0, string); // (<variable>,<amount of digits we are going to use>,<amount of decimal digits>,<string name>)
if (digitalRead(encoderButton) == LOW)
{
HMS = HMS + 1;
if (HMS == 4)
{
HMS = 1;
}
delay(1000);
}
rotating = true; // reset the debouncer
encoderPos = constrain(encoderPos, -1, 1);
if (lastReportedPos != encoderPos) {
if (HMS == 1) {
hours = hours + encoderPos;
hours = constrain(hours, 0, 48);
}
else if (HMS == 2) {
minutes = minutes + encoderPos;
minutes = constrain(minutes, 0, 60);
}
else if (HMS == 3) {
seconds = seconds + encoderPos;
seconds = constrain(seconds, 0, 60);
}
if (hours <= 9)
{
display.print("0");
}
display.print(": ");
if (minutes <= 9)
{
display.print("0");
}
display.print(": ");
if (seconds <= 9)
{
display.print("0");
}
encoderPos = 0;
lastReportedPos = encoderPos;
//display.display();
display.setCursor(4, 1);
display.clearDisplay();
display.print("hours ");
display.println(hours);
display.print("minutes ");
display.println(minutes);
display.print("seconds ");
display.println(seconds);
display.println(" ");
display.display();
}
if (digitalRead(Start) == LOW) { //start count down timer
timeState = true;
delay(1000);
while (timeState == true) {
if (minutes == 0 && hours >= 1) {
minutes = 60;
hours = hours - 1;
display.setCursor(4, 1);
display.clearDisplay();
display.print("hours ");
display.println(hours);
display.print("minutes ");
display.println(minutes);
display.print("seconds ");
display.println(seconds);
display.println(" ");
display.display();
}
if (seconds == 0 && minutes >= 1) {
seconds = 60;
minutes = minutes - 1;
display.setCursor(4, 1);
display.clearDisplay();
display.print("hours ");
display.println(hours);
display.print("minutes ");
display.println(minutes);
display.print("seconds ");
display.println(seconds);
display.println(" ");
display.display();
}
else if (minutes == 0 && hours == 0 && seconds == 0) { //count down alarm
while (timeState == true) {
display.setCursor(4, 1);
display.clearDisplay();
display.print("hours ");
display.println(hours);
display.print("minutes ");
display.println(minutes);
display.print("seconds ");
display.println(seconds);
display.println(" ");
display.display();
tone(11, 600, 250);
delay(250);
tone(11, 800, 250);
delay(250);
if (digitalRead(Start) == LOW) { // turn alarm off
timeState = false;
seconds = 1; /////////////////////////////////////////////
display.setCursor(4, 1);
display.clearDisplay();
display.setCursor(0, 0);
display.print("COUNT DOWN TIMER");
display.setCursor(4, 20);
display.print("00: 00: 00");
display.display();
break;
}
}
}
delay(992); // delay for keping time master setting!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
seconds = seconds - 1;
display.setCursor(4, 40);
if (hours <= 9)
display.clearDisplay();
{
display.print("0");
display.print(hours);
display.display();
}
display.print(":");
display.display();
if (minutes <= 9)
{
display.print("0");
display.print(minutes);
display.display();
}
display.print(":");
display.display();
if (seconds <= 9)
{
display.print("0");
display.print(seconds);
display.display();
}
if (digitalRead(Start) == LOW) {
delay(1000);
timeState = false;
break;
}
}
}
}
// Interrupt on A changing state
void doEncoderA() {
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos = 1;
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state
void doEncoderB() {
if ( rotating ) delay (1);
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
encoderPos = -1;
rotating = false;
}
}