hi, I am working on Automatic Domestic Watering for plants project.. Used simpleTimer library for this as i have to water the plants at regular intervals.. this is my code..
#include <SimpleTimer.h> // from the simple timer library
SimpleTimer timer;
void RepeatTask() {
digitalWrite(8,HIGH); //this connects to relay which has solenoid connection -> solenoid will help in allowing water flow through tube.
delay(300000); // water will flow for 5 minutes
}
void setup() {
Serial.begin(9600);
pinMode(8,OUTPUT); // relay
// timed actions setup
timer.setInterval(43200000, RepeatTask); // for relay. repeats every 12 hours. so waters the plants every 12 hours
}
void loop() {
timer.run(); // from simple timer example.
}
my PROBLEM is that when i connect the arduino uno with USB cable and upload it, it works fine. but after that, when i remove the USB cable and connect it to an external power, nothing happens. the program just gets erased it seems. but when i upload other programs (like LED blink), it works fine with external power..
i tested with direct power supply (12 v DC adapter, 1 A), and also with a 9v battery. The arduino works fine.No problem in that. But the program doesn't get executed as it usually does unless or until we erase the program.
@UKHeliBob
thats y i gave the delay. please see the comment..mentioned 5 min.. then it jumps off to timer as it usually does..if i am wrong at somepoint, please do tell me.
The sketch has no idea how the board is powered. Your code will not run any differently on USB power vs. external power.
How do you know the code is not running? The posted code changes output once after 12 hours and then never changes output again. Assuming that setting pin 8 high turns the water on and setting it low turns the water off, you never turn the water off once it is on.
ow ow!! you are going too much into that very early. I tested it with led blinking (instead of watering) for example.. I connected the USB, then uploaded it. The program loaded correctly (coz i saw LED blink after the time i specified (in timer interval for repeatme). then i just removed my USB cable, then connected a 9V battery. Arduino got power, no other work is done. When i upload anyother program(LED blink in example program like that..), it works fine..
is there anything to do in extra to make it independent of the USB cable after the program is loaded???
if there are something, plz say that.!! i am new here boss. dont just say it wont load it wont load!! plz elaborate. I cant get you...
if at all some changes has to be done to the code, plz help me with that....
You do not need to tell the Arduino that it is working on external power.
How are you connecting the 9V battery to the Arduino ?
If it is a PP3 battery then its life will be extremely limited. Have you measured the voltage of the battery when it is powering the Arduino ?
How much current does the relay take ? Can you provide a link to it ?
Try this series of actions.
Connect via USB and upload the program. How long is it before the relay turns on ?
Disconnect USB and connect the 9V battery (how are you connecting it ?). What is the battery voltage ? How long before the relay turns on, if at all ?
Disconnect the 9V battery and reconnect the USB. Do NOT upload the program again. How long is it before the relay turns on ?
Apart from the above, have you taken on board that once turned on the relay will never turn off when using the program that you posted ?
I connected to it with Vin. I said i tried out with another program!! from that itself you can infer that the battery connection is ok. anyway i connected a band new 9v battery. i also tried with direct method (12v dc adapter).
when i remove USB (after upload), and connect with external source, nothing happens. when i reconnect with USB, nothing happens. the board gets power.thats the only thing that happens..
while testing with other program, everything works normal with USB, 9v supply, 12v adapter etc. But t=for SIMPLE TIMER program, it is not. May be it has a new library and the fact that i have to include a new line" #include<SimpleTimer.h> .?????
NOTE : For TESTING purpose, i checked the code with LED blink. Instead of water delivery , i used onboard LED. (no problem with onboard LED).
the code is the problem here.. NOT the external source, what it is, how it is, how much it has it can, etc...
Pin changed from 8 to 13 for onboard LED.
12 hour timer reduced to 4000 ms for testing.
5 minute delay reduced to 1000 ms for testing.
#include <SimpleTimer.h> // from the simple timer library
SimpleTimer timer;
void RepeatTask() {
digitalWrite(13,HIGH); //this connects to LED
delay(1000); // wait 1 second
}
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT); // LED
// timed actions setup
timer.setInterval(4000, RepeatTask); // for LED. repeats every 4 seconds
}
void loop() {
timer.run(); // from simple timer example.
}
This code ran identically with Arduino Uno connected to PC and connected to external power supply: after power on or reset, LED flashes briefly (Arduino booting up), LED goes off, LED comes on after 4 seconds, then stays on indefinitely (since there is no digitalWrite(13,LOW) to turn it off.
So the code does what I would expect, both with USB power and external power.
I did the same thing but got the desired result till I unplug the USB cable. after that, no response. This happened for many times for many boards (3 actually). All of them had the same result.
i used a 12v adapter (standard one) for arduino uno. then i connected a relay with it (5V relay). In that relay, the desired voltage pin is connected to a separate 12v battery (finely working). Output pin(with desired signal when the relay gets signal from arduino) is connected to solenoid valve which gets actuated (make way for water to pass thru) when it receives 12v signal .
so total current consumption is less than 1A against 1~1.5 A which is supplied to arduino.
Current Consumption isnt important here!!! I already said that i tested this timer with onboard LED instead of relay and solenoid stuff and still got the problem.. So the problem is totally with the code and not with the current supply... please do understand that...thanks
If "help you with the code" means write the code for you, you should post a request in the Gigs & Collaborations section of the forum. Note that most people would expect to be paid for working for you.
come on man!! helping with the code means correcting the code or helping with a method by which the current code of mine can work.. dont start over again!! I already said the code is working fine only when the arduino uno is connected to the computer.. not with any other supply.. and this happens because of the code/software ( i dont know), and the sources are just fine, wiring and everything is fine but the code.. thats y i asked for help..thats what forums are for.. and you are saying i have to pay for getting HELP?? then that isnt helping!!!
Hackscribble has tested your code, and says the led does not turn off. Others, including me, look at the code and say, yes, the led won't turn off because there is no writing LOW in the repeat task function.
You say that the code works fine.
I am having trouble getting beyond this first contradiction. It's hard to focus on the very unusual issue of internal vs external power supply if we can't get over the first hurdle. If you can run Hacksribble's code as posted and the led goes off and then on again on with a 4 second interval, then, then please document that for the group(maybe with some photo's, or a link to a you tube video) and maybe you will get more help and less attitude.
I thought I would try the code in Reply #10 just for fun. I didn't have the SimpleTimer library and it became too much trouble to install it so I rewrote the code using millis() to do the timing. I don't really see any advantage adding 400 lines of library code.
And this code works just as well when connected to the PC or running from a battery pack (6xAA batteries).
unsigned long curMillis;
unsigned long intervalMillis = 4000;
unsigned long prevMillis;
void RepeatTask() {
digitalWrite(13, ! digitalRead(13)); //this connects to LED
}
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT); // LED
digitalWrite(13,LOW);
}
void loop() {
curMillis = millis();
if (curMillis - prevMillis >= intervalMillis) {
prevMillis += intervalMillis;
RepeatTask();
}
}
i am not asking for an alternative. I just want to clarify, why a program works perfect when connected to pc and why not when connected to anyother suitable power source and on both the case, the program is feed to the arduino uno.