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