Array Measurement Problem

I'm trying to get a robot to go thru a maze. It will go forward until it is 5 inches away
using a ultrasonic sensor. Then it will turn 90 degrees left take an ultrasonic measurement
and store the number in an array. Then it will turn 180 degrees right and take another measurement and store that number in a array. Then it will compare the two array numbers and which ever direction is larger it will go in that direction. The problem
I'm having is storing these numbers and then comparing them. The code I posted is not the complete bot code, just the code dealing with the two measurements. When the program is downloaded it waits for an IR signal and then starts taking readings. If the reading is 6 inches it goes to the subroutine (left). If the reading is 3 it goes to the subroutine (right). The problem is when I serial print the array in the sub it's always zero. How do I get the array to read measurement that sent it to the sub i.e. 6 if the measurement is 6, retain this number in the sub so I can compare it with the other sub number? I don't have the compare code written because I can't get the array to retain the measurement number. Thanks.

#include <Wire.h>
#include <IRremote.h>
int RECV_PIN = A0;//define the pin of IR receiver as A0
int LED_PIN = 3; //define the pin of LED as pin 3
int a = 0;
int value;
int trigPin = 12; // Trigger
int echoPin = 13; // Echo
long duration, cm, inches;
int i = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
int anArray[200];
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Initialize the IR receiver
pinMode(LED_PIN, OUTPUT); //set pin 3 of LED to OUTPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = (duration / 2) / 74;
Serial.println();
delay(1000);
int anArray[200];
byte arrayIndex = 0;
anArray[arrayIndex] = inches;
arrayIndex++;
for (int i = 0; i <= 100 ; i ++)
{
if (irrecv.decode(&results))
if (results.value == 0xFF02FD )
{
if (anArray[i] <= 50 )
if (anArray[i] > 0)
Serial.print(anArray[i]);
if (anArray[i] == 6 )
{
left();
}
if (anArray[i] == 3 )

    {
      right();
    }
  }

}
}
void left()
{
Serial.print(anArray[i]);
Serial.print(" LEFT ");
}
void right()
{
Serial.print(anArray[i]);
Serial.print(" RIGHT ");
}

Why have you got two arrays called anArray?
That second one isn't going to be of much use, because of its scope.

Please remember to use code tags when posting code

Please edit your post to add code tags, so that it looks like this:

int anArray[200];
byte arrayIndex = 0;
anArray[arrayIndex] = inches;
arrayIndex++;

If you think the posted code through (always a good idea), the only element of the local variable "anArray[]" that will ever have a defined value is anArray[0].

There are many other problems with the code. Go through it line by line to find them all.

How do I use code tags? Sorry this is my first post.

I got the code on line to test the Ultrasonic

Nope.

1. Copy the codes/sketch of your IDE (select and then copy).
2. Click on </> of the Toolbar of this window; as a result, the following appear:

type or paste code here

3. Put the cursor at the left of the word "type" of Step-2 and delete all the words at the right.
4. Now press Ctrl-V.
5. Chech that a nicely formatted sketch like below has appeared in the space between Step-2 and Step-3.

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.