View unanswered posts | View active topics It is currently Tue Sep 02, 2014 5:00 pm






Reply to topic  [ 2 posts ] 
Storing Values and Using them to decide which way to go 
Author Message
Rookie

Joined: Tue May 13, 2008 6:26 pm
Posts: 1
Post Storing Values and Using them to decide which way to go
Ok, for my robotics class I currently have an assignment to stop 3 cm from a wall and have the head turn left, store the value, and then turn its head right and store the value, and then pick the way to go. I have already accomplished the turning the head but I can not figure out how to get it to get the value and store it.

If anyone can please post a code in which you [b]store a vaue[/b] would be very helpful. I would be even more appreciative if anyone can post a code storign values with a Sonar Sensor usign Sonar 9V or deciding which way to go based on which way is closer. Thank You


Tue May 13, 2008 6:36 pm
Profile
Rookie

Joined: Sat May 10, 2008 3:41 pm
Posts: 5
Post 
Hi!

First of all you should add the sensor in Robot/Motors and sensors menu. I read in the documentation that the S4 is best for the Sonar sensor, so make sure you connect it to the appropriate place. You should write a name for the sonar for example sonarSensor and select the sonar as type.
You can write something like this:

Code:
const int LEFT = 0;
const int RIGHT = 1;

int leftDistance;
int rightDistance;

int whichWayToGo() {
        //turn left
   leftDistance = SensorValue[sonarSensor];
   //turn right
   rightDistance = SensorValue[sonarSensor];
   if (leftDistance < rightDistance)
     return LEFT;
   else
     return RIGHT;
}


task main() {
   if (whichWayToGo() == LEFT) {
      //Go left
   } else {
      //Go right
   }
}


Here rightDistance and leftDistance are global variables, so you can read/write their values from anywhere in your code. If you don't need the measured distances, you can declare leftDistance and rightDistance in the whichWayToGo function. You have to write your turning/moving codes to the appropriate places.

BR,
Miki


Thu May 15, 2008 2:15 pm
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.