Hi all, first of all thank you for taking the time to read my problem:
I have 6 motors which I need to be kept on independently using an Arduino Mega till the timer for each independent machine runs out;
Motor A (connected to pin 22) will be given a countdown of 20minutes and Motor B (connected to pin 24) will be given a countdown of 10 minutes and the motors will be turned off 20 minutes and 10 minutes later respectively. I am planning to do this by simply keeping the pins 22 and 24 HIGH till the countdown finishes and I am able to do this.
But my difficulty is I need to change the countdown for each machine using the computer while the Arduino's uploaded sketch is running. Meaning, say Machine A and B are running and will be turned off in 1 hour. I want to set the countdown for Motor C as 2 hours and change the countdown of Motor B to 30minutes.
Is it possible, if it is so how can I change the values of the variables inside the sketch running in the Arduino without having to stop or recompile the arduino sketch...?
Thank you in advance
You could write a piece of code to interpret serial commands. I.e. give each variable you wish to modify a text name, then in the serial monitor you could send
DELAY_TIME: 500;
; being a terminator
DELAY_TIME being the variable
500 the new value.
not to hard to implement.
could even extend to something like
READ: DELAY_TIME;
to read the data.
There's 2 things you need to do.
-
You need to disable the auto-reset functionality of the Arduino, so that when you open the serial port from the computer it doesn't reset the Arduino.
-
You need to define a protocol and write the code to parse and use that protocol.
The protocol is the sequence of characters and numbers you will send from the computer to the Arduino that tells the Arduino what to do. It is up to you how you do it.
Then, the Arduino needs programming to react to that protocol, receiving characters on the serial port and looking for the patterns you have defined in your protocol. From then it's just a matter of decoding the information in the protocol (how depends on how you design the protocol) and just setting the variable values as you would normally (variablename = newvalue).
To pYro_65;
do you mean I can use the arduino and the arduino program like 'scanf("%i", &variable)' in C programming to enter the value for variable while the program is running? if so, may I know how...? thanks in advance
-also, can I run one function in the background? ::meaning while the counter is counting down the time for each machine, I need to pop a menu for the user for him to change the values of the countdown of each machine... so while the menu is being displayed to the user, can I run the counter function in the background using the arduino?
like 'scanf("%i", &variable)' in C programming
The Arduino is C programming.
And you would use sscanf not scanf as the source is in a string.
As for "background" - there isn't the concept of background and foreground.
There is interrupts, which is as close to "background" as you can get, but that's not really what you want.
You want "concurrent" operations. You can do things at the same time as counting down if you don't use things like delay().
My making your system "stateful" you don't care about how long things take, and you just run them when they need to be run.
For instance, using delay() is a bad thing. Instead, examine the return value of the millis() function to find out how much time has passed. You can do other things while you're waiting for that time to pass.
Thanks alot majenko. I used millis() at start and at end and found out how much time will be elapsed
one more issue... in the while loop as below:
1] while(reply == 'q') {
2] //do something here;
3] reply = Serial.read();
4] }
I want the loop to run over and over again till the user keys in letter 'q'.
But the problem is that it will stop at line 3 waiting for user reply. How can I make the while-loop go over and over again till the user enters 'q' or any key if it makes the problem easier. Thank you.
But the problem is that it will stop at line 3 waiting for user reply.
It most certainly does not. Serial.read() will return -1 if there is nothing in the serial buffer.
how about using a do-while loop to make the program wait for a user input?
how about using a do-while loop to make the program wait for a user input?
No. A while loop, yes.
jdforlife:
how about using a do-while loop to make the program wait for a user input?
You could do this:
int c;
do {
c = Serial.read();
} while (c < 0);
which will loop until you get a char typed from the user.
int c;
do {
c = Serial.read();
} while (c != 'q');
will loop until you get a 'q'.
Is that the sort of thing you mean?