loop forever in for=1 value

Hello.

I’m using arduino UNO r3 with SD shield.

I’m stuck with some code I made. The objetive of the code is to use an analog measure of voltage very fast. Put the values in a short called “curva*”. Compare the actual value measured with the value stored before and if the difference (diff) is higher than a threshold it triggers some code with an if sentence.*
The problem is that for some reason the difference is always 0 no matter what i measure or what I do, and the for sentence has always i=1 value and never goes up.
This is a chunk of the code that gives the problem.
```
*void loop()
{
  Serial.println(“loop”);
  long int last = 0;
  long int now = 0;
  last = millis();

for(int i=1; i<V; i++)
  {           
    Serial.println(“for”);
    now = millis();
    curva[i] = analogRead(AnalogPin);
    if(i=1)
      {
        curva[0]=curva[1];  //the loop stays forever here for some reason
      }
    int diff=curva[i-1]-curva[i];
    Serial.print(“Diff=”);
    Serial.println(diff);
         
    //here if diff>threshold I trigger some code

while(now == last)
      {     
      delayMicroseconds(Delay);
      now = millis();
      }
     
  }
    writelink();
}*
* *Here I put the full code but it can be very very confusing* *
*#define V 100
#include <SD.h>

const int chipSelect = 4;

short curva[V];
short auxiliar[3];

int AnalogPin = 0; //pin analogico 0
int LedPin = 13; //pin con led
int Delay = 1000000;            //delay 200 microsegundos
int threshold = 205;            //umbral de disparo. No es float porque va de 0 a 1023
int n1 = 50;                    //datos guardados antes del trigger
int n2 = 50;                  //datos guardados despues del trigger
int c=0;

void setup()
{
Serial.begin(9600);
Serial.println(“setup”);
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
    Serial.println(“initialization failed!”);
    while(true) {}
}
}

void loop()
{
  delay(1000);
  Serial.println(“loop”);
  long int last = 0;
  long int now = 0;
  last = millis();

for(int i=1; i<V; i++)
  {           
    Serial.println(“for”);
    now = millis();
    curva[i] = analogRead(AnalogPin);
    if(i=1)
      {
        curva[0]=curva[1];
      }
    int diff=curva[i-1]-curva[i];
    delay(1000);
    Serial.print(“Diff=”);
    Serial.println(diff);
    if (diff > threshold)
      {
        int triggern = i;
        auxiliar[1]=triggern;
        int n3=triggern-n1;
        auxiliar[3]=n3;
        if(triggern < n1)
          {
          for(int a=0; a<n2; a++)
            {
            Serial.println(“trigger>n1”);
            now = millis();
            while(now == last)
              {      // wait until 1 ms elapses
              delayMicroseconds(Delay);
              now = millis();
              }
            curva[a+triggern] = analogRead(AnalogPin);
            }
          }
          else
          {
            for( int b=triggern; b<V; b++) //quizas triggern en lugar de i
              {
              now = millis();
              while(now == last)
                {      // wait until 1 ms elapses
                delayMicroseconds(Delay);
                now = millis();
                }
              curva[b] = analogRead(AnalogPin);
              }
              for( int c=0; c<n3; c++)
              now = millis();
              while(now == last)
                {      // wait until 1 ms elapses
                delayMicroseconds(Delay);
                now = millis();
                }
              curva[c] = analogRead(AnalogPin);
          }
      }
     
   
    while(now == last)
      {      // wait until 1 ms elapses
      delayMicroseconds(Delay);
      now = millis();
      }
     
    }
   
    writelink();
}

void writelink()
{
 
  Serial.println(“writelink1”);
 
  int aux=auxiliar[1];  //aux=triggern
  int n3=auxiliar[3];
 
  if(aux<n1)
  {
    //construimos el String
  String dataString = “”;
    {
      for( int d=0; d<n3; c++)
      dataString += String(curva[d]);
      dataString += String(",");
     
      for(int a=aux; a<aux+n2; a++)
      dataString += String(curva[a]);
      dataString += String(",");
     
      for (int e=aux+n2; e<V; e++)
      dataString += String(curva[e]);
      dataString += String(",");
    }
   
  Serial.println(“writelink3”);
   
  File dataFile = SD.open(“curva3.txt”, FILE_WRITE);
 
  Serial.println(“writelink4”);
 
  if (dataFile)
  {
    Serial.println(“writelink5”);
    dataFile.println(dataString);
    dataFile.println(“afterstring”);
    Serial.println(“writelink6”);
   
    dataFile.close();
    // print to the serial port too:
    Serial.println(“writelink7”);
  } 
  // if the file isn’t open, pop up an error:
  else
  {
    Serial.println(“error opening curva2.txt”);
  }
 
  while (true) {}
  }
  else
  {//condición triggern igual o mayor que n1
    String dataString = “”;
    {
      for( int c=0; c<n3; c++)
      dataString += curva[c];
      dataString += String(",");
     
      for(int f=n3; f<aux; f++)
      dataString += curva[f];
      dataString += String(",");
     
      for (int b=aux; b<V; b++)
      dataString += curva[b];
      dataString += String(",");
    }

Serial.println(“writelink3”);
   
    File dataFile = SD.open(“curva3.txt”, FILE_WRITE);
 
    Serial.println(“writelink4”);
 
    if (dataFile)
    {
      Serial.println(“writelink5”);
      dataFile.println(dataString);
      dataFile.println(“afterstring”);
      Serial.println(“writelink6”);
   
      dataFile.close();
      // print to the serial port too:
      Serial.println(“writelink7”);
    } 
    // if the file isn’t open, pop up an error:
    else
    {
      Serial.println(“error opening curva2.txt”);
    }
 
    while (true) {}
  }
}*
```
Thanks

