Ok, here is all the code. Forgot to note that I am also controlling 3 vibration motors at different intensities depending on the data they get from their correlating sensor.
const int anPin = A0;
long anVolt, inches, cm;
int cmAway1, cmAway2, cmAway3, cmAway4, cmAway5;
int sum=0;//Create sum variable so it can be averaged
int avgrange=60;//Quantity of values to average (sample size)
void setup() {
Serial.begin(9600);
}
void loop() {
const int anPin = A0;
//MaxSonar Analog reads are known to be very sensitive. See the Arduino forum for more information.
//A simple fix is to average out a sample of n readings to get a more consistant reading.\\
//Even with averaging I still find it to be less accurate than the pw method.\\
//This loop gets 60 reads and averages them
for(int i = 0; i < avgrange ; i++)
{
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
//Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
anVolt = analogRead(anPin)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cmAway1 = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cmAway1);
Serial.print("cm");
Serial.println();
//reset sample total
sum = 0;
delay(500);
const int motorPin1 = 7;
pinMode (motorPin1, OUTPUT);
if (cmAway1 >= 200)
{
analogWrite(motorPin1, 0);
}
if(cmAway1 > 150 & cmAway1 < 199)
{
analogWrite(motorPin1, 64);
}
if(cmAway1 > 100 & cmAway1 < 149)
{
analogWrite(motorPin1, 127);
}
if(cmAway1 > 50 & cmAway1 < 99)
{
analogWrite(motorPin1, 191);
}
if(cmAway1 > 0 & cmAway1 < 49)
{
analogWrite(motorPin1, 255);
}
const int an2Pin = A3;
for(int i = 0; i < avgrange ; i++)
{
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
//Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
anVolt = analogRead(anPin)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cmAway2 = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cmAway2);
Serial.print("cm");
Serial.println();
//reset sample total
sum = 0;
delay(500);
const int motorPin2 = 3;
pinMode (motorPin2, OUTPUT);
if (cmAway2 >= 200)
{
analogWrite(motorPin2, 0);
}
if(cmAway2 > 150 & cmAway2 < 199)
{
analogWrite(motorPin2, 64);
}
if(cmAway2 > 100 & cmAway2 < 149)
{
analogWrite(motorPin2, 127);
}
if(cmAway2 > 50 & cmAway2 < 99)
{
analogWrite(motorPin2, 191);
}
if(cmAway2 > 0 & cmAway2 < 9)
{
analogWrite(motorPin2, 255);
}
const int an3Pin = A5;
for(int i = 0; i < avgrange ; i++)
{
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
//Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
anVolt = analogRead(anPin)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cmAway3 = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cmAway3);
Serial.print("cm");
Serial.println();
//reset sample total
sum = 0;
delay(500);
const int motorPin3 = 5;
pinMode (motorPin3, OUTPUT);
if (cmAway3 >= 200)
{
analogWrite(motorPin3, 0);
}
if(cmAway3 > 150 & cmAway3 < 199)
{
analogWrite(motorPin3, 64);
}
if(cmAway3 > 100 & cmAway3 < 149)
{
analogWrite(motorPin3, 127);
}
if(cmAway3 > 50 & cmAway3 < 99)
{
analogWrite(motorPin3, 191);
}
if(cmAway3 > 0 & cmAway3 < 49)
{
analogWrite(motorPin3, 255);
}
const int an4Pin = A3;
for(int i = 0; i < avgrange ; i++)
{
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
//Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
anVolt = analogRead(anPin)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cmAway4 = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cmAway4);
Serial.print("cm");
Serial.println();
//reset sample total
sum = 0;
delay(500);
const int motorPin4 = 9;
pinMode (motorPin4, OUTPUT);
if (cmAway4 >= 200)
{
analogWrite(motorPin4, 0);
}
if(cmAway4 > 150 & cmAway4 < 199)
{
analogWrite(motorPin4, 64);
}
if(cmAway4 > 100 & cmAway4 < 149)
{
analogWrite(motorPin4, 127);
}
if(cmAway4 > 50 & cmAway4 < 99)
{
analogWrite(motorPin4, 191);
}
if(cmAway4 > 0 & cmAway4 < 49)
{
analogWrite(motorPin4, 255);
}
const int an5Pin = A4;
for(int i = 0; i < avgrange ; i++)
{
//Used to read in the analog voltage output that is being sent by the MaxSonar device.
//Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
//Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
anVolt = analogRead(anPin)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cmAway5 = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cmAway5);
Serial.print("cm");
Serial.println();
//reset sample total
sum = 0;
delay(500);
const int motorPin5 = 10;
pinMode (motorPin5, OUTPUT);
if (cmAway5 >= 200)
{
analogWrite(motorPin5, 0);
}
if(cmAway5 > 150 & cmAway5 < 199)
{
analogWrite(motorPin5, 64);
}
if(cmAway5 > 100 & cmAway5 < 149)
{
analogWrite(motorPin5, 127);
}
if(cmAway5 > 50 & cmAway5 < 99)
{
analogWrite(motorPin5, 191);
}
if(cmAway5 > 0 & cmAway5 < 49)
{
analogWrite(motorPin5, 255);
}
}
What I meant by "not getting the correct readings" was that the serial monitor usually displayed a number too high than the actual distance. Sorry for the confusion.