Seid gegrüsst!
Ich hatte vor einiger Zeit einen Topic gestartet (Problem mit PWM Frequenz und Chrono)
https://forum.arduino.cc/index.php?topic=600839.0, dieses Problem konnte ich mit Hilfe eures Wissens lösen!
Jetzt wollte ich den Sketch umschreiben, für einen Nano R3, nur den Teil mit dem venting wird gebraucht.
Was soll passieren:
Taster steuert PWM Ausgang mit 79 HZ Frequenz und variablem Zyklus (die 79Hz kommen von der anzusteuernden HW, siehe ersten Topic)
RGB Led gibt je nach Status Farben aus.
Das funzt soweit, nur bekomme ich an Pin 11 kein Signal… sollte doch aber eigentlich, die ersten 3 Timerregister des 328 sind doch gleich wie beim Mega… so zumindest wird es nach Suchen von Tante Google behauptet…
stehe auf’m Schlauch…
Code:
//Code:
//variables:
#define x 98 // high value venting (0-98)
#define y 53 // mid value venting (0-98)
#define z 25 // low value venting (0-98)
int sec_time = 5; // security timer (minutes)
//--------------------------------------------------------------
//constants, do not change
const byte button_vent = 1; // venting
const byte pwmv = 11; // 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 20 // 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 = 11;
//--------------------------------------------------------------
//program
void setup() { //sets pins as input/output
pinMode (button_vent, INPUT_PULLUP); //internal pullup resistor, switch to GND
pinMode (vblue, OUTPUT);
pinMode (vgreen, OUTPUT);
pinMode (vred, OUTPUT);
pinMode(pin_PWM, OUTPUT);
int randomSeed(analogRead(0));
set_Timer1();
set_pwm_DutyCycle (DutyCycle);
}
void loop()
{ //button venting check
static uint32_t debounce_time;
if (millis() - debounce_time > debounce_delay)
{ 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();
}
void venting_off() //off, white dimmed
{
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
}
Heat_Vent_R3.ino (7.16 KB)