Robot
JavaScript
Compiler
Language
Videos
Simulator
Games
Examples
Download
Cheat sheet

Motor Examples


Sync Motors (B, C)

Example:
syncMotors( B, C )
sleep(5000)

Sync Motors (B, C, p)

Example:
syncMotors( B, C, 60 )
sleep(5000)
syncMotors( B, C, -60 )
sleep(5000)

Point Turn

Example:
syncMotors( B, C, 30, -200 )
sleep(2000)
syncMotors( B, C, 30, 200 )
sleep(2000)

Swing Turn

Example:
syncMotors( B, C, 30, -100 )
sleep(2000)
syncMotors( B, C, 30, 100 )
sleep(2000)

Gradual Turn

Example:
syncMotors( B, C, 30, -40 )
sleep(2000)
syncMotors( B, C, 30, 40 )
sleep(2000)

Encoder Value

Example:
// This program will start motor B at 30% power
// Variable i with sleep(100) is used to count 2 seconds
// After the for() loop, the encoder is reset
// The while() loop repeats this process infinitely
setMotor( B, 30 )
while true {
  for( i=0; i<20; i++ ) {
    clearScreen()
    drawText( 10, 10, encoderValue(B), 2 )
    sleep(100)
  }
  resetEncoder(B)
}

Reset Encoder Value

Example:
setMotor(B, 30)
resetEncoder(B)
while abs(encoderValue(B)) < 1000 {
  clearScreen()
  drawText( 10, 10, encoderValue(B), 2 )
  sleep(100)
}
resetEncoder(B)
setMotor(B,  -30)
while abs(encoderValue(B)) < 1000 {
  clearScreen()
  drawText( 10, 10, encoderValue(B))
  sleep(100)
}

Graphing Encoder Values

The motor encoder stores the degrees of rotation of the motor. Each motor has its own encoder which is updated through a rotation sensor inside the motor. The value of the encoder is reset either by turning the robot off, or by using the resetEncoder() function.

Connect one or motors to the motor ports. Connect a touch sensor to one of the sensor ports. Pressing the touch sensor will reset the encoders.

You can either rotate the motors by hand. Or apply power to the motors.
To apply power to motors B and C, uncomment the lines below.
setMotor( B, 20 )
setMotor( C, -20 )

You can start, then stop, then re-start the program. The motor encoders continue from their last recorded value. The encoders are not re-set each time you start the program. You could include the resetEncoder() functions at the beginning of your program if you want the encoders to be reset.

Example:
while true {
  if touchSensorPressed() {  // Reset each encoder
    resetEncoder(A)
    resetEncoder(B)
    resetEncoder(C)
    resetEncoder(D)
  }
  // Display the encoder values
  clearScreen()
  drawText( 0,  10, 'A: '+encoderValue(A)+'  ', 2)
  drawText( 0,  40, 'B: '+encoderValue(B)+'  ', 2)
  drawText( 0,  70, 'C: '+encoderValue(C)+'  ', 2)
  drawText( 0, 100, 'D: '+encoderValue(D)+'  ', 2)
  // Graph the encoder values
  radius = 15
  x = 150
  for(i=A; i<=D; i++) {           // Starting with motor A, loop through each motor port until D
     circle( x, i*30-10, radius )   // x, y, radius
     // Draw a line pointing to the current degree of rotation
     line( x, (i*30-10),  x + cosin(encoderValue(i))*radius, (i*30-10) + sin(encoderValue(i))*radius )
  }
  // Pause for 300 milliseconds 
  sleep(300)
}

Moving robot in a square pattern

Make the robot move around a square. The robot will move straight for 2 seconds and then reset the gyro and turn 90 degrees counter-clockwise. The for() loop will repeat this action 4 times.
Example 1:
for(i=1;i<=4;i++) {                       // Loop for 4 times
  syncMotors( 2, 3, 30 )                  // Start driving straight
  sleep(2000)                             // For 2 seconds
  resetGyroSensor()                       // Reset the gyroSensor
  syncMotors( B, C, 30, -100 )            // Start turning (swing turn)
  waitHereWhile abs(gyroSensorValue())<88 // Turn for 88 degrees
}

S-Turns

Example:
// Turn one wheel faster than the other
syncMotors( B, C, 30, -60 )
sleep(2000)
syncMotors( B, C, 30, 60 )
sleep(2000)
syncMotors( B, C, 30, -60 )
sleep(2000)
syncMotors( B, C, 30, 60 )
sleep(2000)

Accelerate One Motor

Example:
// Accelerate the motor on port A
for(i=1;i<20;i++) {
  setMotor(1, 10+i*4)
  sleep(300)
}
sleep(3000)

Acceleration

Example:
// Accelerate the robot by increasing power to the syncMotors function.
for(i=1;i<20;i++) {
  syncMotors(B,C, 10+i*4)
  sleep(300)
}
sleep(3000)

Acceleration Using stepMotors()

Example:
// Accelerate the robot using the stepMotors() function.
stepMotors( B, C, 60, 720)          // Parameters are: motor1, motor2, desired speed, degrees of rotation to achieve desired speed
waitHereWhile getMotorSpeed(B)==0   // The stepMotors() function always starts with the motor speed of zero.  Wait here while the robot prepares to move.
waitHereUntil getMotorSpeed(B)==0   // Once the robot has started moving, wait here until it stops.
beep(20, 440, 200)                  // Play a beep indicating the end of the stepMotors cycle.
sleep(2000)


// Accelerate the robot using the stepMotors() function.
stepMotors( B, C, 60, 360, 720)     // Parameters are: motor1, motor2, desired speed, degrees of rotation for acceleration, degrees at desired speed
waitHereWhile getMotorSpeed(B)==0   // The stepMotors() function always starts with the motor speed of zero.  Wait here while the robot prepares to move.
waitHereUntil getMotorSpeed(B)==0   // Once the robot has started moving, wait here until it stops.
beep(20, 440, 200)                  // Play a beep indicating the end of the stepMotors cycle.
sleep(2000)


// Accelerate the robot using the stepMotors() function.
stepMotors( B, C, -60, 360, 1080, 360)     // Parameters are: motor1, motor2, desired speed, acceleration, desired speed, deceleration
waitHereWhile getMotorSpeed(B)==0   // The stepMotors() function always starts with the motor speed of zero.  Wait here while the robot prepares to move.
waitHereUntil getMotorSpeed(B)==0   // Once the robot has started moving, wait here until it stops.
beep(20, 440, 200)                  // Play a beep indicating the end of the stepMotors cycle.
sleep(2000)