I would like to know if i can still operate the ultrasonic sensor in the switch case if the reading of the ultrasonic sensor is used as the condition for the switch case. Because currently inside the switch case the ultrasonic sensor reading is stuck on the switch case condition and as such it cannot record any distance readings.
I would just like to know if there is a method to reactivate the ultrasonic sensor once it is in the switch case.
const int echoPin = A3; // attach pin D2 Arduino to pin Echo of HC-SR04
const int trigPin = A6; //attach pin D3 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int WallLength;
int state = 0;
int InitialServoAngle = 110;
int pos;
int sensor1 = A7;
int sensor2 = A2;
int state1;
int state2;
void setup()
{
Serial.begin(9600); // Open serial monitor
controller.begin(); // Initialize battery charge
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
servo4.setAngle(InitialServoAngle);
}
void loop() {
state1 = analogRead(sensor1);
state2 = analogRead(sensor2);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back
WallLength = duration * 0.034 / 2;
switch(distance)
{
case 2 : ////The rover needs to stop at 3cm to be able to pickup the box
M1.setDuty(0);
M2.setDuty(0);
delay(1000);
servo4.setAngle(InitialServoAngle +10 );
delay(100);
servo4.setAngle(InitialServoAngle +20 );
delay(100);
servo4.setAngle(InitialServoAngle +30 );
delay(100);
servo4.setAngle(InitialServoAngle +40 );
delay(100);
servo4.setAngle(InitialServoAngle +50 );
delay(100);
servo4.setAngle(InitialServoAngle +60 );
delay(100);
servo4.setAngle(InitialServoAngle +65 );
delay(2000);-
Serial.println(distance);
delay(2000);
Serial.println(WallLength);
delay(20000);
if (WallLength <= 10) //////THIS IS WHERE I WOULD LIKE TO USE THE ULTRASONIC SENSOR READING
{
M1.setDuty(0);
M2.setDuty(0);
delay(2000);
servo4.setAngle(InitialServoAngle +65 -10 );
delay(100);
servo4.setAngle(InitialServoAngle +65 -20 );
delay(100);
servo4.setAngle(InitialServoAngle +65 -30 );
delay(100);
servo4.setAngle(InitialServoAngle +65 -40 );
delay(100);
servo4.setAngle(InitialServoAngle +65 -50 );
delay(100);
servo4.setAngle(InitialServoAngle +65 -60 );
delay(100);
servo4.setAngle(InitialServoAngle +65 -65 );
delay(100);
delay(2000);
M1.setDuty(22);
M2.setDuty(-19);
delay(2000);
M1.setDuty(0);
M2.setDuty(0);
delay(1000);
}
else if (WallLength > 10)
{
M1.setDuty(-20);
M2.setDuty(20);
}
break;
default :
servo4.setAngle(InitialServoAngle);
if ((state1 < 100) && (state2 < 100))
{
M1.setDuty(-19);
M2.setDuty(19);
} //IF BOTH WHEELS ARE ON THE WHITE
if ((state1 < 100) && (state2 > 100))
{
M1.setDuty(-24);
M2.setDuty(-20);
} //IF THE LEFT WHEEEL IS ON THE BLACK
if ((state1 > 100) && (state2 < 100))
{
M1.setDuty(20);
M2.setDuty(24);
} //IF THE RIGHT WHEEL IS ON THE BLACK
break;
}
}
That will not work. Remember that the case variable is a float. You need to use greater than (>),less than (>) etc.
The main approach, reading the sensors first and then deciding upon values is perfect as I can see!
You use delay. Get rid of them. They create lots of problems for the flow of the rest of the program.
I don't understand why You want to reread the distance some microseconds later.
Before pressing any key on the keyboard making code a plan should be made. One kind is named "flowcharts". Just pressing keys ends up in situations like You have. You have no overview.
Code hangs up....... Debugging Serial.print and serial monitor are excellent tools for finding out what's going on....
This sounds like a recent post using another title.
"Pick up a box"....... Who will understand anything from that?
Loop is running, You read the sensors and act upon that. Then, reading the ultrasonic again is confusing. The next run in loop will read that again.
I would use the basic case to control the steps of action.
Search object
Pick up object etc..
Inside those cases different actions are done.
You need to get another reading from the sensor. To do that, the following code needs to be re-run, I suggest putting it in a function and calling that when you need a new measurement to be taken.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back