Hallo!
Nach ferienbedingter Abwesenheit habe ich Eure Tipps ausprobiert, der Code von Doc funktioniert perfekt, vielen Dank!
Nur mit dem timer hab ich immer noch ein kleines Problem..
Er springt mir nicht aus der Schleife zurück, aber warum, ich komm nicht drauf...
Welchen Denkfehler hab ich da eingebaut?
Hier der Code:
//variables:
#define x 50 // 1/3 Wert H/L 85
#define y 150 // 2/3 Wert H/L 170
#define z 255 // 3/3 Wert H/L 255
int Sicherheitszeit = 1; // Sicherheitstimer in Minuten
//--------------------------------------------------------------
//constants, do not change
#define taster1 0 // Lüftung
#define taster2 1 // Heizung
#define pwmh 2 // pwm pin Heizung
#define pwmv 11 // pwm pin Lüftung
#define hred 4 // rote Taster LED Heizung
#define hgreen 5 // grüne Taster LED Heizung
#define hblue 6 // blaue Taster LED Heizung
#define vred 7 // rote Taster LED Lüftung
#define vgreen 8 // grüne Taster LED Lüftung
#define vblue 9 // blaue Taster LED Lüftung
#define debounce_delay 20 // Prellzeit
//--------------------------------------------------------------
//program variables
bool taster1_wert;
bool taster1_wert_alt;
bool taster2_wert;
bool taster2_wert_alt;
uint8_t taster1_count;
uint8_t taster2_count;
unsigned long timeh1;
unsigned long timeh2;
unsigned long timeh3;
unsigned long timev4;
unsigned long timev5;
unsigned long timev6;
const unsigned int TOP = 99; //
unsigned int DutyCycle = 0; // 0% Pulsweite, OCR1A <= ICR1 !!!
const byte pin_PWM = 11;
unsigned long secura = (Sicherheitszeit * 1000);
int timerh1;
int timerh2;
int timerh3;
int timerv4;
int timerv5;
int timerv6;
//--------------------------------------------------------------
//program
void setup() {
pinMode (taster1, INPUT_PULLUP);
pinMode (taster2, INPUT_PULLUP);
pinMode (hblue, OUTPUT); // setzt den Pin 4 als Ausgang
pinMode (hgreen, OUTPUT); // setzt den Pin 5 als Ausgang
pinMode (hred, OUTPUT); // setzt den Pin 6 als Ausgang
pinMode (vblue, OUTPUT); // setzt den Pin 7 als Ausgang
pinMode (vgreen, OUTPUT); // setzt den Pin 8 als Ausgang
pinMode (vred, OUTPUT); // setzt den Pin 9 als Ausgang
pinMode(pin_PWM, OUTPUT);
int randomSeed(analogRead(0));
set_Timer1();
set_pwm_DutyCycle (DutyCycle);
timerh1 = LOW;
timerh2 = LOW;
timerh3 = LOW;
timerv4 = LOW;
timerv5 = LOW;
timerv6 = LOW;
}
void loop()
{
static uint32_t debounce_time;
if (millis() - debounce_time > debounce_delay)
{ taster1_wert = digitalRead(taster1);
if (taster1_wert != taster1_wert_alt)
{ debounce_time = millis();
taster1_wert_alt = taster1_wert;
if (!taster1_wert)
{ taster1_count++;
if (taster1_count > 3) taster1_count = 0;
}
}
}
if (taster1_count == 0) funktion0();
if (taster1_count == 1) funktion1();
if (taster1_count == 2) funktion2();
if (taster1_count == 3) funktion3();
if (millis() - debounce_time > debounce_delay)
{ taster2_wert = digitalRead(taster2);
if (taster2_wert != taster2_wert_alt)
{ debounce_time = millis();
taster2_wert_alt = taster2_wert;
if (!taster2_wert)
{ taster2_count++;
if (taster2_count > 3) taster2_count = 0;
}
}
}
if (taster2_count == 0) funktion4();
if (taster2_count == 1) funktion5();
if (taster2_count == 2) funktion6();
if (taster2_count == 3) funktion7();
}
void funktion0() //alles aus Heizung
{
analogWrite(pwmh, 0);
analogWrite(hred, 0);
analogWrite(hgreen, 0);
analogWrite(hblue, 0);
timerh1 = LOW;
timerh2 = LOW;
timerh3 = LOW;
}
void funktion1() // 1/3 Heizung, Farbe gelb
{
if (timerh1 == LOW) {
timerh1 = HIGH;
timeh1 = millis();
analogWrite(pwmh, x);
analogWrite(hred, 255);
analogWrite(hgreen, 255);
analogWrite(hblue, 0);
}
else {
(timerh1 == HIGH); {
if (millis() - timeh1 > secura) {
taster2_count = 0;
timerh1 = LOW;
funktion0();
}
}
}
}
void funktion2() // 2/3 Heizung, Farbe orange
{
if (timerh2 == LOW) {
timerh2 = HIGH;
timeh2 = millis();
analogWrite(pwmh, y);
analogWrite(hred, 255);
analogWrite(hgreen, 127);
analogWrite(hblue, 0);
}
else {
(timerh2 == HIGH); {
if (millis() - timeh2 > secura) {
taster2_count = 0;
timerh1 = LOW;
funktion0();
}
}
}
}
void funktion3() // 3/3 Heizung, Farbe rot
{
if (timerh3 == LOW) {
timerh3 = HIGH;
timeh3 = millis();
analogWrite(pwmh, z);
analogWrite(hred, 255);
analogWrite(hgreen, 0);
analogWrite(hblue, 0);
}
else {
(timerh3 == HIGH); {
if (millis() - timeh3 > secura) {
taster2_count = 0;
timerh2 = LOW;
funktion0();
}
}
}
}
void funktion4() //alles aus Lüftung
{
set_pwm_DutyCycle (DutyCycle = 0);
analogWrite(vred, 0);
analogWrite(vgreen, 0);
analogWrite(vblue, 0);
timerv4 = LOW;
timerv5 = LOW;
timerv6 = LOW;
}
void funktion5() // 1/3 Lüftung, Farbe aqua
{
set_pwm_DutyCycle (DutyCycle = x);
analogWrite(vred, 0);
analogWrite(vgreen, 255);
analogWrite(vblue, 255);
if (timerv4 == HIGH) {
if (millis() - timev4 > secura) {
taster2_count = 0;
funktion4();
timev4 = millis();
if (timerv4 == LOW) {
timerv4 = HIGH;
timev4 = millis();
}
}
}
}
void funktion6() // 2/3 Lüftung, Farbe blau
{
set_pwm_DutyCycle (DutyCycle = y);
analogWrite(vred, 0);
analogWrite(vgreen, 0);
analogWrite(vblue, 255);
if (timerv5 == HIGH) {
if (millis() - timev5 > secura) {
taster2_count = 0;
funktion4();
if (timerv5 == LOW) {
timerv5 = HIGH;
timerv4 = LOW;
timev5 = millis();
}
}
}
}
void funktion7() // 3/3 Lüftung, Farbe grün
{
set_pwm_DutyCycle (DutyCycle = z);
analogWrite(vred, 0);
analogWrite(vgreen, 255);
analogWrite(vblue, 0);
if (timerv6 == HIGH) {
if (millis() - timev5 > secura) {
taster2_count = 0;
funktion6();
if (timerv6 == LOW) {
timerv6 = HIGH;
timerv5 = LOW;
timev6 = millis();
}
}
}
}
void set_pwm_DutyCycle (unsigned int duty)
{
if (duty <= TOP) { // einfache Plausibilitätsabfrage
OCR1A = duty;
}
}
void set_Timer1()
{
cli(); // Interrupts ausschalten
TCCR1B = 0; // Reset, unter anderem Timer stoppen
TCCR1A = 0; // Reset
TCNT1 = 0; // Reset
TIMSK1 = 0; // Reset
ICR1 = TOP;
OCR1A = DutyCycle; // Pulsweite, OCR1A <= ICR1
TCCR1A = (1 << COM1A1);
TCCR1B = (1 << WGM13) | (1 << CS12) | (1 << CS10); // Prescaler 1024
sei(); // Interrupts einschalten
}