View unanswered posts | View active topics It is currently Wed May 22, 2013 12:54 am






Reply to topic  [ 2 posts ] 
Using a while loop with Sonar Sensor (before 21/10/2011) 
Author Message
Rookie

Joined: Tue Mar 01, 2011 6:16 pm
Posts: 21
Post Using a while loop with Sonar Sensor (before 21/10/2011)
Hy, thats the code i want the robot to do.
Code:

#pragma config(Sensor, S2,     LightSensor,         sensorLightActive)
#pragma config(Sensor, S3,     sonarSensor,         sensorSONAR)
#pragma config(Motor,  motorA,          a,             tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          b,             tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          c,             tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main(){
  motor[a]= 0;
  motor[b]= 30;
  motor[c]= 40;
  wait10Msec(810);


   while(true)
   {
      if(SensorValue(LightSensor) > 33)
      {
motor[a]= 0;
motor[b]= 10;
motor[c]= 15;
      }

      else
      {
motor[a]= 0;
motor[b]= -15;
motor[c]= -10;
wait10Msec(190);
  motor[a]= 80;
  motor[b]= 0;
  motor[c]= 0;
  wait10Msec(450);
   motor[a]= 0;
  motor[b]= 10;
  motor[c]= 15;
  wait10Msec(100);
    motor[a]= -80;
  motor[b]= 0;
  motor[c]= 0;
  wait10Msec(450);

      }
   }
}


But that code its a loop so when it begans it doesn't stop. What i want to know, is the code for make the robot stop or go backwards, when it reaches a distance of 10 cm from the wall. Please i need that answer before friday 21/10/2011! thanks.


Wed Oct 19, 2011 2:03 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:44 am
Posts: 421
Location: Pittsburgh, PA
Post Re: Using a while loop with Sonar Sensor (before 21/10/2011)
If you want the condition to check for a distance away from the wall, you want to use the "sonarSensor" in the condition.

So change this:
Code:
if(SensorValue(LightSensor) > 33)


To this:
Code:
if(SensorValue(sonarSensor) > 10)


Notice that I changed 33 to 10. You specified you wanted to set a distance of 10cm away from the wall.

_________________
Vu Nguyen
Software Training Development Team | Webmaster
Need more support? Use the ROBOTC Ticketing system

Robotc.net| Robomatter Store | Robotics Academy


Thu Oct 20, 2011 9:13 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.