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

Use a Joystick to Control a Linkbot

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
/* Code generated by RoboBlockly v2.5 */
/* Problem Statement: 
    Problem statement here
    */
#include <arduino.h>
#include <robot.h>
CLinkbotI robot;
double radius = 1.75;
double thebutton;
double movingforward;
double yvalue;

// Set up LED pins and make sure they're off
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
// Set pin for the joystick button to input
pinMode(2, INPUT);
// Set pin for joystick y value to 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 y reading
  yvalue = analogRead(A0);
  // If y value is high, move robot forward with green light
  // If y value is low, move robot backward with red light
  // Else stop
  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(11, HIGH);
    }
  }
  else {
    // Stop robot and make sure lights are off
    movingforward = 0;
    robot.stop();
    digitalWrite(11, 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(11, LOW);
Load Blocks
Hardware
LArduino
Workspace Show Ch Save File
Problem Statement:

Modify the code so that it uses the green and blue LEDs instead of red and green.


		
Rubbish bin
Time