Hi, for my project I am trying to get readings from 5 ultrasonic range finders and use that information to change the speed of five vibration motors (each sensor is paired with one motor). However, nothing was working. So I tried to put in some code to send the readings of the sensors back to the computer but it just kept saying 0cm. Here is my code:
const int anPin = A0;
long anVolt, inches, cm;
int cmAway1, cmAway2, cmAway3, cmAway4, cmAway5;
void setup() {
Serial.begin(9600);
}
void loop() {
pinMode(anPin, INPUT);
anVolt = analogRead(anPin)/2;
inches = anVolt;
cm = inches * 2.54;
cm = (cmAway1);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(400);
const int motorPin1 = 3;
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 = A1;
pinMode(an2Pin, INPUT);
anVolt = analogRead(an2Pin)/2;
inches = anVolt;
cm = inches * 2.54;
cm = (cmAway2);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(400);
const int motorPin2 = 5;
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 = A2;
pinMode(an3Pin, INPUT);
anVolt = analogRead(an3Pin)/2;
inches = anVolt;
cm = inches * 2.54;
cm = (cmAway3);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(400);
const int motorPin3 = 6;
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;
pinMode(an4Pin, INPUT);
anVolt = analogRead(an4Pin)/2;
//147uS per inch
inches = anVolt;
//change inches to centimetres
cm = inches * 2.54;
cm = (cmAway4);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(400);
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;
pinMode(an5Pin, INPUT);
anVolt = analogRead(an5Pin)/2;
inches = anVolt;
cm = inches * 2.54;
cm = (cmAway5);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(400);
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);
}
}
I am a beginner in electronics and programming so I don't know to much about the arduino language. Any help would be greatly appreciated. Thanks!