I am trying to get the LED to be dimmed up and down continously untill the button interrupts the display.
meaning that led1 will increase when led2 decreases and reverse, the problem is getting this done and beeing able to push a button and instantly interrupt the effect.
The delay command is therefor not a viable option.
I have two codes that i have been trying with.
this first is with Delay but i am unnable to interrupt the dimming at any given time
const int led = 9;
const int led2 = 10;
void setup(){
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop(){
for(int verdi = 0; verdi <= 255; verdi = verdi ++){
analogWrite(led, verdi);
analogWrite(led2, 255 - verdi);
delay(5);
}
for(int verdi = 255; verdi >= 0; verdi = verdi --){
analogWrite(led, verdi);
analogWrite(led2, 255 - verdi);
delay(5);
}
}
This second code is the modified one but i lack the dimming properties from the first code that i want.
const int led = 9;
const int led2 = 10;
const int button = 2;
int value, value2 ;
long time=0;
int periode = 1500;
int displace = 1000;
void setup(){
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
int butt = digitalRead(button);
if(butt == LOW)
{
time = millis();
value = 128+127*cos(2*PI/periode*time);
value2 = 128+127*cos(2*PI/periode*(displace-time));
analogWrite(led, value); // sets the value (range from 0 to 255)
analogWrite(led2, value2);
}
else{
digitalWrite(led, LOW);
digitalWrite(led2, LOW);
}
}