Hello everyone. I have a problem that is making my head spin. I am trying to fade several LEDs at different intervals using Millis. I read a lot of tutorials and tried a lot of samples. I feel I am understanding it pretty good now for one LED but adding others is giving me difficulties. . So I got this example here, in hopes of figuring it out. Problem is I tried it and the sample doesn’t work. Tested my connections with another sketch. Copied it again in case I missed something. Does anyone know why this sketch is not working for me? Thanks for any help. I need to add it complies OK
[code]
// SeveralThingsAtTheSameTime.ino
// An expansion of the BlinkWithoutDelay concept to illustrate how a script
// can appear to do several things at the same time
// this sketch does the following
// it blinks the onboard LED (as in the blinkWithoutDelay sketch)
// it blinks two external LEDs (LedA and LedB) that are connected to pins 12 and 11.
// it turns another Led (buttonLed connected to pin 10) on or off whenever a button
// connected to pin 7 is pressed
// it sweeps a servo (connected to pin 5) back and forth at different speeds
// One leg of each LED should be connected to the relevant pin and the other leg should be connected to a
// resistor of 470 ohms or more and the other end of the resistor to the Arduino GND.
// If the LED doesn't light its probably connected the wrong way round.
// On my Uno and Mega the "button" is just a piece of wire inserted into pin 7.
// Touching the end of the wire with a moist finger is sufficient to cause the switching action
// Of course a proper press-on-release-off button switch could also be used!
// The Arduino is not capable of supplying enough 5v power to operate a servo
// The servo should have it's own power supply and the power supply Ground should
// be connected to the Arduino Ground.
// The sketch is written to illustrate a few different programming features.
// The use of many functions with short pieces of code.
// Short pieces of code are much easier to follow and debug
// The use of variables to record the state of something (e.g. onBoardLedState) as a means to
// enable the different functions to determine what to do.
// The use of millis() to manage the timing of activities
// The definition of all numbers used by the program at the top of the sketch where
// they can easily be found if they need to be changed
// --------CONSTANTS (won't change)---------------
const int led_A_Pin = 9;
const int led_B_Pin = 10;
const int led_A_Interval = 2000;
const int led_B_Interval = 2000;
const int blinkDuration = 5000; // number of millisecs that Led's are on - all three leds use this
const int blinkDurationB = 200;
//------------ VARIABLES (will change)---------------------
byte led_A_State = LOW; // LOW = off
byte led_B_State = LOW;
// will be changed to negative value for movement in the other direction
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;
//========================================
void setup() {
Serial.begin(9600);
Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino"); // so we know what sketch is running
pinMode(led_A_Pin, OUTPUT);
pinMode(led_B_Pin, OUTPUT);
}
//========================================
void loop() {
// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
updateLed_A_State();
updateLed_B_State();
}
//========================================
void updateLed_A_State() {
if (led_A_State == LOW) {
if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
led_A_State = HIGH;
previousLed_A_Millis += led_A_Interval;
}
}
else {
if (currentMillis - previousLed_A_Millis >= blinkDuration) {
led_A_State = LOW;
previousLed_A_Millis += blinkDuration;
}
}
}
//========================================
void updateLed_B_State() {
if (led_B_State == LOW) {
if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
led_B_State = HIGH;
previousLed_B_Millis += led_B_Interval;
}
}
else {
if (currentMillis - previousLed_B_Millis >= blinkDuration) {
led_B_State = LOW;
previousLed_B_Millis += blinkDuration;
}
}
}
//========================================
void switchLeds() {
// this is the code that actually switches the LEDs on and off
digitalWrite(led_A_Pin, led_A_State);
digitalWrite(led_B_Pin, led_B_State);
}
//========================================
//========================================END
[/code]