Moin ![]()
Ich komm so langsam mit meiner Beleuchtung voran, hier erstmal der aktuelle code:
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
int interval = 0; // interval at which to blink (milliseconds)
int brightness = 10; // how bright the LED is
#define led1 3
#define led2 5
#define led3 6
#define led4 9
#define fled1 10 //Led on digital pin 10
#define fled2 11 //Led on digital pin 11
//Fade a led without delaying the rest of the code
//START
unsigned long fadeMillis = 0;
boolean fadeUp = true;
int fadeValue = 0;
void analogFade()
{
if (millis() - fadeMillis >= 2)
{
fadeMillis = millis();
if (fadeUp == true)
{
if (fadeValue < 255)
{
fadeValue++;
}
else
{
fadeUp = false;
fadeValue--;
}
}
else
{
if (fadeValue > 0)
{
fadeValue--;
}
else
{
fadeUp = true;
fadeValue++;
}
}
analogWrite(fled1, fadeValue);
analogWrite(fled2, fadeValue);
}
}
//STOP
void normallight()
{
analogWrite(6, brightness);
analogWrite(9, brightness);
}
void frontflash()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = 1-ledState;
// set the LED with the ledState of the variable:
digitalWrite(led1, ledState);
digitalWrite(led2, ledState);
if (interval == 59)
interval = 100;
else if (interval == 100)
interval = 61;
else if (interval == 61)
interval = 800;
else
interval = 59;
}
}
void flashlight()
{
analogFade();
frontflash();
normallight();
}
void all_off()
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
analogWrite(6, LOW);
analogWrite(9, LOW);
analogWrite(fled1, LOW);
analogWrite(fled2, LOW);
}
volatile int state = LOW;
void setup()
{
attachInterrupt(0, IRQ, CHANGE); // both edges
Serial.begin(9600); //serial library start
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop()
{
}
volatile uint32_t time = 0;
void IRQ()
{
if (digitalRead(2) == HIGH) { time = micros(); return; } // set time at high edge
time = micros() - time; // check duration at low edge
if (time > 1000)
{
flashlight();
// do your thing here
} else
{
all_off();
}
Serial.println(time);
}
Wenn ich das Script ausführ und mit meiner RC-Fernsteuerung schalte braucht der Fade knappe 10sek und ich hab keine ahnung warum.
Kann man das irgendwie optimieren?
Link zum alten Thread: http://arduino.cc/forum/index.php/topic,66262.0.html