RoboCatz.com

Main Program (Fall 2020)


The code below is a set of functions, variables, constants, and algorithms that are the work of RoboCatz for the Fall 2020 season.

//hi
function driveBackward(inches, pwr) {
	beep()
	sleep(100)
	beep()
	
	myPower = 30
        if( pwr*1 > 0 ) myPower=pwr             // Same as: if(typeof pwr == 'number') myPower=pwr*1
	syncMotors(B, C, myPower)               // 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, pwr) {
	myPower = 30
        if( pwr*1 > 0 ) myPower=pwr             // Same as: if(typeof pwr == 'number') myPower=pwr*1
	resetEncoder(B)                         // Reset the encoder
	syncMotors(B, C, myPower * -1)                   // 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 in the other direction
	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 swingTurnCounterClockwise2(degrees) {
	syncMotors( C, B, 30, -100 )             // Start a swing turn in the other direction
	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 in the other direction
	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 pointTurnCounterClockwise(degrees) {
	beep()
	syncMotors( B, C, 30, 200 )             // Start a swing turn in the other direction
	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 pointTurnClockwise(degrees) {
	syncMotors( B, C, 30, -200 )             // Start a swing turn in the other direction
	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 attachmentMotor(degrees, pwr) {
   setMotor(A, pwr)                     
   // Start Tilting up
   resetEncoder(A)                         
   // Reset the encoder
   oldEncoder = encoderValue(A)    // Initializing old encoder value
   while( abs(encoderValue(A)) < degrees * 1 ) { 
      clearScreen()                         
      // Clear the screen
      drawText( 10, 10, encoderValue(A), 2 ) // Show the encoder value
      sleep(100)
      currentEncoder = encoderValue(A) * 1
      if(abs(currentEncoder - oldEncoder) < 5) break;
      oldEncoder = encoderValue(A) 		// reset the oldEncoder value
      // Move for a period of time
   } // Repeat the loop
   stopAllMotors()
   
  
}

function baseToDance(){

}
function linefollow(){
		target = 19
		gain = 3
		while(true) {
		  drawText(10,10, lightSensorValue())
		  syncMotors(2, 3, 20, (lightSensorValue()-target) * gain)
		  sleep(100)
		}
}


function steptopullupbar() {
	driveBackward(1.0625,35) 	//back away from step
	stopAllMotors()
	pointTurnCounterClockwise(90)// lines up with bar
	syncMotors(B, C,  30)
	sleep(1000)
	while(touchSensorPressed()==false) {
	  
	  sleep(50)
	}
	stopAllMotors() 
	sleep(1000)	
	stopAllMotors()
	driveForward(31.25,30)  
	stopAllMotors()
	pointTurnCounterClockwise(75)
	stopAllMotors()
	driveForward(11.5,20)
	stopAllMotors()
	pointTurnClockwise(75)
	stopAllMotors()
	driveForward(6,20)
	// Turn one wheel at a time
	syncMotors( B, C, 30, -100 )
	sleep(1000)
	syncMotors( B, C, 30, 100 )
	sleep(1000)
	// Accelerate the motor on port (B, C,)
	for(i=1;i<20;i++) {
	  setMotor(2, 10+i*4)
	  //setMotor(3, 10+i*-4)
	  sleep(300)
	}
	sleep(3000)
}


function stepanddance() {
	alert('You selected ENTER')
    driveForward(26.5,20)	//drive to counter
    pointTurnCounterClockwise(75)	//back up to wall
    stopAllMotors()
	syncMotors(B, C,  30)
	sleep(1000)
	while(touchSensorPressed()==false) {
	  
	  sleep(50)
	}
	stopAllMotors() 
	sleep(1000)
	driveForward(3,10)	
	pointTurnClockwise(85)
	stopAllMotors()
	driveForward(8.4,3)		//move counter to blue
	stopAllMotors()
	steptopullupbar()
}



function slide2() {
driveForward(10,50)  //electronic stuff 
}
//#learningcodetobeahacker

function slide() {
	//alert ('You selected UP')
	
	driveForward(13, 35)	//drive forward to start slide
	stopAllMotors()
	pointTurnCounterClockwise(90)	//lines up to touch wall
    stopAllMotors()
	syncMotors(B, C,  30)
	sleep(1000)
	while(touchSensorPressed()==false) {
	  
	  sleep(50)
	}
	stopAllMotors() 
	sleep(1000)
	syncMotors(B, C, -30)
	while(lightSensorPct()>6) {	//finds line
	  
	  sleep(50)
	 
	}


	stopAllMotors()
	pointTurnClockwise(41)	//lines up with slide
	stopAllMotors()
	attachmentMotor(60, -30)
	stopAllMotors()
	driveForward(6.9)
	stopAllMotors()
	attachmentMotor(93, 30)		//swings attachment motor			
	stopAllMotors()
	driveBackward(5)		//knocks guy down slide
	pointTurnCounterClockwise(20)
	stopAllMotors()
	attachmentMotor(50, -30)
	stopAllMotors()
	driveForward(23)
	pointTurnClockwise(83)
	driveForward(3)
	stopAllMotors()
	
}
	
function bench() {
	alert('You selected DOWN')
	attachmentMotor(200,20)
    driveForward(13,20)
    stopAllMotors()
    swingTurnCounterClockwise(15)
    driveForward(2)
    pointTurnCounterClockwise(20) //knockes over bench
    stopAllMotors()
    attachmentMotor(45,100)
    attachmentMotor(45,-100)
	pointTurnClockwise(20)
	stopAllMotors()
	swingTurnClockwise(15)
	stopAllMotors()
	syncMotors(B, C, 30) //backs up to wall inside base
	while(touchSensorPressed()==false) {
	  
	  sleep(50)
	}
	stopAllMotors() 
	sleep(1000)
	driveForward(2)
	pointTurnClockwise(83)
}
function knockbench() {
		alert('You selected RIGHT')
}

function MH_Dance() {
	syncMotors( B, C, 30, -200)
	sleep(1000)
	syncMotors( B, C, 30, 200 )
	sleep(1000)
	
	// This is an experiment in generating 
	// Frequencies for notes
	freq=[
	    392.00, 415.30, 440, 466.16, 493.88, 523.25,  554.37,  587.33,  622.25,  659.26,  698.46,  739.99,  
	    783.99, 830.61, 880, 932.33, 987.77, 1046.5, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 
	   1567.98 
	 ]
	// Create a 3-dimensional array to store the note information
	//   dimension 1: [key]
	//   dimension 2: [up/down] (where 0=up and 1=down)
	//   dimension 3: [note index] (0-4 or 0-5 depending on the up/down sequence
	sequence=[]
	sequence.push([[7, 12, 16, 19], [19, 16, 12, 7, 4]])  // G-Major
	sequence.push([[4,  8, 12, 20], [20, 16, 12, 8, 4]])  // Augmented
	sequence.push([[5,  9, 12, 17], [21, 17, 12, 9, 5]])  // C-Major
	sequence.push([[8, 12, 15, 20], [20, 15, 12, 8, 3]])  // E-flat-Major
	sequence.push([[7, 12, 16, 19], [19, 16, 12, 7, 4]])  // G-Major
	
	clearScreen()
	for(k=0;k<sequence.length; k++) {
	  beep(20, freq[0], 150 )   // Tonic
	  sleep(200)
	  for(l=0; l<4; l++) {      // sequence up
	    f=sequence[k][0][l]
	    x=freq[f]
	    beep(20, x, 150 )
	    sleep(200)
	  }
	  beep(20, freq[24], 150 )  // Tonic 3rd Octave
	  sleep(200)
	  beep(20, freq[24], 150 )  // Tonic 3rd Octave
	  sleep(200)
	  for(l=0; l<5; l++) {      // sequence down
	    f=sequence[k][1][l]
	    x=freq[f]
	    beep(20, x, 150 )
	  }
	}
	beep(20, freq[0], 1000 )    // Tonic
	//sleep(0)
	// Turn wheels in opposite directions
	syncMotors( B, C, 30, -200 )
	sleep(1000)
	syncMotors( B, C, 30, 200 )
	sleep(1000)




}

proceedFromBase=true
while(true) {
	setLED(0)
	clearScreen()
	
	rect(10,10,160,60)
	drawText(15,30,'Press a key to')
	
	drawText(15,45,'  select a program')
	
	keyPressed=waitForPress()
	
	switch(keyPressed)
	{
	
	
	
	  case 2:
	  	stepanddance()		
	    break
	
	  case 3:
	  	bench()
	  	slide()
	  	//MH_Dance()
	  	//break
	    proceedFromBase=false
	  case 1:
	    if(proceedFromBase){
	      baseToDance()
	    }
		MH_Dance()
		
	    break
	    
	  case 4:
	  	knockbench()
	    break
	   
	  case 5:
	  	steptopullupbar()
	  	break
	 }			
}