Regarding General Arduino Programming related Issues

I have been coding a project in Arduino but facing a few problems in that process.

  1. Is there any provision to swap the function's [setup() and loop()] order of operation.
  2. How can I execute a different function (which I have created), apart from calling them from setup() and loop() functions.

Sincerely Yours,
Debmallya Das.

Is there any provision to swap the function's [setup() and loop()] order of operation.

No. Why would you want to ? You could, of course, exchange the code from setup() into loop() and vice versa but don't expect it to work afterwards

How can I execute a different function (which I have created), apart from calling them from setup() and loop() functions.

You can't unless you call it from a function that is itself called from setup() or loop() or write your own main() function to replace the one that the compilation process creates for you behind the scenes and don't have setup() or loop() in your program at all.

What is it that you are trying to do ?.

UKHeliBob:
What is it that you are trying to do ?.

I am trying to get data from loop() function, and analyze that acquired data in a separate function(self made), and show the output in serial monitor.

As my analysis is dependent on the complete data acquired by loop() function, I cannot call my separate self made function from the loop() function.

And as the setup() function is executed before the loop() function, I cannot call my self made function from setup() function because before entering the loop() function, I have no data to work with.

Thus I need a separate function for the analysis part.

I did not know about the main() method you suggested, that would solve all of my issues if the setup() and loop() functions can be completely avoided.

As my analysis is dependent on the complete data acquired by loop() function, I cannot call my separate self made function from the loop() function.

At some point, loop() has acquired all the required data. That would be where/when you call your function FROM LOOP().

Post the code that you are having problems with, as the stickies require for anyone posting in this section of the forum.

As my analysis is dependent on the complete data acquired by loop() function, I cannot call my separate self made function from the loop() function.

So don't call your function until the complete data has been acquired. How will you know when it is complete ?

that would solve all of my issues

Based on the nature of your original question I can predict with some certainty that it won't.

I am trying to get data from loop() function, and analyze that acquired data in a separate function(self made), and show the output in serial monitor.

That is a general description of just about every program that has input data and output to the serial monitor. I'm not aware of anyone else who has found it necessary to change the basic structure of the programming language in order to do that, so it is a bit baffling why you have determined that it is. A more complete description of exactly what you are trying to accomplish would be needed before anyone will be able to understand your problem.

The code:

int count=0;
int i;
int val[1000];
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop()
{
int sensorValue1=analogRead(A1);
Serial.println(sensorValue1);
val*=sensorValue1;*

  • //delay(0.824);*
  • count++;*
  • if(count==1000)*
  • {*
  • while(1)*
  • {*
  • //do nothing*
  • }*
  • }*
    }
    void peakdetect()
    {
  • int peak[1000];*
  • int pos[1000];*
  • int i,j=0,k=1;*
  • for(i=0;i<1000;i++)*
  • {*
    _ if(val[i+1]<val*)_
    _
    {_
    if((val_==val[i-1])||(val>val[i-1]))
    {
    peak[j]=val;
    pos[j]=i;
    j++;
    }
    }
    }
    i=0;
    while(k<j)
    {
    if((pos-pos[k])>(-5))
    {
    peak[k]=0;
    }
    else*

    * {
    i=k;
    }
    k++;
    }
    for(i=0;i<j;i++)
    {
    if(peak!=0&&peak>500)
    {
    Serial.print(peak);
    Serial.print("-> ");
    Serial.println(pos);
    }
    }
    }*

    I want to call peakdetect() after the loop() function is completed._

Debmallya:
I have been coding a project in Arduino but facing a few problems in that process.

  1. Is there any provision to swap the function's [setup() and loop()] order of operation.
  2. How can I execute a different function (which I have created), apart from calling them from setup() and loop() functions.

Sincerely Yours,
Debmallya Das.

You can look at several event driven libraries. I like uMT for the DUE, there is a version of freeRTOS that will run on the Arduino, I use freeRTOS on the ESP32. My loop function looks like this:

void loop () {}

With uMt and freeRTOS you can run your functions either in round robin, event driven or a combination of round robin and event driven.

The loop functions, when empty, becomes the clean up routine that runs at the lowest priority.

The code:

I bet that it is not in italics when in the IDE

Please put the code in code tags.

Don't know how to do that, then read this before posting a programming question

The code:

int count=0;
int i;
int val[1000];
void setup() 
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() 
{
      int sensorValue1=analogRead(A1);
      Serial.println(sensorValue1);
      val[i]=sensorValue1;
      //delay(0.824);
      count++;
      if(count==1000)
          {
             while(1)
              {
                //do nothing
              }
          }
 }
void peakdetect()
{
  int peak[1000];
  int pos[1000];
  int i,j=0,k=1;
  for(i=0;i<1000;i++)
  {
    if(val[i+1]<val[i])
    {
      if((val[i]==val[i-1])||(val[i]>val[i-1]))
      {
        peak[j]=val[i];
        pos[j]=i;
        j++;
      }
    }
  }
  i=0;
  while(k<j)
  {
    if((pos[i]-pos[k])>(-5))
    {
      peak[k]=0;
    }
    else
    {
      i=k;
    }
    k++;
  }
  for(i=0;i<j;i++)
  {
    if(peak[i]!=0&&peak[i]>500)
    {
      Serial.print(peak[i]);
      Serial.print("-> ");
      Serial.println(pos[i]);
    }
  }
}
int val[1000];

On which Arduino?

      if(count==1000)
          {
             while(1)
              {
                //do nothing
              }
          }

Isn't this where you'd actually use (analyze) the data you collected, instead of stuffing your head in the sand?

If you just want to do this once, move all the code from loop into setup. Get rid of the while(1) loop and when count is 1000, call peakdetect. Then loop will get called forever more, doing nothing.

PaulS:
Isn't this where you'd actually use (analyze) the data you collected, instead of stuffing your head in the sand?

I am new to this Arduino programming, Thank You for your kind words of encouragement.
I will try out what you suggested.

Debmallya:
I am new to this Arduino programming

Which is a good reason for first learning the Arduino programming paradigm and how to develop application code within it. As noted, your stated application can easily be done within this paradigm in several ways.

While it is possible to go outside of the Arduino model, you would have to recreate yourself the initialization and housekeeping work that it does for you. You’d also need a similar application code structure since there is no OS to “return to” once the task is done. Based on my perception of your coding experience (gained only from this thread), I’m not sanguine about your chance of success.

@OP

PaulS:

int val[1000];

On which Arduino?

Have you thought on the above remark?

You have declared an array of 1000 elements in the global area; where, each element will occupy 2 memory locations and they all will be initialized to 0s. How many bytes/locations of the RAM Space (aka SRAM for static RAM) of your Arduino will be reserved for the array -- 2000 bytes?

Most of the users of Arduino Forum use Arduino UNO whose SRAM capacity is shown below:
ram.png

Think if it safe to declare such such a big array in UNO Platform or if you need such a big array at all.

ram.png

I'm not really understanding how you are determining the peak value, hard to follow the code looking at it on a phone.

The loop portion does have an error, you are never changing the value of i, so every input is stored in the same element of the array.