Storing Values and Using them to decide which way to go
Page 1 of 1

Author:  lilluckybro [ Tue May 13, 2008 6:36 pm ]
Post subject:  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

Author:  Miklos Szeles [ Thu May 15, 2008 2:15 pm ]
Post subject: 


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:

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;
     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.


Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group