View unanswered posts | View active topics It is currently Fri Oct 30, 2020 10:11 pm

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Could someone look at this code?
Author Message
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 16
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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 2 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning