Ok ich hab mich mit dem ganzen relativ lang herumprobiert. Dabei gibt es jetzt noch folgende Probleme. In der Case A_PWM (siehe Programm), ist die Pulsweitenmodulation eingebaut. Da ist es so, dass nur ein Wert ausgelesen wird. Ich möchte aber die LED´s durchgehend regeln können, bis der zweite Taster betätigt wurde. Wie mache ich das?
2. nachdem alle LED´s runtergedimmt haben, soll der ganze Zyklus von neuem starten. Danke für eure Hilfe im Voraus.
const unsigned int UpTaster = 4; // Pin -- Taster -- GROUND
const unsigned int DownTaster = 2; // Pin -- Taster -- GROUND
const unsigned int dimSpeed = 50; // mS pro dimschritt
const int warm = A0; // Analog input pin that the sensor is attached to
const int kalt = A1; // Analog input pin that the sensor is attached to
const int staerke = A2; // Analog input pin that the sensor is attached to
const int analogOutwarm = 9; // Analog output pin that the LED is attached to
const int analogOutkalt = 10; // Analog output pin that the LED is attached to
const int analogOutstaerke = 3;
int wert1 = 0; // value read from the pot
int wert2 = 0;
int wert3 = 0;
int outputwarm = 0; // value output to the PWM (analog out)
int outputkalt = 0;
int outputstaerke = 0;
struct Automat
{
typedef enum AutomatenStatus {A_On, A_Off, A_DimUp, A_DimDown,A_PWM};
AutomatenStatus status;
unsigned long lastHit;
byte dim;
Automat() // Konstruktor
{
pinMode(UpTaster, INPUT_PULLUP);
pinMode(DownTaster, INPUT_PULLUP);
pinMode(warm, INPUT);
pinMode(kalt, INPUT);
lastHit = millis();
dim = 0;
status = A_Off;
analogWrite(analogOutwarm,dim);
analogWrite(analogOutkalt,dim);
}
void handle()
{
switch (status)
{
case A_Off:
if (!digitalRead(UpTaster)) // low = taster gedrückt
{
status = A_DimUp;
lastHit = millis();
}
break;
case A_DimUp:
if (millis() - lastHit > dimSpeed)
{
lastHit = millis();
dim++;
analogWrite(analogOutwarm,dim);
analogWrite(analogOutkalt,dim);
}
if (dim == 62)
{
status = A_PWM ;
}
break;
case A_PWM:
wert1 = analogRead(warm);
// map it to the range of the analog out:
outputwarm = map(wert1, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutwarm, outputwarm);
wert2 = analogRead(kalt);
outputkalt = map(wert2, 0, 1023, 0, 255);
analogWrite(analogOutkalt, outputkalt);
status = A_On;
break; //!!!!!!!
case A_On:
if (!digitalRead(DownTaster)) // low = taster gedrückt
{
status = A_DimDown;
lastHit = millis();
}
break;
case A_DimDown:
if (millis() - lastHit > dimSpeed)
{
Serial.print("A_DimDown: dim=");
Serial.println(dim);
lastHit = millis();
dim--;
//analogWrite(analogOutwarm,dim);
//analogWrite(analogOutkalt,dim);
Serial.print("A_DimDown: outputkalt=");
Serial.println(outputkalt);
analogWrite(analogOutwarm,outputwarm--);
analogWrite(analogOutkalt,outputkalt--);
if (outputwarm < 0) outputwarm=0;
if (outputkalt < 0) outputkalt=0;
if(dim == -194){
status = A_Off;
}
break;
}
}
}
};
Automat Dimmer;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Dimmer.handle();
wert3 = analogRead(staerke);
outputstaerke = map(wert3, 0, 1023, 0, 255);
analogWrite(analogOutstaerke, outputstaerke);
}