hi, I'm having a problem in interphasing IR distance sensor with motorshield. This is my programming. Please help me. tq
int IR_MIDDLE = A0; // Sensor is connected to the analog A0
int IR_LEFT = A1;
int IR_RIGHT = A2;
int intSensorResult = 0; //Sensor result
float val1 = 0; //Calculated value
float val2 = 0;
int middlestat = 0; // variable for reading the pushbutton status
int E1 = 3; // shield motor
int M1 = 2;
int E2 = 4;
int M2 = 5;
void setup()
{
Serial.begin(9600); // Setup communication with computer to present results serial monitor
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT); // start the serial port
pinMode(IR_MIDDLE, INPUT);
//pinMode(IR_LEFT, INPUT);
// pinMode(IR_RIGHT, INPUT);
}
void loop()
{
// read the value from the ir sensor
intSensorResult = analogRead(IR_LEFT); //Get sensor value
intSensorResult = analogRead(IR_RIGHT);
val1 = (6787.0 / (intSensorResult - 5.0)) - 4.0; //Calculate distance in cm
val2 = (6787.0 / (intSensorResult - 5.0)) - 4.0;
middlestat = digitalRead(IR_MIDDLE);
if (val1 <35 && val2<35 && middlestat==LOW ) //straight
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
else if (val1 >35 )//turn left
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
else if (val2 >35 )//turn right
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 255);
}
else if (middlestat==HIGH )// stop
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0);
}
Serial.print(val1 && val2); //Send distance to computer
Serial.println(" cm"); //Add cm to result
delay(1000); //Wait
}