For a school project we have to make a carnival game. My group is doing a horse racing game where two players compete to press their buttons faster than the other players. We have most of the code done, but we can’t figure out how to make the motor only move when the button is pressed and released, rather than being able to hold the button. The point of the game is whoever presses the button the fastest until it reaches the finish line, wins. The code we currently have is below. All we need to know is how to either stop and restart the motor every second the button is held, or how to make the motor only respond when the button is pressed and released. Thanks!
task main()
{ //Program begins, insert code within curly braces
{
while(true)
{
if(SensorValue(bumpSwitch1) == 0) //if the sensor value of the bumpswitch1 is 0…
{
stopMotor(motor1); //the motor is off
}
if(SensorValue(bumpSwitch1) == 1) //if the sensor value of the bumpswitch1 is 1…
{
startMotor(motor1, 23); //the motor is on
}
{
if(SensorValue(bumpSwitch2) == 0) //if the sensor value of the bumpswitch2 is 0…
{
stopMotor(motor2); //the motor is off
}
if(SensorValue(bumpSwitch2) == 1) //if the sensor value of the bumpswitch2 is 1…
{
startMotor(motor2,18); //the motor is on
}
}
if (SensorValue (limitSwitch1) ==1) //if the sensor value of the limitswitch1 is 1…
{
setServo(servoMotor, 100); //the servo motor turns
}
if (SensorValue (limitSwitch2) ==1) //if the sensor value of the limitswitch2 is 1…
{
setServo(servoMotor, 100); //the servo motor turns
}
}
}
}
task main() { //Program begins, insert code within curly braces
while (true) {
if (SensorValue(bumpSwitch1) == 0) //if the sensor value of the bumpswitch1 is 0…
{
stopMotor(motor1); //the motor is off
}
if (SensorValue(bumpSwitch1) == 1) //if the sensor value of the bumpswitch1 is 1…
{
startMotor(motor1, 23); //the motor is on
} {
if (SensorValue(bumpSwitch2) == 0) //if the sensor value of the bumpswitch2 is 0…
{
stopMotor(motor2); //the motor is off
}
if (SensorValue(bumpSwitch2) == 1) //if the sensor value of the bumpswitch2 is 1…
{
startMotor(motor2, 18); //the motor is on
}
}
if (SensorValue(limitSwitch1) == 1) //if the sensor value of the limitswitch1 is 1…
{
setServo(servoMotor, 100); //the servo motor turns
}
if (SensorValue(limitSwitch2) == 1) //if the sensor value of the limitswitch2 is 1…
{
setServo(servoMotor, 100); //the servo motor turns
}
}
}
}
First of all, here is the code formatted to be readable. There seem to be some problems with the curly braces in your if/else structure, I’m not sure if you copied it wrong or what.
if (SensorValue(bumpSwitch1) == 1) //if the sensor value of the bumpswitch1 is 1…
{
startMotor(motor1, 23); //the motor is on
} { //<--- what is this curly brace doing?
if
You have the motor turning when the limit switch is active, and not when it is pressed. I can’t find suitable RobotC documentation for this, but there is most likely a language function that fires when the limit switch is pressed.
If there is nothing of the sort, you might be able to do something similar. When the button is pressed, you can check if the button has been held down since the last press, by requiring it to activate something in the off state, probably a boolean value. Checking if that value is true, and spinning the motor & disabling that boolean again.
This wasn’t well explained, if someone could provide a code example, that might help out.
I reformatted your code and removed the extraneous brackets…
NOTE: When you post your code tags, everything in it needs to be indented (tab) once (found that out the hard way myself).
//Program begins, insert code within curly braces
task main()
{
while(true)
{
//if the sensor value of the bumpswitch1 is 0…
if(SensorValue(bumpSwitch1) == 0)
{
stopMotor(motor1); //the motor is off
}
//if the sensor value of the bumpswitch1 is 1…
if(SensorValue(bumpSwitch1) == 1)
{
startMotor(motor1, 23); //the motor is on
}
//if the sensor value of the bumpswitch2 is 0…
if(SensorValue(bumpSwitch2) == 0)
{
stopMotor(motor2); //the motor is off
}
//if the sensor value of the bumpswitch2 is 1…
if(SensorValue(bumpSwitch2) == 1)
{
startMotor(motor2,18); //the motor is on
}
//if the sensor value of the limitswitch1 is 1…
if (SensorValue (limitSwitch1) ==1)
{
setServo(servoMotor, 100); //the servo motor turns
}
//if the sensor value of the limitswitch2 is 1…
if (SensorValue (limitSwitch2) ==1)
{
setServo(servoMotor, 100); //the servo motor turns
}
}//end while
}//main()
Remember to make it readable for anyone you are showing it to and anyone who may edit/view it sometime later. It should be a balance between your personal philosophy and the capability, patience, etc. of those who may view it.
As for your motor - if you are keying the motor as someone slaps a bump-switch multiple times a second, remember that there is a delay or “play” in the timing. You need to use a single motor and test for this and write your code accordingly. For instance, you may have to “give credit” to multiple hits or continually run the motor if a new “bumpswitch == 1” comes in within a certain amount of time (or stop it if, say, they “release” for 100ms or more since the last “press”).
[not real code]
task for limit one:
while true:
wait until limit switch pressed
spin motor
wait until limit not pressed
stop motor
task for limit two:
while true:
wait until limit pressed...
[basically repeat above code]
Obviously, this code won’t do what you want it to do, but I can’t figure out what you want it to do. You could do something like every time the button is pressed the speed goes up.
velocity = 0
velocityDecrement = 1
while true:
if limit pressed:
if not limitPressedLastTime:
velocity = 100
limitPressedLastTime = true
else: // Limit isn't pressed
limitPressedLastTime = false
spin motor at velocity
velocity = velocity - velocityDecrement
And basically repeat that for the other limit switch. If you don’t understand how the variables turn on and off, I’ll be happy to explain further. Please don’t just copy the code, though. If you copy it mindlessly and it doesn’t work, you won’t know what’s broken.