This is what I made:
- I will put this robot straight in line with the ball
- I have to make this robot go forward and once it reaches the ball, I have to make it stop and hit the ball with the swing shown in the picture
- I used ultrasonic sensor to determine if it reached the ball
It has to identify if it's a red ball or blue ball with the light sensor. If it's a red ball the robot should hit the ball and if it's a blue ball it shouldn't hit the ball.
I used while and if statements but it doesn't seem to work properly. If I use break; then it stops the program instantly when I start it, so I didn’t use break, but if I start my code, it goes forward and stops and then continue this It just wouldn't go continuously
Here’s the code I wrote:
#include "NXCDefs.h"
task main() {
SetSensorLowspeed(S2);
ClearSensor(S2);
SetSensorType(S1,SENSOR_TYPE_LIGHT_ACTIVE);
SetSensorMode(S1,SENSOR_MODE_PERCENT);
ClearSensor(S1);
while(true) {
OnFwd(OUT_AB,75);
if(SensorUS(S2)<=16) {
OnFwd(OUT_AB,0);
if(Sensor(S1)>49) {
OnFwd(OUT_C,-70);
Wait(2000);
}
else {
Wait(500);
}
}
}
}