Hi guys and girls I've recently decided to try teach myself to program this really cool platform unfortunately I've never programmed before. I've watched several hours of tutorials and think I have a grasp of the basics. So I decided to try and take on a simple project but have found i kept hitting a dead end.
This is what I would like to do:
When input 1 is triggered my output must fade an led on for a set period of time then it must fade off but if input 2 is triggered earlier than the set time the delay must be bypassed and led output must fade off
Now i was able to get the led to fade on and delay for a certain time but then I found that the delay function seems to stop the whole process therefore rendering input 2 useless, I've read up on using the millis function but have no idea where to start.
I'm using the Arduino leonardo, can anyone point me in the right direction please.
Although the sketch is simple, the principle it is demonstrating is fundamentally important (and not explained at all in the example) and also non-obvious unless you stop and think about it. It requires a different mindset when designing your sketch. Instead of writing your sketch as a sequence of code that runs from beginning to end as your use case proceeds and waits for things to happen along the way, your code will test for things that may need to be handled and handles them if necessary. The result is that your code doesn't block (stop) waiting for something to happen. This means that even while it is waiting for one thing to happen (e.g. enough time to have passed) it can still respond to other events (e.g. an input changing state).
Ok so this is as far as ive got it fades on when the input is give but doesnt stay on it seems to go back and fade on again ive tried a few options but am stuck can anyone point out where ive gone wrong?
const int ledPin = 13; // the number of the LED pin
const int signal = 7; // the number od signal pin
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 5000; // interval at which to blink (milliseconds)
int fadeValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(signal, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval && fadeValue == 0) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if(digitalRead(signal) == HIGH && fadeValue == 0)
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
delay(50);
}
{ if(digitalRead(signal) == LOW)
digitalWrite(ledPin, LOW);
}
}}
You still have a delay in there which means you code is still blocking, that is it won't do anything while it is fading.
What you are missing apart from the above is that there is nothing to tell the code that the fade is finished. You need to indicate this with a variable or boolean variable that indicates that part of the sequence is done and don't re do it.