Problem with counter

Hi friends,

i am not able to find the way mu counter does not go across 10.
I want to counter the row of my values but the counter stop up to 10.
Could you help me ?
Thanks

Donato

char buffer[20];
int ledinput1 = 2;
int ledinput2= 3;
int ledinput3 = 4;
int cont=0;
int starttime;
void setup()
{

Serial.begin(57600);
// Serial.println(“Lettura”);
pinMode(ledinput1,INPUT);
pinMode(ledinput2,INPUT);
pinMode(ledinput3,INPUT);
starttime=0;
}

void loop()

{
int input=int(analogRead(ledinput1));
int input1=int(analogRead(ledinput2));
int input2=int(analogRead(ledinput3));
int input3;
if((millis()-starttime) <=1000)
{
delay(1);
// Serial.print(" Leggiamo i valori del potenziometro: ");
// Serial.print("il valore di sens0 e’: ");
// Serial.println(sens0);
input3=input1;
buffer[0] = (input / 1000) + ‘0’; input %= 1000;
buffer[1] = (input / 100) + ‘0’; input %= 100;
buffer[2] = (input / 10) + ‘0’; input %= 10;
buffer[3] = (input) + ‘0’;
buffer[4]=’,’;
buffer[5] = (input1 / 1000) + ‘0’; input1 %= 1000;
buffer[6] = (input1 / 100) + ‘0’; input1 %= 100;
buffer[7] = (input1 / 10) + ‘0’; input1 %= 10;
buffer[8] = (input1) + ‘0’;
buffer[9]=’,’;
buffer[10] = (input3 / 1000)+ ‘0’ ; input3 %= 1000;
buffer[11] = (input3 / 100)+ ‘0’ ; input3 %= 100;
buffer[12] = (input3 / 10)+ ‘0’; input3 %= 10;
buffer[13] = (input3)+ ‘0’ ;
buffer[14]=’,’;
buffer[15] = (cont / 1000) + 48; cont %= 1000;
buffer[16] = (cont / 100) + 48; cont %= 100;
buffer[17] = (cont / 10) + 48; cont %= 10;
buffer[18] = (cont) + 48;

//Serial.println("L’array compilato e’ : ");
Serial.println(buffer);

// Serial.print("L’array ad ogni ciclo e’ : ");Serial.println(buffer);
//delay(1);

//Serial.print("Il valore di cont e’ ");
//Serial.println(cont);
cont++;

}

}

To illustrate why cont never goes above 10, I’ve extracted only a portion of your code:

void loop()

{
   if((millis()-starttime) <=1000)
  {
// Snipped a bunch of code
      cont++;
  }
}

The variable starttime is never reset, so the snipped code only executes for one second after the Arduino is restarted.

The snipped code takes some time to execute, with the analogRead statements taking the longest, and the Serial.println taking time, too.

Why are you only letting this run for one second?

Hi Pauls,

i want to let for only one second because i want to know how many values Arduino can read in a second. My intention is fill the buffer with the three values of analogRead, adding a counter as last value.if i obtain 106 rows of values why i have the variable count up to 10?

Does cont go 0, 1, 2... uo to 10, and then stay at 10 for the remaining 90+ rows?

No paul , it goes up to 10 , after restart from 1 up to ten , for the whole cycle. I dont understand why it is possible , why the cont variable restart from one once it is equal to ten

I actually got out my Arduino and copied your code to it. I added a bunch of Serial.print statements to see what was going on. Finally, the light went on.

In the last 4 lines that put values in buffer, you are modifying cont.

cont %= n; changes the value of cont. All 4 do nothing, the first 9 times through, because cont is less than 10. As soon as cont hits 10, cont %= 10 resets it to 0.

You need, as you've no doubt guessed by now, to make a copy of cont, and modify that copy.

Great PaulS, thank you very much.

Regards Donato