I have 3 HCsro4 ultra sonic senors attached to a arduino atMega with 4 motors running off motor controllers im having issues with this piece of code
else if (CM2 < 30 && CM2 > 3)
{
LPCOUNT =7 ;
BackLeft.writeMicroseconds (1600);
BackRight.writeMicroseconds (1400);
FrontRight.writeMicroseconds (1400);
FrontLeft.writeMicroseconds (1600);
unsigned int CM1 = sonar.ping_cm();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
it constantly find this statement true heres the rest
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 53
#define ECHO_PIN 52
#define MAX_DISTANCE 400
#define TRIGGER_PIN2 50
#define ECHO_PIN2 51
#define MAX_DISTANCE2 400
#define TRIGGER_PIN3 49
#define ECHO_PIN3 48
#define MAX_DISTANCE3 400
int LPCOUNT = 0;
Servo BackLeft;
Servo BackRight;
Servo FrontLeft;
Servo FrontRight;
NewPing sonar (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // SETUP for 1st ultrasonic sensor
NewPing sonar2 (TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);// SETUP for 1st ultrasonic sensor
NewPing sonar3 (TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE3);
void setup()
{
BackLeft.attach (2); // attach all of the servos to a pin on the board
BackRight.attach (3);
FrontRight.attach(4);
FrontLeft.attach (5);
}
void loop ()
{
unsigned int CM1 = sonar.ping_cm();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
if ( CM1 > 1 && CM1 < 20 && LPCOUNT== 5)
{
BackLeft.writeMicroseconds (1600);
BackRight.writeMicroseconds (1400);
FrontRight.writeMicroseconds (1600);
FrontLeft.writeMicroseconds (1400);
delay (500);
BackLeft.writeMicroseconds (1550);
BackRight.writeMicroseconds (1550);
FrontRight.writeMicroseconds (1550);
FrontLeft.writeMicroseconds (1550);
unsigned int CM1 = sonar.ping();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
}
else if ( CM1 > 30 && LPCOUNT == 5)
{
BackLeft.writeMicroseconds (1600);
BackRight.writeMicroseconds (1600);
FrontRight.writeMicroseconds (1600);
FrontLeft.writeMicroseconds (1600);
unsigned int CM1 = sonar.ping_cm();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
}
else if (CM2 < 30 && CM2 > 3)
{
LPCOUNT =7 ;
BackLeft.writeMicroseconds (1600);
BackRight.writeMicroseconds (1400);
FrontRight.writeMicroseconds (1400);
FrontLeft.writeMicroseconds (1600);
unsigned int CM1 = sonar.ping_cm();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
}
else if (CM2 > 30)
{
LPCOUNT= 5;
}
else if (CM3 > 40)
{
LPCOUNT = 5;
}
else if (CM2 < 32)
{
LPCOUNT= 5;
}
else if (CM3 < 30 && CM3 > 2)
{
LPCOUNT= 7;
BackLeft.writeMicroseconds (1400);
BackRight.writeMicroseconds (1600);
FrontRight.writeMicroseconds (1600);
FrontLeft.writeMicroseconds (1400);
unsigned int CM1 = sonar.ping_cm();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
}
else if (CM2 > 30)
{
BackLeft.writeMicroseconds (1600);
BackRight.writeMicroseconds (1600);
FrontRight.writeMicroseconds (1600);
FrontLeft.writeMicroseconds (1600);
}
else
{
BackLeft.writeMicroseconds (1600);
BackRight.writeMicroseconds (1600);
FrontRight.writeMicroseconds (1600);
FrontLeft.writeMicroseconds (1600);
unsigned int CM1 = sonar.ping_cm();
delay (50);
unsigned int CM2 = sonar2.ping_cm();
delay (50);
unsigned int CM3 = sonar3.ping_cm();
delay (50);
}
}
any help would be appreciated