#pragma config(Motor, motorA, , tmotorNXT, openLoop) #pragma config(Motor, motorB, rightMotor, tmotorNXT, PIDControl, encoder) #pragma config(Motor, motorC, leftMotor, tmotorNXT, PIDControl, encoder) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// /*RESCUE ROBOTICS WORKSHOP #1 I N T R O T O F U N C T I O N S Ron Kessler Created 8/7/2018 Updated 8/12/2018 PURPOSE: Shows how to organize behaviors/actions into their own code blocks. This introduces "Functions". PROGRAMMING RULE: NEVER DUPLICATE CODE. WHEN YOU NEED TO RUN CODE FROM MORE THAN ONE PLACE, MOVE IT INTO ITS OWN SECTION. We will modify our Up and Back project to show how this works. Recall that we told the bot to go forward for 1800 degrees and then go backwards 1800 degrees. To do that, we reset our motor encoders twice using the same code (a big no-no) and we defined the distance twice also. Here is a better way..... STEP 1: Create two functions (custom code blocks) for resetting motor encoders and for defining the distance to travel. */ //---my new function called ResetMyEncoders (You get to name it. No spaces in these names) void ResetMyEncoders () { //---reset the motor encoders using the nMotorEncoder command. nMotorEncoder[leftMotor] = 0; nMotorEncoder[rightMotor] = 0; } //---my other function to set distance void SetRobotDistance() { //---set the encoder target property to 1800 degrees. nMotorEncoderTarget[leftMotor] = 1800; nMotorEncoderTarget[rightMotor] = 1800; } //Step 2: Press F7 to update our code task main() { //Step 3: Now use our new functions. Just type in their name.It will appear in the intellisense // drop down menu. Press TAB to pop in in your code! It only shows up if you F7 first. ResetMyEncoders(); //reset encoders SetRobotDistance(); //now set the desired distance //---now run motors forward at 35% motor[leftMotor] = 35; motor[rightMotor] = 35; wait1Msec(10000); //******** BACK UP SECTION ************** //---now make it back up //---reset the motor encoder again. ResetMyEncoders(); SetRobotDistance(); //---now run motors backwards at 35% motor[leftMotor] = -35; motor[rightMotor] = -35; wait1Msec(10000); } //Notice how much cleaner this is. When you want to add another behavior, add another funtion. //You can "call"/use these functions as many times as you wish and from anywhere in the project.