I'm having trouble writing modifying this code that fades the LED on pin 13 on and off to do the exact same thing but without using any delays.
Here is the code which I currently have.
int ledPin = 13;
unsigned int i=0;
boolean fade=true;
int interval=1000;
void setup(){
pinMode(ledPin, OUTPUT);
}
void loop()
{
if(i == interval) {
i=1;
fade=!fade;
}
if(fade == false) {
digitalWrite(13, LOW);
delayMicroseconds(i);
digitalWrite(13, HIGH);
delayMicroseconds(interval-i);
}
if(fade == true) {
digitalWrite(13, LOW);
delayMicroseconds(interval-i);
digitalWrite(13, HIGH);
delayMicroseconds(i);
}
i+=1;
}
So far, I converted some of the code but I don't know how to make the Arduino wait without interrupting other code I plan to have running simultaneously.
Can please give me some ideas?
int ledState = LOW;
long previous = 0;
long interval = 0;
boolean fade = true;
void setup() {
pinMode(13, OUTPUT);
}
void loop()
{
unsigned long current = micros();
if(current - previous > interval) {
previous = current;
fade = !fade; //change fading
}
//fade out
if(fade == false) {
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
}
//fade in
if(fade == true) {
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
}
}