I'm trying to understand how my Arduino thinks and processes code. My previous programming knowledge comes from PBASIC for BASIC Stamps, which compared to the complexity and power of C/C++ isn't getting me anywhere.
My goal is to have three LEDs on the breadboard:
One fading normally provided by the example
One briefly flashing when the pwm of the Fading LED reaches 0 or 255
One LED lit for a longer period without using delay @ 0 or 255
The only thing I cannot figure out is how to do is set the fader's pwm value of 0 or 255 as the trigger to get the third LED to flash for a set amount of time. In my code I nearly got it, however it is only triggered on and off and lowering the value of the interval has no effect.
My source for the two code examples are the Arduino's "Blink without delay" & "Fade"
const int ledPin = 12;
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 200; // interval at which to blink (milliseconds)
int brightness = 0;
int fadeAmount = 5;
unsigned long currentMillis = millis();
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(9, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop()
{
analogWrite(9, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255)
{
fadeAmount = -fadeAmount ;
digitalWrite(2, HIGH);
currentMillis = millis();
Serial.println(brightness);
}
else
{
digitalWrite(2, LOW);
}
if(currentMillis - previousMillis > interval)
{
//Serial.println( previousMillis);
previousMillis = currentMillis; // save the last time you blinked the LED
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
delay(30);
}
I am determined to figure this out, but advice would be greatly appreciated!
Tangent: I just learned that C/C++ utilizes subroutines, is that what I'm missing?
You don't need subroutines ('functions') to achieve this, although I do recommend that you learn about them because they are indispensable when you get to larger sketches.
I haven't looked at the code in detail, but I couple of things jump out at me:
This happens every time round loop() rather than after an interval. Probably, that means the fading will happen much faster than you intend:
brightness = brightness + fadeAmount;
Using the 'blink without delay' technique is absolutely the right way to control the timing of multiple things independently, as you're doing here. But if they are independent (for example, fading would be independent of the logic to decide when to end the 'flash') then you need to record the due time separately for each thing you're aiming to control.
Ok, I think I have an idea. You were right, I was trying to combine the two. How about in the if statement from the fader i insert something along the lines of:
if (brightness == 0 || brightness == 255)
{
fadeAmount = -fadeAmount ;
digitalWrite(2, HIGH);
currentMillis = millis();
timer = 1
}
else
{
digitalWrite(2, LOW);
timer = 0
}
Then when the timer = 1 I start the blink without delay code with little more than an if statement!
Using the 'blink without delay' approach, you want to design loop() to run repeatedly and each time it runs it will decide whether it needs to do anything.
If it is time to change the fade level, change the fade level and note when the next change is due.
If it is time to start the LED blink by turning it on, turn it on and note when it needs to be turned off.
If the LED is on and it is time to turn it off, turn it off.
And so on for each other thing you want to control. You need to record the due time for each of these actions separately, i.e. with a separate variable holding the due time for each action.
I really appreciate the help, but do you think you could translate that into some simple code? I have tried several times to get this to work. How do I check if the pwm value needs to be increased? Aside from that what do I put in the "if statement" of the fader to trigger the blink? During debugging I found that currentMillis - previousMillis = 1500. If i set the blink interval above that threshold the code worked just fine.
Would an interrupt make this easier?