‘i=1’

You are assigning 1 to i, therefore it’ll always be ‘true’, equality comparisons require two ‘=’

‘i==1’

You have this statement block:

   while(now == last) 
      {      // wait until 1 ms elapses
      delayMicroseconds(Delay);
      now = millis();
      }

repeated several times and it's only purpose is to kill time. Why not make it a function and pass last to it, return now, and assign that value back into last? It might save you a few bytes.

pYro_65:
'i=1'

You are assigning 1 to i, therefore it'll always be 'true', comparisons require two '='

'i==1'

So i was forcing the value of i to 1 inside the if sentence. You can not imagin how much time I spent with that thing. I feel so stupid right now. I tested and it works. Thanks.

econjack:
You have this statement block:

   while(now == last) 

{      // wait until 1 ms elapses
      delayMicroseconds(Delay);
      now = millis();
      }




repeated several times and it's only purpose is to kill time. Why not make it a function and pass *last* to it, return *now,* and assign that value back into *last*? It might save you a few bytes.

I dont understand very well what you mean.

What I think that while function is doing, is to make sure that the samples of voltage I get have a difference in time of the delay as exatly as possible (microseconds). I want the samples very well spaced if possible.

I'm using that code because I found someone doing something like what I want here:

In that post I also dont understand very well what while(1) does at the end.

You have duplicate code in several places. For example, you have:

     //here if diff>threshold I trigger some code 

    while(now == last) 
      {      
      delayMicroseconds(Delay);
      now = millis();
      }

What I'm saying is to replace it with:

     last = WasteTime(last);

and write and add this function to your file:

int WasteTime(int last)
{
    now = millis();

    while(now == last) 
    {      
      delayMicroseconds(Delay);
      now = millis();
    }

    return now;

}

Espada:
So i was forcing the value of i to 1 inside the if sentence. You can not imagin how much time I spent with that thing. I feel so stupid right now. I tested and it works. Thanks.

Common problem. Do not worry, there'll be many more :smiling_imp: . Especially when tired you can overlook the simplest mistake.