Joystick Experiments

After completing the Joystick project, test yourself by trying these experiments.

1 Use the joystick to control a servo motor. Refer to the servo motor project on how to control a servo motor. When the joystick is moved to one end the servo motor turns to 0 degrees, and when the joystick is moved to the other end the servo motor turns to 180 degrees.
int VRX = A0;       // joystick x-axis pin

#include 
Servo myservo;

int center = 512;   // the joystick center value
int maximum = 1023; // the joystick maximum value

void setup() {
  Serial.begin(9600);
  myservo.attach(12);       // the servo motor is attached to pin 12
  center = analogRead(VRX); // read initial center joystick value
}

void loop() {
  int x = analogRead(VRX);
  if (x > center) {
    int degree = map(x, center+1, maximum, 91, 180);
    Serial.print("   degree: ");
    Serial.print(degree);
    myservo.write(degree);
  } else if (x < center){
    int degree = map(x, 0, center-1, 0, 89);
    Serial.print("   degree: ");
    Serial.print(degree);
    myservo.write(degree);
  }
}
2 (Difficult) Write a program to turn the leds in a 7-segment led display either in a clockwise direction or counterclockwise direction depending on the input from the joystick. Refer to the 7-segment LED display project on how to control a 7-segment display. The leds turn clockwise if the joystick is moved to the right, and turn counterclockwise if the joystick is moved to the left. The speed of the led movements depends on how far the joystick is moved to the left or right.
2a This version is simple but much longer. The led rotation response to the joystick changes is not immediate and not smooth because the joystick value is read only after each complete round of the leds and there are long delays after displaying each led.

Delay commands are used to adjust the led rotation speed. At each delay command, the computer stops execution until the delay time is up, and so the joystick value is not checked until the computer resumes execution after the delay command.
// This program turns the leds in a 7-segment led display either in a clockwise
// direction or counterclockwise direction depending on the input from the joystick.
// The leds turn clockwise if the joystick is moved to the right,
// and turn counterclockwise if the joystick is moved to the left.
// The speed of the led movements depends on how far the joystick is moved to the left or right

// 7-segment pins
int segA = 7;
int segB = 6;
int segC = 4;
int segD = 2;
int segE = 11;
int segF = 9;
int segG = 10;

// joystick pins
int VRX = A0;
int VRY = A1;
int SW = A2;

void turnClockwise(int d) {
  digitalWrite(segA, LOW);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d);
  digitalWrite(segA, HIGH);
  digitalWrite(segB, LOW);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, LOW);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, LOW);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, LOW);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, LOW);
  digitalWrite(segG, HIGH);
  delay(d); 
}

void turnCounterClockwise(int d) {
  digitalWrite(segA, LOW);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, LOW);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, LOW);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, LOW);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, LOW);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
  digitalWrite(segA, HIGH);
  digitalWrite(segB, LOW);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  delay(d); 
}

int center = 512;   // the joystick center value
int maximum = 1023; // the joystick maximum value

void setup() {
  Serial.begin(9600);
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(SW, INPUT_PULLUP);
  center = analogRead(VRY); // read initial center joystick value
}

void loop() {
  int x = analogRead(VRX);
  int y = analogRead(VRY);  // use this y joystick value
  int s = digitalRead(SW);
  Serial.print("X value: ");
  Serial.print(x);
  Serial.print("   Y value: ");
  Serial.print(y);
  Serial.print("   switch value: ");
  Serial.print(s);

  
  if (y > center+2) {
    // convert the right half of the joystick range to a delay value
    int delayValue = map(y, center+2, maximum, 700, 60);
    Serial.print("   delay value: ");
    Serial.print(delayValue);
    turnClockwise(delayValue);

  } else if (y < center-2){
    // convert the left half of the joystick range to a delay value
    int delayValue = map(y, 0, center-2, 60, 700);
    Serial.print("   delay value: ");
    Serial.print(delayValue);
    turnCounterClockwise(delayValue);
  }

  Serial.println();
}
2b This version is more difficult to understand but shorter. The led rotation response to the joystick changes is very fast and smooth because the joystick value is read much more frequently with no delays after displaying each led.

No delay commands are used to adjust the led rotation speed. Instead, the milllis() function is used to calculate the time difference between the starting time and the current time. If the time difference is less than the delay time that we want, the computer does not stop here but continues on to check the joystick value.
// This program turns the leds in a 7-segment led display either in a clockwise
// direction or counterclockwise direction depending on the input from the joystick.
// The leds turn clockwise if the joystick is moved to the right,
// and turn counterclockwise if the joystick is moved to the left.
// The speed of the led movements depends on how far the joystick is moved to the left or right

// 7-segment pins
int segA = 7;
int segB = 6;
int segC = 4;
int segD = 2;
int segE = 11;
int segF = 9;
int segG = 10;

// joystick pins
int VRX = A0;
int VRY = A1;
int SW = A2;

// given the led number from 0 to 6, the corresponding
// led A to G is turned on
// all the leds are turned off if any other number is given
void displayLed(int led) {
  // first turn off all the leds
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, HIGH);
  digitalWrite(segF, HIGH);
  digitalWrite(segG, HIGH);
  // turn on the corresponding led from the given led number
  if (led == 0) {
    digitalWrite(segA, LOW);
  } else if (led == 1) {
    digitalWrite(segB, LOW);
  } else if (led == 2) {
    digitalWrite(segC, LOW);
  } else if (led == 3) {
    digitalWrite(segD, LOW);
  } else if (led == 4) {
    digitalWrite(segE, LOW);
  } else if (led == 5) {
    digitalWrite(segF, LOW);
  } else if (led == 6) {
    digitalWrite(segG, LOW);
  }
}

int center = 512;   // the joystick center value
int maximum = 1023; // the joystick maximum value
int LedNumber = 0;  // led number 0 to 6 to turn on segments A to G respectively
unsigned long startTime = millis(); // start time for the stopwatch

void setup() {
  Serial.begin(9600);
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(SW, INPUT_PULLUP);
  center = analogRead(VRY); // read initial center joystick value
}


void loop() {
  int x = analogRead(VRX);
  int y = analogRead(VRY);  // use this y joystick value
  int s = digitalRead(SW);
  Serial.print("X value: ");
  Serial.print(x);
  Serial.print("   Y value: ");
  Serial.print(y);
  Serial.print("   switch value: ");
  Serial.print(s);

  
  if (y > center+2) {
    // convert the right half of the joystick range to a delay value
    int delayValue = map(y, center+2, maximum, 700, 60);
    Serial.print("   delay value: ");
    Serial.print(delayValue);
    if ((millis() - startTime) > delayValue) {  // move led only when times up
      LedNumber++;  // calculate next led to turn on in clockwise direction
      if (LedNumber > 5) LedNumber = 0;
      startTime = millis(); // restart stopwatch startTime
    }
    
  } else if (y < center-2){
    // convert the left half of the joystick range to a delay value
    int delayValue = map(y, 0, center-2, 60, 700);
    Serial.print("   delay value: ");
    Serial.print(delayValue);
    if ((millis() - startTime) > delayValue) {  // move led only when times up
      LedNumber--;  // calculate previous led to turn on in counterclockwise direction
      if (LedNumber < 0) LedNumber = 5;
      startTime = millis(); // restart stopwatch startTime
    }  
  }
  
  displayLed(LedNumber);  // turn on the given led
  Serial.println();
}