Monitor the analog signal without loop

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)

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?

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

Thanks for your fast response.

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.

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?

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.

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.

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".

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.

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?

Q: whats HV?

Parental guidance advised

That's it, HV = High Voltage.

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

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

Thank you again.

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!

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.