Pages: [1]   Go Down
Author Topic: Monitor the analog signal without loop  (Read 1439 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean by:
Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, i do not. It is just for monitoring and to check that all is going properly.

Thanks for your fast response.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13718
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

something like this?

Code:
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
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 Offline
Full Member
***
Karma: 2
Posts: 162
Never catch a falling soldering iron...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
for(;;);

Do you understand what this does?  It repeats the line forever, effectively halting the program.  It's like saying "stop".
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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(;smiley-wink; 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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13718
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, without loop(), embedded in your framework it is even simpler.

Code:
#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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Q: whats HV?
Parental guidance advised
« 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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's it, HV = High Voltage.

But, don't worry. I won't connect 25000V directly to Arduino!  smiley-grin

Many thanks. This is exactly what I needed!! I hope I can count on your help next time.

Thank you again.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13718
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: