Hello good people of Arduino land.
A brief bit of background:
I'm building a multi-valve, multi-drop, water drop controller for high speed water drop photography projects.
(Or more specifically a controller that will operate three independent valves, which will each dispense three independently timed drops.)
I have taken the (brave?) step of moving away from the delay() function, since this is the only solution to running different things at (virtually) the same time - in a non-blocking way.
I'm very new to Arduino, so please accept my apologies for being thick!
Back when I was working with a single valve, life was easy....delay() did the job nicely.
But now I would like more than one valve....and so it's time for milis().
I have been working with the classic 'BlinkWithoutDelay' sketch, with some success.
I have learned how to vary the on and off times of the LED - which is standing in for the valve.
However, I would like to blink the LED three times only, via a switch press, and vary the on and off times for each blink.
This is what I'm having a great deal of trouble with!
Here is the order of events I would like to see:
- turn on LED
- wait ¾ second
- turn off LED
- wait 2 seconds
- turn on LED
- wait ½ second
- turn off LED
- wait ¼ second
- turn on LED
- wait 3 seconds
- turn off LED
The timings are arbitrary...suffice to say that I need them all to be different.
These values will eventually be entered via potentiometers....or perhaps rotary encoders if I'm feeling very brave!
(And naturally I realise that I will be working with milliseconds...I'm just illustrating my basic requirements)
Here is the sketch I'm starting with...a variation of the aforementioned 'BlinkWithoutDelay' sketch:
//define pins
const byte startButton = 6;
const byte led = 7;
//variable to keep track of the LED
bool ledState = LOW;
//standard millis timing variables
unsigned long currentMillis;
unsigned long previousMillis = 0;
//my timing intervals
const unsigned long onTime1 = 750;
const unsigned long offTime1 = 2000;
void setup() {
Serial.begin(9600);
pinMode(startButton, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
currentMillis = millis(); //grab the millis() value and set it to currentMillis
if ((ledState == HIGH) && (currentMillis - previousMillis >= onTime1)) { //if LED is on (ledState = HIGH) and onTime has passed...
ledState = LOW; //...set ledState LOW
previousMillis = currentMillis; //remember previousMillis for next iteration
digitalWrite(led, ledState); //change actual LED according to ledState - LOW in this case
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= offTime1)) { //if LED is off (ledState = LOW) and offTime has passed...
ledState = HIGH; //...set ledState to HIGH
previousMillis = currentMillis; //remember previousMillis for next iteration
digitalWrite(led, ledState); //change actual LED according to ledState - HIGH in this case
}
}
Obviously this will just cycle the same blink over and over.
(I'll cross the whole 'startButton' thing when I get this nailed down)
I've tried adding more timing variables and more if else() statements to accomodate the other 'blinks', but it doesn't seem to work.
I tried adding this in my variable declarations:
const unsigned long onTime2 = 500;
const unsigned long offTime2 = 250;
...and this in the main loop after the last if else() statement to add another timed 'blink':
else if ((ledState == HIGH) && (currentMillis - previousMillis >= onTime2)) {
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= offTime2)) {
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
What is happening when I added these extras is that I only seem to be getting a steady blink with no variation.
I've tried a variety of 'nesting' the if else() statements, but again no success.
It seems that simply adding more if else() statements is not the same as adding more delay() functions, and I've got myself in all sorts of knots....can't seem to understand why!
I've googled till I'm blue in the face and still have got nowhere...all I seem to get is solutions to problems I don't have - eg. how to blink several LEDs at different rates.
I hope some kind soul would be kind enough to nudge this old thicko in the right direction...I think I'm going blinking mad!!