RoboCatz.com
About FLL | Navigation | Playbook | Strategy | Models | Techniques | RobotC | Programming | Robot News |

Playbook: Remote Control -- The Exercise

Goal: Use one robot to be a remote controller for another robot.

Step 1: Load the Example Menu program (5 options with cursor)

function drawMenu() {
  clearScreen()
  rect(10, 10, 150, 110)
  drawText(40, 25,"Option 1")
  drawText(40,40,"Option 2")
  drawText(40,55,"Option 3")
  drawText(40,70,"Option 4")
  drawText(40,85,"Option 5")
}
option = 1
function clearCursor() {
  drawText(20, option*15+10, '  ')
}
function drawCursor() {
  drawText(20, option*15+10, '>>')
}
while(true) {
  drawMenu()
  drawCursor()
  key=waitForPress()
  clearCursor()
  if key==1 option-=1
  if key==3 option+=1
  if option<1 option = 1
  if option>5 option = 5
  drawCursor()
  if key==2 break

}

if option==1 {
  alert('1 blah blah blah')
} else if option==2 {
  alert('2 blah blah blah')
} else if option==3 {
  alert('3 blah blah blah')
}
Delete the if statements at the end of the program and also the line that says: "if key==2 break"

Your program should now look like:

function drawMenu() {
  clearScreen()
  rect(10, 10, 150, 110)
  drawText(40, 25,"Option 1")
  drawText(40,40,"Option 2")
  drawText(40,55,"Option 3")
  drawText(40,70,"Option 4")
  drawText(40,85,"Option 5")
}
option = 1
function clearCursor() {
  drawText(20, option*15+10, '  ')
}
function drawCursor() {
  drawText(20, option*15+10, '>>')
}
while(true) {
  drawMenu()
  drawCursor()
  key=waitForPress()
  clearCursor()
  if key==1 option-=1
  if key==3 option+=1
  if option<1 option = 1
  if option>5 option = 5
  drawCursor()

}
Add the following touch sensor event handler function before the "while(true)" loop.

// This is the handler function for the touchSensor events.  This handler will receive both the pressed and the released events.

function toHandleTheTouchSensor(e) {    // A touchSensor event will occur when the touchSensor is pressed as well as when it is released.

	if e.value == 1                       // If the touchSensor was pressed the event value will equal 1.
	  setLED(2),                          // Set LED to red
	  sendEventTo('Robot5', option, 1, 2),// RobotName, Value of the option, Port, Protocol
	  drawText(20, option*15+10, 'OO')    // Change the cursor
	  
	if e.value == 0                       // If the touchSensor was released the event value will equal 0
		setLED(1),                          // Set the LED to green
		sendEventTo('Robot5', 0, 1, 2),     // Send to RobotName, the Value of zero (stop), at Port, using Protocol
		drawCursor()                        // Redraw the cursor
}
// Add an event listener to listen for touchSensor events and then handle them by passing the event to the callback function.
// An event listener requires two parameters: 1) name of the event as a string and 2) identifier of the callback function (i.e., the event handler).
addEventListener( 'touchSensor', toHandleTheTouchSensor )

Your program should now look like:

function drawMenu() {
	clearScreen()
	rect(10, 10, 150, 110)
	drawText(40, 25,"Option 1")
	drawText(40,40,"Option 2")
	drawText(40,55,"Option 3")
	drawText(40,70,"Option 4")
	drawText(40,85,"Option 5")
}
option = 1
function clearCursor() {
	drawText(20, option*15+10, '  ')
}
function drawCursor() {
	drawText(20, option*15+10, '>>')
}
// This is the handler function for the touchSensor events.  This handler will receive both the pressed and the released events.

function toHandleTheTouchSensor(e) {    // A touchSensor event will occur when the touchSensor is pressed as well as when it is released.

	if e.value == 1                       // If the touchSensor was pressed the event value will equal 1.
	  setLED(2),                          // Set LED to red
	  sendEventTo('Robot5', option, 1, 2),// RobotName, Value of the option, Port, Protocol
	  drawText(20, option*15+10, 'OO')    // Change the cursor
	  
	if e.value == 0                       // If the touchSensor was released the event value will equal 0
		setLED(1),                          // Set the LED to green
		sendEventTo('Robot5', 0, 1, 2),     // Send to RobotName, the Value of zero (stop), at Port, using Protocol
		drawCursor()                        // Redraw the cursor
}
// Add an event listener to listen for touchSensor events and then handle them by passing the event to the callback function.
// An event listener requires two parameters: 1) name of the event as a string and 2) identifier of the callback function (i.e., the event handler).
addEventListener( 'touchSensor', toHandleTheTouchSensor )
// The forever loop will ensure the program runs forever
forever {
	drawMenu()               // Draw the menu
	drawCursor()             // Draw the cursor at the current option
	key=waitForPress()       // Wait for keyPresses.  touchSensor events are also handled here.
	clearCursor()            // Erase the cursor from the current option
	if key==1 option-=1      // If the Up key was pressed, adjust by decreasing the option index
	if key==3 option+=1      // If the Down key was pressed, adjust by increasing the option index
	if option<1 option = 1   // Check if you are out of bounds
	if option>5 option = 5
	drawCursor()             // Redraw the cursor at the new option
}

Re-Label the Menu Options

function drawMenu() {
	clearScreen()
	rect(10, 10, 150, 110)
	drawText(40, 25,'Forward'  )
	drawText(40, 40,'Backward' )
	drawText(40, 55,'Turn Left')
	drawText(40, 70,'Turn Right')
	drawText(40, 85,'Option 5' )
	drawText(40,100,'Option 6' )
}