Offline
Newbie
Karma: 0
Posts: 19
|
 |
« on: July 12, 2011, 11:35:42 am » |
Good afternoon.
I am quite new on Arduino and now I am working in my first project. I have found one problem, which I can not solve by myself. I have been searching for the solution and finally I decided to ask the experts.
Basically, and simply, I need to:
- Turn on LED 13 - Monitor the analog signal in analog pin 0 (for example, once every second) - Turn off LED 13 (for example, after 1 minute) - Stop monitoring
I can not use "void loop" for this, because I need to run the sketch a certain number of times, so I need to include this part of my sketch in "void setup". I think this should not be very difficult, but I could not find the way to do it...
I would appreciate if someone can help me with my project.
Thank you very much.
(Please, excuse my poor English)
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: July 12, 2011, 11:37:27 am » |
What do you mean by: Monitor the analog signal in analog pin 0 (for example, once every second) ? Do you need to take some action based on a value read back?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #2 on: July 12, 2011, 11:49:35 am » |
No, i do not. It is just for monitoring and to check that all is going properly.
Thanks for your fast response.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 100
Posts: 9548
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #3 on: July 12, 2011, 11:52:52 am » |
something like this? unsigned long start = 0;
void setup() { pinMode(13, OUTPUT); Serial.begin(115200); Serial.println("start"); }
void loop() { if (start == 0) // will be true only once ... { digitalWrite(13, HIGH); start = millis(); }
x = analogRead(A0); Serial.println(x, DEC); // do something with x
delay(1000); // to be tuned
if (millis() - start > 60000L) // are there 60 seconds past after start has been set? { digitalWrite(13, LOW); for(;;); } } Dont compile this code, just read it oud loud and understand what it does.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: July 12, 2011, 11:56:06 am » |
It is just for monitoring and to check that all is going properly.
That (to me) doesn't make a great deal of sense - "monitoring" and "properly" imply that there is an "improperly" and some action to be taken if this proves not to be the case in the course of monitoring. Can you explain a little more, please?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #5 on: July 12, 2011, 12:05:11 pm » |
Thank you robtillaart. Let me try it. I tell you if it is good for me as soon as possible.
AWOL, I understand your point. We need to monitor the current from one external device in the analog pin. It is just a value that we need to make sure that is stable. If not, we need to change some parts in the system we are working on. That is the reason that we do not need Arduino to do any action.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #6 on: July 12, 2011, 12:33:46 pm » |
robtillaart, your sketch is, actually, working perfectly. It does that what I need, but you have used the loop. To use the loop is a problem for us. Below I paste a small part of the code we are using now, in order to show you what really we need. int HV = 7; // HV supply connected to digital pin 7
void setup() {
pinMode (HV, OUTPUT); // sets the digital pin 7 as output for (int x=0; x<3; x++) { // repeats which is between {} 3 times
digitalWrite (HV, HIGH); // turns HV supply ON delay (60000); // HERE is where we need to see the current, as I explained above, during the time the HV is ON digitalWrite (HV, LOW); // turns HV supple OFF
}
void loop() {} You can note that we do not want to do the loop, since we only need to repeat the sketch 3 times. Thank you again.
|
|
|
|
|
Logged
|
|
|
|
|
Minneapolis, MN USA
Offline
Full Member
Karma: 2
Posts: 151
Never catch a falling soldering iron...
|
 |
« Reply #7 on: July 12, 2011, 12:59:02 pm » |
But you can perform the work in loop() and still only do it 3 times. You are assuming that loop() will always repeat for ever. But you can prevent it from repeating. That's why robtillaart told you to use this line: for(;;); Do you understand what this does? It repeats the line forever, effectively halting the program. It's like saying "stop".
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #8 on: July 12, 2011, 01:52:43 pm » |
Please, excuse my last stupid post. I have been working the whole day and now I can not see the things clearly. Really, the robtillaart's sketch is just what I needed! I did not realize that the loop ends. But, actually, I don't understand how for(; ; works, I have to study more... Now, the question is: how can I do to repeat the loop a certain number of times? Thanks to all for making this so fascinating world easy.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 100
Posts: 9548
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #9 on: July 12, 2011, 03:56:00 pm » |
OK, without loop(), embedded in your framework it is even simpler. #define HV 7 // HV supply connected to digital pin 7 #define REPEATS 3 // you may do that more often if you want #define DURATION 60000L // don't forget the L for long
void setup() { pinMode (HV, OUTPUT); // sets the digital pin 7 as output
pinMode(13, OUTPUT); Serial.begin(115200); Serial.println("start");
for (int i=0; i<REPEATS ; i++) { digitalWrite (HV, HIGH); // turns HV supply ON
digitalWrite(13, HIGH); unsigned long start = millis(); // remember the start time while (millis() - start < DURATION ) // for a minute { int x = analogRead(A0); Serial.print(millis(), DEC); // added time for tuning and Serial.print(" , "); // added a comma so the output can be pasted in excel easily Serial.println(x, DEC); // do something with x delay(1000); // to be tuned to do it every second } digitalWrite(13, LOW); }
digitalWrite (HV, LOW); // turns HV supple OFF }
void loop() {}
for( ; ; ); is an endless loop similar to while(true){} or while(1); and you can use goto for an endless loop too, but the use of goto is considered harmfull, and there are some more endless loop constructs in C++ Q: whats HV?
|
|
|
|
« Last Edit: July 12, 2011, 04:14:49 pm by robtillaart »
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: July 12, 2011, 04:06:25 pm » |
|
|
|
|
« Last Edit: July 13, 2011, 01:54:12 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #11 on: July 12, 2011, 04:28:23 pm » |
That's it, HV = High Voltage. But, don't worry. I won't connect 25000V directly to Arduino!  Many thanks. This is exactly what I needed!! I hope I can count on your help next time. Thank you again.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 100
Posts: 9548
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #12 on: July 12, 2011, 04:32:49 pm » |
And what are you measuring????
Please spend some time in the reference section and the tutorialsection of the forum, you can learn a lot there!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #13 on: July 12, 2011, 04:52:11 pm » |
We want to measure the current which comes from the HV supply. We receive modified voltage (from 0 to 5 volts, proportional to the high voltage) in the analog in, and with that value we can calculate and monitor the current with Arduino.
Actually, I don't know all the details, so I cannot give you more information, because this is my workmates' job, who are more strong on electronics than me. I will ask them tomorrow.
I am really having fun working with Arduino. For sure I will continue learning, at least for hobby, in the sections you mentioned.
|
|
|
|
|
Logged
|
|
|
|
|
|