Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #15 on: August 16, 2011, 09:20:13 am » |
This is just enhancement to my current code, which monitor the water level, if water fall below the sensor it will turn on the pump.
Right now every six seconds it read the sensor value, if LOW turn on the pump (yellow LED on), after six seconds if HIGH turn off the pump (green LED on). So at startup it will wait six seconds before anything happens, after six second one of the LED turn on (either pump on or pump off state). I wanted to take that only intial six seconds and have the all LEDs to flash before one of the LED remain solid on.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #16 on: August 16, 2011, 09:26:20 am » |
It was suggested that I can get the LED to flash inside setup(), but I dont see how to get its flash without inside a main loop().
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2345
|
 |
« Reply #17 on: August 16, 2011, 10:52:53 am » |
A large number of people on the forum could very easily make helpful suggestions, but seeing your code is crucial to those suggestions making sense.
Also, why do you want a six second delay when the program starts? Is it a desired feature, or a side effect of how loop is structured?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #18 on: August 16, 2011, 11:09:59 am » |
Because after pump turn on, I wanted its to remain on for 6 more seconds...just a buffer to water level.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #19 on: August 16, 2011, 03:53:54 pm » |
I was just looking for examples or how to get started.
Other than the one I wrote? In what way did that not work?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #20 on: August 16, 2011, 04:38:51 pm » |
Nick, below is what I took it from your example. It stay on for 1 second and off, its doesn't flash. int red = 8; // LED red int green = 4; // LED green int amber = 2; // LED amber
void setup() { ..... pinMode(red, OUTPUT); // set the digital pin 81 as output pinMode(green, OUTPUT); // set the digital pin 4 as output pinMode(amber, OUTPUT); // set the digital pin 2 as output digitalWrite(green, HIGH); for (int x=0; x<1000; x++) { delay(1); } digitalWrite(green, LOW); for (int x=0; x<1000; x++) { delay(1); } } void loop() ........
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #21 on: August 16, 2011, 04:41:05 pm » |
for those interested, this is my main loop. void loop() { relay_on_time = millis(); if (relay_on_time >= (loopTime + 6000)) { val = digitalRead(inPin); if (val == HIGH) { digitalWrite(green, LOW); digitalWrite(amber, HIGH); //amber LED on digitalWrite(relayPin, HIGH); if ( 0 == shutDownTime) // <<< changed shutDownTime = relay_on_time + 30000; // <<< changed } if (val == LOW) { digitalWrite(amber, LOW); // amber LED off digitalWrite(green, HIGH); // green LED on digitalWrite (relayPin, LOW); shutDownTime = 0; // <<< changed } if ( shutDownTime && shutDownTime <= relay_on_time ) // <<< changed { // <<< changed digitalWrite(amber, LOW); digitalWrite(green, LOW); // green LED off digitalWrite(relayPin, LOW); // <<< changed while(1) // <<< changed { // <<< changed // wait for reset // <<< changed digitalWrite(red, HIGH); // red LED on, wait for reset } // <<< changed } // <<< changed loopTime = relay_on_time; } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #22 on: August 16, 2011, 04:44:35 pm » |
val = digitalRead(inPin); if (val == HIGH) { digitalWrite(green, LOW); digitalWrite(amber, HIGH); //amber LED on digitalWrite(relayPin, HIGH); if ( 0 == shutDownTime) // <<< changed shutDownTime = relay_on_time + 30000; // <<< changed } if (val == LOW)
If "val" isn't HIGH as a result of a digitalRead, it is unlikely to be anything other than LOW, so retesting it is a bit of a waste of time. A simple "else" would suffice
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2345
|
 |
« Reply #23 on: August 16, 2011, 06:36:25 pm » |
Nick, below is what I took it from your example. It stay on for 1 second and off, its doesn't flash.
Nick's example does exactly what you want as is, if you increase the delays, no need to make the adaption of it (that broke it  )
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #24 on: August 16, 2011, 07:02:46 pm » |
Nick, below is what I took it from your example. It stay on for 1 second and off, its doesn't flash. ... digitalWrite(green, HIGH); for (int x=0; x<1000; x++) { delay(1); }
Well you took out the part that flashed it, didn't you? How about: for (byte i = 0; i < 6; i++) { digitalWrite (green, HIGH); delay (1000); digitalWrite (green, LOW); delay (1000); } That's on for 1 second, then off for 1 second. Tweak the delay as desired. for (int x=0; x<1000; x++) { delay(1); } That is the same as: delay (1000); Why make it more complicated?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #25 on: August 16, 2011, 07:33:15 pm » |
Ok, its work. I have to exam how this code works now :-)
Thank you Nick!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #26 on: August 16, 2011, 08:53:02 pm » |
for (byte i = 0; i < 6; i++) { digitalWrite (green, HIGH); delay (1000); digitalWrite (green, LOW); delay (1000); } I haven't try it yet, but just curious will the flashing still work if I use mills() instead of delay()? Of course the coding structure will change too.
|
|
|
|
|
Logged
|
|
|
|
|
Sydney, Australia
Offline
Full Member
Karma: 3
Posts: 230
Arduino rocks
|
 |
« Reply #27 on: August 17, 2011, 06:22:42 am » |
Anything's possible, so you could re-write that to use millis() instead of delay(), but why would you want to?? You'd have to spin in a tight loop waiting for millis() to go beyond a previously recorded value from millis(), which is basically what delay() does...
G.
|
|
|
|
|
Logged
|
Is life really that serious...??!
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2345
|
 |
« Reply #28 on: August 17, 2011, 08:46:38 am » |
^^ This. Or to put it another way, using millis is useful when you want your code to do other things during the delay, which is often the case and can give the illusion of doing many things at once. Here, you have no such need, so why complicate your code?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #29 on: August 17, 2011, 09:41:11 am » |
Just keep an open option if I want to expand the program to do something else in future :-)
|
|
|
|
|
Logged
|
|
|
|
|
|