It’s me again. I am still using the old cortex and robot C . I finally got the switch to work when pressed, and it goes, but it will not stop. How can we get it to stop without having to push the limit switch again? I know ==1 is pushing it and the motors go, and ==0 stops, but how can we make it loop or go 3 seconds and stop.
Here is my code #pragma config(Sensor, dgtl6, Switch, sensorTouch) #pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//
task main()
{
while(1==1){
if(SensorValue(Switch)==1)
{
motor[port2] = 127; // if pressed runs the motor
}
}
Please if anyone can help. We really want to run our dragsters. It has been a minute since I have used robot C
Here is OP’s code in a more readable format for everyone.
#pragma config(Sensor, dgtl6, Switch, sensorTouch)
#pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//
task main()
{
while(1==1){
if(SensorValue(Switch)==1)
{
motor[port2] = 127; // if pressed runs the motor
}
}
}
You’re going to need to do something like this:
if(SensorValue(Switch)==1)
{
// Wait for a second or two so you have time to stand away from the dragster
motor[port2] = 127;
// Wait for 3 seconds
motor[port2] = 0;
}
I’m giving you the task of using Google (or your search engine of choice) to look for the correct command to make a RobotC program sleep for a certain amount of time. You will need to replace the pseudocode lines with actual code once you look this up.
The motor is told to go 127, but you never told the motor to stop. The motor will never stop until it is explicitly told to stop by the program or the robot turns off.
I believe it would be better for something like the following:
#pragma config(Sensor, dgtl6, Switch, sensorTouch)
#pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//
task main()
{
//Yield the code until the switch sensor is pressed
while(SensorValue(Switch) == 0)
{
wait1Msec(10);
}
motor[port2] = 127; //Go full power
wait1Msec(3000);//Wait 3000 milliseconds (or 3 seconds)
motor[port2] = 0; //Stop
}
if(SensorValue(Switch)==1)
{
motor[port2] = 127; // if pressed runs the motor
}
}
wait1Msec(1000); // robot moves forward for 1 seconds before program ends
motor[port2] = 0; //Stop
}
and it would not stop, but my battery was low. I will try it tomorrow. What is the difference with the == versus the = . Is it the push of the button?
task main()
{
while(1==1){
if(SensorValue(Switch)==1)
{
motor[port2] = 127; // if pressed runs the motor
}
}
wait1Msec(1000); // robot moves forward for 1 seconds before program ends
motor[port2] = 0; //Stop
}
Now let’s see what you did wrong:
Let’s go line by line here. You know what task(main) and the brackets do, but let’s then take a look at line 4.
while(1==1){
This will forever loop what is in the brackets. Where does our bracket end? Let’s just take a look. It appears the closing bracket for your while loop is on line 10. This means that for all of eternity your code will be looping these 5 lines (can you guess them?)
Answer
if(SensorValue(Switch)==1)
{
motor[port2] = 127; // if pressed runs the motor
}
This means that your program will never, ever stop waiting for a button press, then setting the motor value to 127. Because there is no provision to set the motor value back down to 0, this effectively does nothing other than making our motor run for all of time once the button is pressed.
Because of this loop, this part of the code is never being run:
wait1Msec(1000); // robot moves forward for 1 seconds before program ends
motor[port2] = 0; //Stop
To the Cortex, it is as if these lines of code don’t exist. They need to be inside the while loop.