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 ");
}