View unanswered posts | View active topics It is currently Mon Sep 01, 2014 7:22 am






Reply to topic  [ 3 posts ] 
Maze Help for PIC 
Author Message
Rookie

Joined: Mon Apr 29, 2013 7:30 am
Posts: 2
Post Maze Help for PIC
Hello All,

I am new to programming with RobotC and am working with a student to figure out what is going on with his maze. We are using RobotC 3.60 to program our V.5 PIC. Our problem is this: We are wanting to use the bump sensors to bump a wall, backup and make a turn, continue driving straight, then repeat the process. The only variable would be which direction the robot turns when it bumps the wall. We like the idea of having a variable (turns) in order to keep track of where he is at in the maze. I would like to know if there is anything obvious that stands out in his code right of the bat. The code is not complete, we were using it for testing purposes. I will attach it below. I'd prefer to be able to continue with his own code, but will accept any other suggestions or approaches. Thanks!

#pragma config(Sensor, in16, bumper, sensorTouch)
#pragma config(Motor, port1, leftmotor, tmotorVex393, openLoop)
#pragma config(Motor, port2, rightmotor, tmotorVex393, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
int turns = 0;

while (turns == 0)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
turns++;
}

while (turns == 1)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = -127;
motor[port2] = 127;
wait1Msec(1000);
turns++;

}

while (turns == 2)
{
motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
turns++;
}


}


Attachments:
File comment: code
robotc_pic_maze.c [1.2 KiB]
Downloaded 160 times
File comment: Maze Layout
maze_layout.pdf [125.08 KiB]
Downloaded 174 times
Mon Apr 29, 2013 7:48 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 566
Post Re: Maze Help for PIC
Code:
if (SensorValue(bumper) == 1)
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);


This code may be part of the problem. Remember, your if statements, while loops, etc should use the braces to encompass they code they want to control. In this example, only the first line (motor[port1] = -127;) is being controlled by the if statement; the rest of the code will run regardless of the state of the bumper switch. Instead, try this:

Code:
if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
}


This will run that code segment if and only if the value of the 'bumper' sensor is equal to 1. You may need to make these changes throughout the rest of the program as well.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Tue May 21, 2013 9:51 am
Profile
Rookie

Joined: Mon Apr 29, 2013 7:30 am
Posts: 2
Post Re: Maze Help for PIC
OK. Thank You! That got me further, but I am still having an issue. Now if I run the code with the cable still connected and the debugger open, it works great. Then, if I unplug and turn the robot off and on, it runs through the while loop and the first if statement before the sensor is pressed, which throws off my count. Shouldn't it run through the while loop continuing to drive forward until the sensor is pressed the first time, then run through the first if, which advances the count? I am including my updated code.

Thanks Again.

#pragma config(Sensor, in16, bumper, sensorTouch)
#pragma config(Motor, port1, leftmotor, tmotorVex393, openLoop)
#pragma config(Motor, port2, rightmotor, tmotorVex393, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
int turns = 0;


while (turns == 0)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 127;
motor[port2] = -127;
wait1Msec(1000);
turns++;
}
}

while (turns == 1)
{

motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = -127;
motor[port2] = 127;
wait1Msec(1000);
turns++;
}


}

while (turns == 2)
{
motor[port1] = 127;
motor[port2] = 127;

if (SensorValue(bumper) == 1)
{
motor[port1] = -127;
motor[port2] = -127;
wait1Msec(1000);
motor[port1] = 0;
motor[port2] = 0;
turns = 0;
}

}


}


Attachments:
robot_pic_maze.c [1014 Bytes]
Downloaded 148 times
Wed May 22, 2013 3:47 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.