void setup() { Serial.begin(115200); // set up Serial library at 9600 bps Serial.println("C'est Partit!");
motor2.setSpeed(110); // set the speed to 200/255 motor1.setSpeed(125);
servo.attach(10); servo.write(90); delay(50); } void loop (){ unsigned int uS = sonar.ping(); Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println("cm");
else //if path is blocked { motor1.run (RELEASE); motor2.run (RELEASE); servo.write(180); delay(500); unsigned int left = sonar.ping(); servo.write(10); delay(500); unsigned int right = sonar.ping(); servo.write(90); } if(left < dangerDist){ motor1.run(FORWARD); delay(turnTime); } if(left > dangerDist){ motor2.run(FORWARD); delay(turnTime); } }
I would like to have my IR sensor turning left and right with my servo collecting data from what he saw left and right and judge if its over the danger dist (established at 10cm). The problem is I cant get the variable ''right'' or ''left'' to be attached to the ping value the IR saw on either left or right side of the robot.
You have to make sure that you use the right sensor with the right code. Ping is for ultrasound, like the SF-04 and analog read for IR sensors like the Sharp GD**** The below code is a snipped from my Chopstick Junior and using the Sharp IR sensor.
void sweep()
{ for(pos = 20; pos < 160; pos += 1) // goes from 20 degrees to 160 degrees
{ // in steps of 1 degree
eye.write(pos); // tell servo to go to position in variable ‘pos’ delay(10); // waits 10ms for the servo to reach the position
distance = analogRead(sensor); //Serial.print(distance); //Serial.print(" - Position “); //Serial.print(pos); //Serial.println();
} for(pos = 160; pos >= 20; pos -= 1) // goes from 160 degrees to 20 degrees
{
eye.write(pos); // tell servo to go to position in variable ‘pos’ delay(10); // waits 10ms for the servo to reach the position
distance = analogRead(sensor); //Serial.print(distance); //Serial.print(” - Position "); //Serial.print(pos); //Serial.println();
}
}
The exact model for my ultrasonic ranging sensor is HC-SR04
Im not too sure how to declare those two value since they will be fluctuating all the time. They will vary from 0 to the maximum range value. I will try to read on that segment of my code.
For the delay part in my void setup I added that part to have time to place the power then place the shell of my robot over my electronic. I dont have a on off switch so its kind of sketchy for now. I should add a photo to it to make it clearer for you guys.
I have modify my code to make it look like the robot ‘‘Penny’’ from ignoblegnome. It is adapted to my robot. Everything seems to function except the nodanger void or all forward. The ping is collected and the value is greater then the dangerlevel value but it seems to stay in place and scan left and right for ever. So I might need your help again on this one guys. There is also the debug fonction in the loop and I cant understand the purpose of it.
NewPing sonar(TRIGGER_PIN, ECHO_PIN); Servo headservo; // creates servo object int dangerlevel = 600; // how far away should things be, before we react? int turn = 300; // this sets how much we should turn int servo_turn = 700; //
int currDist = 0; // holds the current distance int compDist = 0; // holds a distance to compare currDist to
void setup(){ Serial.begin(9600); Serial.println(“C’est Partit”); motor2.setSpeed(110); // set the speed to 200/255 motor1.setSpeed(125); headservo.attach(10); headservo.write(70); delay(15000); } void loop() { currDist = sonar.ping(); Serial.print("Ping: "); Serial.print(currDist / US_ROUNDTRIP_CM); Serial.println(“cm”); if(currDist < dangerlevel) { nodanger(); // if no danger, drive ahead } else { whichway(); // if obstacle ahead then decide with way is better } // delay(500); if(DEBUG){ Serial.print("Current Distance: "); Serial.println(currDist); } }