Barobo
PortalSign In
HomeShopTutorialsFree CurriculumHour of Code
RoboBlockly for Learning Math and Coding

Use a Joystick to Control a Linkbot (Part 2)

Piano
Linkbot Image Mindstorm Image Cursor Image
0x2418126-6-12y2418126-6-12
Grid:
Tics Lines:
Width px
Hash Lines:
Width px
Labels:
Font px
Trace Lines:
Robot 1:
Width px
Robot 2:
Width px
Robot 3:
Width px
Robot 4:
Width px
Labels: x-axis y-axis Show Grid
Grid: 12x12 inches 24x24 inches 36x36 inches
72x72 inches 96x96 inches 192x192 inches
Quad: 4 Quadrants 1 Quadrant 1&4 Quadrants
Units: US Customary Metric
Background: Background Image

Robot 1

Linkbot
Mindstorm
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (0 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in

Robot 2

Linkbot
Mindstorm
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (6 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in

Robot 3

Linkbot
Mindstorm
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (12 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in

Robot 4

Linkbot
Mindstorm
Initial Position:
( in, in)
Initial Angle:
deg
Current Position: (18 in, 0 in)
Current Angle: 90 deg
Wheel Radius:
Track Width:
in
Use a Joystick to Control a Linkbot (Part 2)
/* Code generated by RoboBlockly v2.5 */
/* Problem Statement: 
    Modify the code so that it blinks the blue LED 3 times
    before turning right or left.
    */
#include <arduino.h>
#include <robot.h>
CLinkbotI robot;
double radius = 1.75;
double trackwidth = 3.69;
double thebutton;
double movingforward;
double yvalue;
double xvalue;

// Set up LED pins and make sure they're off
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// Set pin for the joystick button to input
pinMode(2, INPUT);
// Set pins for joystick x and y values to input
pinMode(A1, INPUT);
pinMode(A0, INPUT);
// Set value of button variable to 0 (unpressed)
thebutton = 0;
// Set value of movingforward variable to 0 (stopped)
movingforward = 0;
// Keep reading button until pressed (nonzero value)
while(thebutton == 0) {
  // Get joystick x and y readings
  yvalue = analogRead(A0);
  xvalue = analogRead(A1);
  // If y value is high, move robot forward with green light
  // If y value is low, move robot backward with red light
  // If x value is high, turn right 90 degrees
  // If x value is low, turn left 90 degrees
  // Else stop robot
  if (yvalue > 712) {
    // If robot not already moving forward, move it forward
    if (movingforward != 1) {
      movingforward = 1;
      robot.setSpeed(4, radius);
      robot.driveForeverNB();
      digitalWrite(12, HIGH);
    }
  }
  else if (yvalue < 312) {
    // If robot not already moving backward, move it backward
    if (movingforward != -1) {
      movingforward = -1;
      robot.setSpeed(-4, radius);
      robot.driveForeverNB();
      digitalWrite(13, HIGH);
    }
  }
  else if (xvalue > 712) {
    // Blink blue 3 times and turn left 90 degrees
    digitalWriteHighLow(11, 200, 200, 3);
    robot.turnLeft(90, radius, trackwidth);
  }
  else if (xvalue < 312) {
    // Blink blue 3 times and turn right 90 degrees
    digitalWriteHighLow(11, 200, 200, 3);
    robot.turnRight(90, radius, trackwidth);
  }
  else {
    // Stop robot and make sure lights are off
    movingforward = 0;
    robot.stop();
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
  }
  // Delay 0.1 seconds so don't overload reading
  delay(100);
  // Read the value of joystick button (1 if pressed)
  thebutton = digitalRead(2);
}
// Joystick button was pressed, so loop ends
// Make sure robot is stopped and lights are off
robot.stop();
digitalWrite(12, LOW);
digitalWrite(13, LOW);
Load Blocks
Hardware
LArduino
Workspace Show Ch Save File
Problem Statement:

Modify the code so that it blinks the blue LED 3 times before turning right or left.


		
Rubbish bin
Time