musical0714:
I'm guessing the error was at :where I had to change HIGH --> LOW?
That's right.
musical0714:
Can you explain why
digitalWrite(port_LED, ledState); is inside the if loop?
It's actually within two if blocks.
The rational is this
If the LED is active, there is not need to do anything to the Solenoid. Similarly, if the Solenoid is active, there is no need to do anything with the LED.
The program works fine but I was just wondering because if it is inside the if clause then ledState will be assigned to HIGH state and it would never enter the if (ledState == LOW) clause again?
A couple of lines down from that if statement, we then check to see if the LED has been on for long enough. If the LED has been on for the predetermined amount of time, we turn it off (ie set ledState to LOW).
I'm just asking because I'm curious and want to learn more about the if clause functionality. I've always thought that the Arduino loops around so fast that it's impossible to "trap" a functionality inside an if clause
How fast a system runs has no impact on logic. If a condition is true or false, it will be true or false no matter how quickly or slowly you evaluate the condition.
I'd suggest the you get a piece of paper and run through the logic, line by line.
Put a heading Up top
LOOP 1:
Run through the code, line by line as if you have just turned the Arduino on. Write down the Logical variables and the digital inputs and their values. record each time that changes.
eg
ledState : HIGH -> LOW
led_On_Time: 0 -> 1000
for things like time variables, just start at 1000
Once you have come to the end of loop(), write a line under your output, then put another heading.
LOOP 2:
Run through the code again. This time, however, assume that 1 second (ie 1000 milliseconds) has passed since the last runthrough (don't worry about setup()). Use the values you wrote down for the last pass each time you encounter a variable.
Again, write the variable name out, and record their values.
Repeat this for the next 7 loops, each loop 1 second apart.
Finally, assume that and additional 10 seconds have passed, just to test the case where the solenoid has turned on, then turned off and the delay before the LED turns on again expired.
It does seem a bit tedious, but.. doing the above is an important exercise for a few reasons.
You can get an idea of how the program actually flows.
You can follow the logic
You can catch any logic errors
It really drives home the fact that loop() loops. Once that fact is fully understood, you suddenly 'get' how you can use it to drive your program
**EDIT
Just a quick addition.
In some functions you'll see a declaration like
static boolean someBool = false;
The keyword static
does two things.
- The variable will get initalised only on the first passthrough
- The value of the variable will be persistent between calls. In other words: on this passthrough, the variable will have the same value it was at the end of the last passthrough.