Ok, so I have this project and this code.
//Front
int trigPinFront = 2;
int echoPinFront = 3;
//Left
int trigPinLeft = 4;
int echoPinLeft = 5;
//Right
int trigPinRight = 8;
int echoPinRight = 9;
/*Back
int trigPinBack = 6;
int echoPinBack = 7;
*/
//BUTTONS
int sensorValCW = 0;
int sensorValCCW = 0;
int sensorValGO = 0;
// defines variables
long durationFront;
int distanceFront;
long durationLeft;
int distanceLeft;
long durationRight;
int distanceRight;
int pinmoding()
{
pinMode(trigPinFront, OUTPUT);
pinMode(echoPinFront, INPUT); // Sets the echoPin as an Input
pinMode(trigPinLeft, OUTPUT);
pinMode(echoPinLeft, INPUT);
pinMode(trigPinRight, OUTPUT);
pinMode(echoPinRight, INPUT);
}
void setup() {
pinmoding();
Serial.begin(9600);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
sensorValCW = digitalRead(12);
sensorValCCW = digitalRead(10);
sensorValGO = digitalRead(11);
Serial.println(sensorValGO);
Serial.println(sensorValCW);
Serial.println(sensorValCCW);
}
void loop() {
pinmoding();
digitalWrite(trigPinFront, LOW);
delayMicroseconds(2);
digitalWrite(trigPinFront, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinFront, LOW);
durationFront = pulseIn(echoPinFront, HIGH);
distanceFront = durationFront*0.034/2;
digitalWrite(trigPinLeft, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeft, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeft, LOW);
durationLeft = pulseIn(echoPinLeft, HIGH);
distanceLeft= durationLeft*0.034/2;
digitalWrite(trigPinRight, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRight, LOW);
durationRight = pulseIn(echoPinRight, HIGH);
distanceRight = durationRight*0.034/2;
if(sensorValGO == HIGH)
{
Serial.println("GO FORWARD!!!");
delay(100);
}
if(sensorValCCW == HIGH)
{
//turn CCW
//Front
trigPinFront = 4;
echoPinFront = 5;
//Left
trigPinLeft = 6;
echoPinLeft = 7;
//Right
trigPinRight = 2;
echoPinRight = 3;
/*Back
trigPinBack = 8;
echoPinBack = 9;
*/
Serial.print("DistanceFront: ");
Serial.println(distanceFront);
Serial.print("DistanceRight: ");
Serial.println(distanceRight);
Serial.print("DistanceLeft: ");
Serial.println(distanceLeft);
}
if(sensorValCW == HIGH)
{//turn CW
//Front
trigPinFront = 8;
echoPinFront = 9;
//Left
trigPinLeft = 2;
echoPinLeft = 3;
//Right
trigPinRight = 6;
echoPinRight = 7;
/*Back
trigPinBack = 8;
echoPinBack = 9;
*/
Serial.print("DistanceFront: ");
Serial.println(distanceFront);
Serial.print("DistanceRight: ");
Serial.println(distanceRight);
Serial.print("DistanceLeft: ");
Serial.println(distanceLeft);
}
}
for example here i have 4 ultrasonic sensors
Front:10 cm;
Left:11 cm;
Back:12 cm;
Right:13 cm;
and i try to "change them in places" by pressing a button and it should be like this first time i press a button it changes in places CW and it should give me this
Front:11 cm;
Left:13 cm;
Right:10 cm;
Back:12 cm;
if i press the button once more
Front:13 cm;
Left:12 cm;
Right:11 cm;
Back:10 cm;
10 11 13
F L B
11L R12 13B F10 12R L11
R R F
13 12 10
i think you understood...syntax is correct,but idk why it is not working pleeeease help me!!!!