Quote:
Originally Posted by Spacemonkey619
Yes I did put the while(true) brackets around the entire code, still it only moved the arm and line tracked forward forever. The code above me only went backwards forever. I can't believe these small problems sometimes.
|
This should do more what you want maybe? Wait untill the button is pressed, move arm, follow line, move stuff around, reverse on line, wait for button again.
I also changed the encoders to an absulate value comparison so it wont matter if you have the tick calculations wrong.
Code:
#pragma config(Sensor, in1, pot, sensorPotentiometer)
#pragma config(Sensor, in3, LineLeft, sensorLineFollower)
#pragma config(Sensor, in4, LineRight, sensorLineFollower)
#pragma config(Sensor, in5, LineCenter, sensorLineFollower)
#pragma config(Sensor, dgtl1, touch, sensorTouch)
#pragma config(Sensor, dgtl2, Encoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, Encoder2, sensorQuadEncoder)
#pragma config(Motor, port2, arm, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, elbow, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port7, rightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port8, leftMotor, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
int threshold = 2000;
void lineFollower(int speed);
void stopAllMotors();
task main()
{
while(true)
{
if(SensorValue(touch) == 0)
{
stopAllMotors();
}
if(SensorValue(touch) == 1)
{
motor[elbow] = -63;
wait1Msec(1500);
motor[elbow] = 0;
SensorValue[Encoder] = 0;
while(abs(SensorValue[Encoder]) < 210)
{
lineFollower(60);
}
motor[arm] = 63; // Brackets were wraped around this, is there suposed to be a loop or somthing here?
wait1Msec(2000);
motor[elbow] = 63;
wait1Msec(200);
motor[rightMotor] = 0;
motor[leftMotor] = 0;
wait1Msec(1000);
motor[arm] = -63;
wait1Msec(1800);
motor[elbow] = -63;
wait1Msec(2500);
motor[elbow] = 0;
wait1Msec(1000);
motor[rightMotor] = -43;
motor[leftMotor] = -43;
wait1Msec(500);
SensorValue[Encoder] = 0;
while(abs(SensorValue[Encoder]) < 450)
{
lineFollower(-60);
}
}
}
}
void lineFollower(int speed) // Can help clean up your code and make it easier to read
{
if(SensorValue(LineRight) > threshold && SensorValue(LineCenter) > threshold)
{ // Added a slight turn to have a bit of a gentler correction when the robot is not that far off the line
motor[leftMotor] = speed;
motor[rightMotor] = (speed / 2);
}
else if(SensorValue(LineLeft) > threshold && SensorValue(LineCenter) > threshold)
{
motor[leftMotor] = 0;
motor[rightMotor] = (speed / 2);
}
else if(SensorValue(LineCenter) > threshold)
{
motor[leftMotor] = speed;
motor[rightMotor] = speed;
}
else if(SensorValue(LineRight) > threshold)
{
motor[leftMotor] = speed;
motor[rightMotor] = 0;
}
else if(SensorValue(LineLeft) > threshold)
{
motor[leftMotor] = 0;
motor[rightMotor] = speed;
}
else
{
motor[leftMotor] = 0;
motor[rightMotor] = 0;
}
}
void stopAllMotors()
{
motor[leftMotor] = 0;
motor[rightMotor] = 0;
motor[elbow] = 0;
motor[arm] = 0;
}