Help me with my code, double loop if possible

I am using arduino UNO v3, my code should do the following, read 10 sensor values, make an average, turn one of the 3 leds according to the value. The average sum should be displayed once per second so that means I need to read the input value once every 0.1 seconds.

Before using the double loop, the min value was 0 and max value was around 600, using the double loop the read value from the sensor has gone haywire

later edit: for the simulation of the sensor, i am using a potentiometer

int leda = 2;
int ledb = 4;
int ledc = 6;

void setup() {
pinMode(leda, OUTPUT);
pinMode(ledb, OUTPUT);
pinMode(ledc, OUTPUT);
Serial.begin(9600);
}

void loop()
{

int a;
int v[10], i, s=0, j;
double m;

for (j=0; j < 10 ; j++){
Serial.println(j);
delay(100);

for (i=1; i<=10; i++)
{a = analogRead(A0);
v = a;
s=s+v*;*
}
}
m=s/10;
Serial.println(m);
* if (m < 200 )*
{
* digitalWrite(leda, HIGH);*
* digitalWrite(ledb, LOW);*
* digitalWrite(ledc, LOW);*
}
else if (m > 400 )
{
* digitalWrite(leda, LOW);*
* digitalWrite(ledc, HIGH);*
* digitalWrite(ledb, LOW);*
}
* else*
{
* digitalWrite(leda, LOW);*
* digitalWrite(ledc, LOW);*
* digitalWrite(ledb, HIGH);*
}

* Serial.println(a);*
* Serial.println(m);*

* delay(1000);*
}

Just taking a cursory look at it, shouldn't:

m=s/10;

Be more like

m=s/100;

Because you are averaging 100 samples since it's in an embedded loop.

But, I bet your problem is that you're blowing through the 32,767 limit of an int (variable "s"). Try making it a "long", and see if that helps.

Use code tags when posting. If you don’t know how, take a look at the How to use this Forum thread at the top of these forums.

Hi…try this…

for (i=1; i<=10; i++)
{a = analogRead(A0);
v = a;
s=s+v;
}
m=s/10;
Serial.println(m);
}

I think you are not averaging and serial sending in the right place.

My 10cents worth… Tom

emil_irimia:
I am using arduino UNO v3, my code should do the following, read 10 sensor values, make an average, turn one of the 3 leds according to the value. The average sum should be displayed once per second so that means I need to read the input value once every 0.1 seconds.

The code you have posted does not do what the above description says. Why do you need a double loop at all?

Your code is taking one hundred sensor readings, in groups of 10. It then divides the sum by ten. I don’t think this is what you want. Also you are really mis-using the variable v.
It is an array, and you should use a sub-script when assigning elements to it.

   v[j] = a;

Before using the double loop, the min value was 0 and max value was around 600, using the double loop the read value from the sensor has gone haywire

Do you mean the actual values from analogRead are now bad, or the “average”?

A much simpler thing to do:

void loop()
{
    long sum;
    int reading, avg;

    for (int i = 0; i < 10; i++)
    {
        reading = analogRead(A0);
        sum += reading;
    }
    avg = reading / 10;
    Serial.println(avg);
    delay(900);  /* most of a second. */
}

Please use code tags, like this:

int leda = 2;

int ledb = 4;
int ledc = 6;

void setup() {
  pinMode(leda, OUTPUT);
  pinMode(ledb, OUTPUT);
  pinMode(ledc, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

int a;
int v[10], i, s=0, j;
double m;

for (j=0; j < 10 ; j++){
Serial.println(j);
delay(100);

for (i=1; i<=10; i++)
{a = analogRead(A0);
v[i] = a;
s=s+v[i];
}
}
m=s/10;
Serial.println(m);

if (m < 200 )
{
   digitalWrite(leda, HIGH);
  digitalWrite(ledb, LOW);
  digitalWrite(ledc, LOW);
}
else if (m > 400 )
{
   digitalWrite(leda, LOW);
  digitalWrite(ledc, HIGH);
  digitalWrite(ledb, LOW);
}
  else
{
   digitalWrite(leda, LOW);
  digitalWrite(ledc, LOW);
  digitalWrite(ledb, HIGH);
}
 
   
  Serial.println(a);
  Serial.println(m);
 
  delay(1000);
}