View unanswered posts | View active topics It is currently Thu Apr 02, 2015 2:37 am






Reply to topic  [ 2 posts ] 
Could someone look at this code? 
Author Message
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 16
Post Could someone look at this code?
We want our robot to check the sensor reading move forward and check again. If the second sensor value is less than the first, go forward, else go back. The robot moves forward twice and then the program stops so I think the problem may be in the if statement. Do you guys see problems in this code?

USSideDist[USSideDistIndex] = SensorValue(SonarSensor);//sets the current value of the USSensor to the first value in this array
USSideDistIndex = USSideDistIndex + 1;

motor[leftmotor] = 40;
motor[rightmotor] = 40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;

wait1Msec(400);

USSideDist[USSideDistIndex] = SensorValue(SonarSensor);//sets the current value of the USSensor to the first value in this array
USSideDistIndex = USSideDistIndex + 1;

if (USSideDist[USSideDistIndex] < USSideDist[USSideDistIndex - 1])//if the latest sensor reading is a less value than the one before it
{
//move forward
motor[leftmotor] = 40;
motor[rightmotor] = 40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;
}
else
{
//move backward
motor[leftmotor] = -40;
motor[rightmotor] = -40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;
}


Sat Mar 14, 2015 9:47 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1512
Post Re: Could someone look at this code?
It's very hard to understand what your code is trying to do. So I am trying to figure out what you want to do from your description. If I understand correctly, are you trying to use a sonar sensor to detect whether you are aligned with the center goal? It looks like you are mounting the sonar sensor to the side so you would continue to go forward until the sonar sensor reads a value indicating the center goal is directly to your (left or right) side. You continue to go forward until the sonar sensor passes the center goal and reads a much bigger value, then you go back. If that's the case, you don't need an array. You can do something like the following.
Code:
//
// Go forward until you see the center goal to your side.
//
while (SensorValue[SonarSensor] > SOME_DISTANCE_THRESHOLD)
{
   motor[leftmotor] = 40;
   motor[rightmotor] = 40;
   wait1Msec(400);
}
//
// Continue to go forward until you pass the center goal
//
while (SensorValue[SonarSensor] <= SOME_DISTANCE_THRESHOLD)
{
   motor[leftmotor] = 40;
   motor[rightmotor] = 40;
   wait1Msec(400);
}
//
// Move backward for a set period of time and stop.
//
motor[leftmotor] = -40;
motor[rightmotor] = -40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;


Wed Mar 18, 2015 3:21 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: mightor 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.