The ptFront variable is linked to a QT circuit on my parallax propeller board. When it sees phototransistor sees a white wall the value drops to under 18000, when it sees anything besides white it increases to above 20000-50000. When the argument of the while loop becomes invalid it will still run the loop. I confirmed this by hooking up an LED circuit connected to pin 6. It never exits. So why doesn't it exit? I need it to be able to interact with things that it recognizes as not being a wall.
#include "simpletools.h"
#include "abdrive360.h"
#include "ping.h"
int irLeft, irRight, ptFloor, ptFront; // IR variables
int main() {
high(9);
high(8);
low(26);
low(27);
low(6);
drive_setRampStep(12);
while (1) {
ptFront = rc_time(9, 1);
ptFloor = rc_time(8, 1);
freqout(11, 1, 38000); // Check left & right objects
irLeft = input(10);
freqout(1, 1, 38000);
irRight = input(2);
// Roaming and Obstacle Avoidance
if (irRight == 1 && irLeft == 1) { // No obstacles?
drive_rampStep(35, 35);
} else if (irLeft == 0 && irRight == 0 && ptFront > 18000) { // Left & right obstacles and it's not the wall?
print("oh robot!");
robotFight();
} else if ((irLeft==0||irRight==0)&& ptFront < 18000) { // Left & right obstacles and is the wall?
trajAdjust();
} else if (ptFloor <= 10000) { // In the pit?
pit();
}
}
}
void trajAdjust() {
while (ptFront <= 18000) {
// Implement trajectory adjustment logic here
// This could involve making a turn to avoid the obstacle (wall)
high(9);
high(6);
freqout(11, 1, 38000); // Check left & right objects
irLeft = input(10);
freqout(1, 1, 38000);
irRight = input(2);
ptFront = rc_time(9, 1); // Update ptFront inside the loop
print("ptFront= %d \n",ptFront);
if (irLeft == 0 && irRight == 1) {
// Only the left sensor detects an obstacle, turn right
drive_rampStep(35, -35);
print("left wall \n");
} else if (irLeft == 1 && irRight == 0) {
// Only the right sensor detects an obstacle, turn left
drive_rampStep(-35, 35);
print("right wall \n");
} else if (irLeft == 0 && irRight == 0) {
// Both sensors detect an obstacle
// Prioritize turning in one direction
drive_rampStep(35, -35); // Example adjustment, you may need to fine-tune
} else {
// No obstacle in front, or only one sensor detects an obstacle
// Continue straight or make other adjustments as needed
drive_rampStep(35, 35);
}
}
}
void pit() {
//pit logic
}
void robotFight() {
while (ptFront > 18000 && irRight == 0 && irLeft == 0) {
// Measure the distance using the ultrasonic sensor
int range = ping_cm(4);
// Print the distance to the terminal (optional)
print("Distance to target: %d cm\n", range);
// Define the desired range boundaries (5-8 cm)
int minRange = 5;
int maxRange = 8;
if (range > maxRange) {
drive_rampStep(35, 35);
} else if (range < minRange && range > 5) {
drive_rampStep(-35, -35);
} else if (range >= minRange && range <= maxRange) {
drive_rampStep(0, 0);
}
}
}
Your problem is right in the first line of your post, because "The ptFront variable is linked to a QT circuit on my parallax propeller board." is not true and that assumption puts you off the track.
The variable
ptFrontis not special, it is defined hereint irLeft, irRight, ptFloor, ptFront; // IR variables(without initialisation)initialised here, along with a very similar one
and cyclically updated here
ptFront = rc_time(9, 1); // Update ptFront inside the loopwith an insightful comment, inside one of the two loops which evaluate this variable in their condition.The other loop doing so does not contain that necessary update of the normal variable within its body and I am assuming that it is the loop which never exits.
Insert the cyclic update of the evaluated variable inside the body of the failing loop and you should be set.