Hallo Forengemeinde
Ich habe vor einiger Zeit hier nach einer Lösung mit Timerregistern gesucht und von euch erhalten. Der Sketch ist seither in zwei weiteren Projekten verwendet worden und läuft dort einwandfrei. Einzig Werte der LED Ausgänge wurden angepasst, um andere Farben am Taster zu erhalten, ansonsten unverändert.
Jetzt will ich den Sketch ein weiteres mal nutzen, jedoch in einem neuen Projekt.
Komischerweise springt der "button_vent_count" - Wert so nach ca. 15- 25 Sekunden auf 1, auch wenn der Taster nicht betätigt wird. Ansonsten funktioniert alles wie bisher und wie gewünscht. Bauteile inklusive Platine sind dieselben, wie in den anderen Projekten.
Anpassungen des debounce delay's bringen keine Besserung, da ich zuerst dachte, da sei mit dem Signal was nicht in Ordnung.
Ich nehme an, an der Leitungslänge des Flachbandkabels (ca 1m) vom Taster zum Arduino liegt es nicht, da in den funktionierenden Aufbauten diese länger ist, und ich ja das Signal gegen GND ziehe, um einen Schaltvorgang auszulösen.
Delay(10) und Serial print im Loop nur zu Kontrollzwecken eingefügt.
Was könnte sonst noch Ursache sein, vielleicht hat jemand eine Idee...
Danke Euch
Gruss ArduBastler
Code:
/*
Security Timer set to 5min
Controller: OriginalArduino NanoR3 (ATmega328P)
Button 22mm RGB (PM221-11E) w. Resistors, momentary, to GND
*/
//variables:
#define x 80 // high value venting (0-98)
#define y 50 // mid value venting (0-98)
#define z 30 // low value venting (0-98)
int sec_time = 5; // security timer (minutes)
//--------------------------------------------------------------
//constants, do not change
const byte button_vent = 7; // venting
const byte pwmv = 9; // pwm pin venting
const byte vred = 3; // red button LED venting
const byte vgreen = 5; // green button LED venting
const byte vblue = 6; // blue button LED venting
#define debounce_delay 200 // debounce time (ms)
//--------------------------------------------------------------
//program variables
bool button_vent_value;
bool button_vent_value_old;
bool timerv4; //timer states for venting
bool timerv5;
bool timerv6;
unsigned long timev4; //timer values
unsigned long timev5;
unsigned long timev6;
unsigned long secura = (sec_time * 60000); // security time calculation to minutes orig:(sec_time * 60 * 1000)
const unsigned int TOP = 99; //
unsigned int DutyCycle = 0; // 0% Pulsweite, OCR1A <= ICR1 !!!
uint8_t button_vent_count;
const byte pin_PWM = 9;
//--------------------------------------------------------------
//program
void setup()
{
Serial.begin(9600);
//sets pins as input/output
pinMode (button_vent, INPUT_PULLUP); // internal pullup resistor, button to GND
pinMode (vblue, OUTPUT); // blue led output to button
pinMode (vgreen, OUTPUT); // green led output to button
pinMode (vred, OUTPUT); // red led output to button
pinMode(pin_PWM, OUTPUT); // pwm output to n-channel mosfet
int randomSeed(analogRead(0));
set_Timer1();
set_pwm_DutyCycle (DutyCycle);
}
//--------------------------------------------------------------
void loop()
{
delay(10);
Serial.println("button_vent_count start Loop");
Serial.println(button_vent_count);
//button venting check
static uint32_t debounce_time;
if (millis() - debounce_time > debounce_delay)
delay(20);
{ button_vent_value = digitalRead(button_vent);
if (button_vent_value != button_vent_value_old)
{ debounce_time = millis();
button_vent_value_old = button_vent_value;
if (!button_vent_value)
{ button_vent_count++;
if (button_vent_count > 3) button_vent_count = 0;
}
}
}
if (button_vent_count == 0) venting_off();
if (button_vent_count == 1) venting_high();
if (button_vent_count == 2) venting_mid();
if (button_vent_count == 3) venting_low();
delay(10);
Serial.println("button_vent_count End Loop");
Serial.println(button_vent_count);
}
//--------------------------------------------------------------
void venting_off() //off, no color
{
analogWrite(vred, 25);
analogWrite(vgreen, 25);
analogWrite(vblue, 25);
set_pwm_DutyCycle (DutyCycle = 0);
timerv4 = LOW;
timerv5 = LOW;
timerv6 = LOW;
}
//--------------------------------------------------------------
void venting_low() // color green
{
if (timerv4 == LOW) {
timerv4 = HIGH;
timev4 = millis();
set_pwm_DutyCycle (DutyCycle = z);
analogWrite(vred, 0);
analogWrite(vgreen, 35);
analogWrite(vblue, 0);
}
else {
if (millis() - timev4 > secura) {
button_vent_count = 0;
timerv4 = LOW;
venting_off();
}
}
}
//--------------------------------------------------------------
void venting_mid() // color light blue
{
if (timerv5 == LOW) {
timerv5 = HIGH;
timev5 = millis();
set_pwm_DutyCycle (DutyCycle = y);
analogWrite(vred, 0);
analogWrite(vgreen, 70);
analogWrite(vblue, 70);
}
else {
if (millis() - timev5 > secura) {
button_vent_count = 0;
timerv5 = LOW;
venting_off();
}
}
}
//--------------------------------------------------------------
void venting_high() // color blue
{
if (timerv6 == LOW) {
timerv6 = HIGH;
timev6 = millis();
set_pwm_DutyCycle (DutyCycle = x);
analogWrite(vred, 0);
analogWrite(vgreen, 0);
analogWrite(vblue, 150);
}
else {
if (millis() - timev6 > secura) {
button_vent_count = 0;
timerv6 = LOW;
venting_off();
}
}
}
//--------------------------------------------------------------
void set_pwm_DutyCycle (unsigned int duty)
{
if (duty <= TOP) { // simple plausibility check
OCR1A = duty;
}
}
//--------------------------------------------------------------
void set_Timer1()
{
cli(); // interrupts off
TCCR1B = 0; // Reset, stop Timer
TCCR1A = 0; // Reset
TCNT1 = 0; // Reset
TIMSK1 = 0; // Reset
ICR1 = TOP;
OCR1A = DutyCycle; // pulse width, OCR1A <= ICR1
TCCR1A = (1 << COM1A1);
TCCR1B = (1 << WGM13) | (1 << CS12) | (1 << CS10); // Prescaler 1024
sei(); // interrupts on
}
//----END OF FILE----------------------------------------------------------