Compound Addition command not adding!

Hi all, a small issue in my first attempt at creating an autonomous car.

In short, to determine range to an obstacle I am using 2x Sharp IR sensors being read on A0 and A1. To try and minimise the amount of noise in their outputs I have sought to take 5x readings and then average them. The average value is then used to determine the action of the car.

The script I have put together is below;

#include <AFMotor.h>
#include <Servo.h>

AF_DCMotor drive (1, MOTOR12_64KHZ);
Servo steer;
// Analog voltage for range finding
int left_danger = 180;
int left_obj_if = 95;
int left_free = 75;
int right_danger = 215;
int right_obj_if = 155;
int right_free = 140;
int range_left = 0;
int range_right = 0;
// Servo postions for steering
int sahead = 90;
int sleft = 10;
int sright = 170;
// Motor speed control
int slow = 150;
int fast = 255;
int count = 0;

void setup()
{
  Serial.begin(9600);
  steer.attach(10);
  steer.write(90);
}

void loop()
{
  start:
  
  //Reset all range and count values to 0
  Serial.println("Reset Values");
  range_left = 0;
  range_right = 0;
  count = 0;
  
  // Take 5x readings from each Analog channel
  while(count <5){
    Serial.println("While Loop");
    range_left = range_left + analogRead(0);
    Serial.println(range_left);
    range_right += analogRead(1);
    Serial.println(range_right);
    count++;
    delay(10);
  }
  
  // Average the readings by dividing by Count
  // Print the sum of the 5x Analog reading for both the left and right channels
  Serial.println(range_left);
  Serial.println(range_right);
  range_left /= count;
  range_right /= count;
  // Print the average of the readings from both the left and right channels
  Serial.println(range_left);
  Serial.println(range_right);  
  delay(10000);
  
  // 1: Control while no objects detected within Free range
  if(range_left < left_free && range_right < right_free){
    Serial.println("All Clear");
    steer.write(sahead);
    drive.setSpeed(fast);
    drive.run(FORWARD);
    goto start;
  }
  
  // 2: Control when an object is closer than Free but further than Obj IF on the RIGHT
  if(range_left < left_free && range_right > right_free && range_right < right_obj_if){
    Serial.println("Obj Far Right");
    steer.write(sleft);
    drive.setSpeed(fast);
    drive.run(FORWARD);
    goto start;
  }
  
  // 3: Control when an object is closer than Free but further than Obj IF on the LEFT
  if(range_left > left_free && range_left < left_obj_if && range_right < right_free){
    Serial.println("Obj Far Left");
    steer.write(sright);
    drive.setSpeed(fast);
    drive.run(FORWARD);
    goto start;
  }
  
  // 4: Control when an object is closer than Obj IF but further than Danger on the RIGHT while LEFT is greater than Object IF
  if(range_left < left_obj_if && range_right < right_danger && range_right > right_obj_if){
    Serial.println("Obj Near Right");
    steer.write(sleft);
    drive.setSpeed(slow);
    drive.run(FORWARD);
    goto start;
  }
  
  // 5: Control when an object is closer than Obj IF but further than Danger on the LEFT while RIGHT is greater than Object IF
  if(range_right < right_obj_if && range_left < left_danger && range_left > left_obj_if){
    Serial.println("Obj Near Left");
    steer.write(sright);
    drive.setSpeed(slow);
    drive.run(FORWARD);
    goto start;
  }
  
  // 6: DANGER detected RIGHT
  // 6: Any other distance LEFT
  if(range_right > right_danger && range_left < left_danger){
    Serial.println("Danger Right");
    steer.write(sright);
    drive.setSpeed(slow);
    drive.run(BACKWARD);
    goto start;
  }
  
  // 7: DANGER detected LEFT
  // 7: Any other distance RIGHT
  if(range_right < right_danger && range_left > left_danger){
    Serial.println("Danger Left");
    steer.write(sleft);
    drive.setSpeed(slow);
    drive.run(BACKWARD);
    goto start;
  }
  
  // 8: DANGER detected both sides
  if(range_right < right_danger && range_left < left_danger){
    Serial.println("Danger In Front");
    steer.write(sahead);
    drive.setSpeed(slow);
    drive.run(BACKWARD);
    goto start;
  }
}

A sample of the Serial output is below to illustrate the error;

Reset Values
While Loop
4
4
While Loop
14
9
While Loop
18
9
While Loop
22
9
While Loop
22
13
22
13
4
2
All Clear

Basically, the program is looping correctly in the While loop, reading A0 and A1, but not correctly compounding the results. You can see that the compound division is working fine (Output: 4 and 2) but the compound addition that should have created the sum of the values within the loop has failed to work. Instead, the final two values read are used (Blue values giving green output).

Regards,

Paul

    goto start;

If I may suggest, get rid of that. Instead use "return".

That will return from loop, which effectively takes you back to the start.

the compound addition that should have created the sum of the values within the loop has failed to work.

Why do you say that? You are printing the compounding numbers. So you must have read (for "left");

4
10  --> giving 14
4   --> giving 18
4   --> giving 22

Chatty: Instead, the final two values read are used (Blue values giving green output).

I think you are misinterpreting the output.

22 13

22 13

4 2

The blue values are the running totals printed inside the loop. The red values are the same running totals printed after the loop. The orange values are the running totals divided by the count, which gives you the average value you're looking for. The values shown are self-consistent.

The values shown are self-consistent.

Which, of course, why printing just numbers is foolish. Had you printed some identifying text to go with the numbers, the data printed would have been obvious with no study required.

Thanks for your replies Nick and PeterH. I'm wondering if you are also able to provide me with a rock to hide under as well.... Very annoyed that I posted this now but I can only put the question down to staring at the screen for too long and going in circles!

For the record PaulS, I have been inserting and removing numerous Serial println/write statements. Some are still in the code that I posted, but most were removed to tidy up the relevent output so it was a little more presentable for members of this forum reading it.

Paul

Some are still in the code that I posted, but most were removed to tidy up the relevent output so it was a little more presentable for members of this forum reading it.

You mean these:

    Serial.println(range_left);
    Serial.println(range_right);
  Serial.println(range_left);
  Serial.println(range_right);
  // Print the average of the readings from both the left and right channels
  Serial.println(range_left);
  Serial.println(range_right);

that print nothing but numbers?

I rarely print a number by itself.

Serial.print("range_left after adding reading: ");
Serial.println(range_left);

Serial.print("range_left after adding all readings: ');
Serial.println(range_left);

Serial.print("range_left after averaging: ')
Serial.println(range_left);

Presto. No head scratching trying to figure out what that number all by itself means.

The streaming library simplifies that, too:

#include <Streaming.h>

...

Serial << "range_left = " << range_left << endl;

That lets you put the meaning and the number all on one line.

Thanks Nick, a much more elegant method.

Paul