I am trying to create a simple sequence of events. First event-Blink an LED on LED_BUILTIN then Fade a separate LED on pwm pin 9. I need assistance in figuring this out and understanding how to embed two or more separate events within a sketch. Here is where I am now, LED blinks but doesn't stop for the other LED fading, they happen simultaneously. Where should I go to help me figure this out and do you have suggestions as to what is wrong and how I should be thinking about this.
/*
Blink then Fade
Blink an LED once and the fade a separate LED after it, then repeat.
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int ledPin = LED_BUILTIN; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 100; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT); // set the digital pin as output:
pinMode(led, OUTPUT);} // set the digital pin as output:
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time you blinked the LED
if (ledState == LOW) { // if the LED is off turn it on and vice-versa:
ledState = HIGH;
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState); // set the LED with the
ledState of the variable:
analogWrite(led, brightness); // set the brightness of pin 9:
brightness = brightness + fadeAmount;// change the brightness for next time through the loop:
if (brightness <= 0 || brightness >= 255) { // reverse the direction of the fading at the ends of the fade:
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Are you saying you want the events to happen serially rather than simultaneously? Just use a flag or an enum. You will basically have 2 states: blink and fade. When the blink state is done set the state to fade. When the fade state is done set the state to blink. ad infinitum...
You can do both you can execute things sequentially and / or in "parallel"
the main advantage of a switch-case-break;-statement is that it reduces the number of if-conditions to a minimum and in combination with non-blocking timing the code stays responsive to input like button-presses etc.
Currently I am interested in it just running, blink on of then fade, repeat into infinity
Trying to wrap my head around the construction and what is needed to figure this sequence first
So here comes a code well structured in functions where each function does one thing
using consequently non-blocking timing (there are zero delay()s in the code)
with an easy to use timing-function
blink the inbuild LED for 4 seconds
then
fade LED on IO-pin 9 for 8 seconds
repeat
also using the switch-case-break;-statement
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int ledPin = LED_BUILTIN; // the number of the LED pin
unsigned long MyChangeStateTimer = 0; // Timer-variables MUST be of type unsigned long
unsigned long myFadingTimer;
const byte blinking = 0;
const byte fading = 1;
byte myState = blinking;
void setup() {
pinMode(ledPin, OUTPUT); // set the digital pin as output:
pinMode(led, OUTPUT);
}
void loop() {
blink_and_Fade();
}
void blink_and_Fade() {
switch (myState) {
case blinking:
BlinkHeartBeatLED(ledPin, 100);
if ( TimePeriodIsOver(MyChangeStateTimer, 4000) ) {
digitalWrite(ledPin,LOW);
myState = fading;
}
break; // IMMIDIATELY jump down to End-of-Switch
case fading:
doFading();
if ( TimePeriodIsOver(MyChangeStateTimer, 8000) ) {
analogWrite(led, 0);
myState = blinking;
}
break; // IMMIDIATELY jump down to End-of-Switch
} // End-of-Switch
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void doFading() {
if ( TimePeriodIsOver(myFadingTimer, 30) ) {
brightness = brightness + fadeAmount;// change the brightness for next time through the loop:
analogWrite(led, brightness);
if (brightness <= 0 || brightness >= 255) { // reverse the direction of the fading at the ends of the fade:
fadeAmount = -fadeAmount;
}
}
}
Stefan,
Thanks it is getting to where I am understanding though without comments throughout sketch, I am unclear as to what different sections do Is it possible to add more comments especially in the second half of the variables and in the cases?
thanks
So read them carefully, look into things you don't understand and figure out what the different sections do.
Literally put your finger on the code and trace it step by step like you were a dumb microprocessor. That's all the Arduino is doing. Step by step.
No good writer of anything is not also very likely to be a good reader. This is a way to learn about what the code does on a line by line level, and how those steps form an algorithm to get stuff to happen.
With such generalised questions like "any thoughts" you are saying in a hided way
"can somebody write down a 3 pages tutorial that explains it all?"
This will not happen.
As soon as you start to ask specific questions about a single linge of code
the answers will rush in.
The difference between a generalised question and a specific question is that you pin-point to a detail that is unclear to you.
A tutorial does pin-point to everything that might be unclear. This is the reason why a tutorial needs so much more text.
I have already posted links to tutorials and you yourself are able to use google
If you are too lazy to do some work yourself go by either a professional tutorial or a professional ready to use product.
No I am asking to be directed to a tutorial because after working for about 5 hours, I am still stuck and looking for guidance. Your sketch was a great help but even as I go through line by line, I am missing something so I ask for more guidance, not a tutorial from the people on the forum more a place to continue trying to educate myself.
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.