RoboCatz.com

Our Function Library (Fall 2020)


A function library is a set of functions, variables, constants, and algorithms that are included with the basic RobotC programs. The functions and variables in the library can then be referenced and called upon as needed in your program.

The purpose of keeping these functions and variables in one file is to make your programs more modular. Many programs can refer to and use the same function library.

Driving Forward and Turning

// JavaScript Function Library
// Created: Fall 2020

function driveBackward(inches) {
	syncMotors(B, C, 30)                     // Start driving backward
	resetEncoder(B)                         // Reset the encoder
	while( abs(encoderValue(B)) < inches * 58 ) { // Convert inches to degrees by multiplying by 58
	  clearScreen()                         // Clear the screen
	  drawText( 10, 10, encoderValue(B), 2 ) // Show the encoder value
	  sleep(100)                            // Move for a period of time
	}                                       // Repeat the loop
}
function driveForward(inches) {
	resetEncoder(B)                         // Reset the encoder
	syncMotors(B, C, -30)                   // Start driving forward
	while( abs(encoderValue(B)) < inches * 58 ) { // Convert inches to degrees by multiplying by 58
	  clearScreen()                         // Clear the screen
	  drawText( 10, 10, encoderValue(B))    // Show the encoder value
	  sleep(100)                            // Move for a period of time
	}                                       // Repeat the loop
}
function swingTurnCounterClockwise(degrees) {
	syncMotors( B, C, 30, -100 )            // Start a swing turn
	resetGyroSensor()                       // Reset the sensor
	while( abs(gyroSensorValue()) < degrees ) {  // While the absolute value of the sensor is less than 90 degrees
	  sleep(50)                             // Move for a period of time
	}                                       // Repeat the loop
}
function swingTurnClockwise(degrees) {
	syncMotors( B, C, 30, 100 )            // Start a swing turn
	resetGyroSensor()                       // Reset the sensor
	while( abs(gyroSensorValue()) < degrees ) {  // While the absolute value of the sensor is less than 90 degrees
	  sleep(50)                             // Move for a period of time
	}                                       // Repeat the loop
}

Using the Functions in your Program

driveForward(10)
swingTurnClockwise(60)
driveBackward(10)
swingTurnCounterClockwise(60)
driveForward(10)
swingTurnClockwise(60)
driveBackward(10)
swingTurnCounterClockwise(60)
driveForward(10)
swingTurnClockwise(60)
driveBackward(10)
swingTurnCounterClockwise(60)
driveForward(10)
swingTurnClockwise(60)
driveBackward(10)
swingTurnCounterClockwise(60)