Arduino Joystick shield, Nokia 5110 display and BlueTooth

Preamble

Following on from my Arduino blog, and the list of my favourite shields, I thought that I would expand upon the Joystick shield.

The 5110 display

Following the superb series of video tutorial from Julian Ilett, starting with Arduino Nokia 5110 LCD Tutorial #1 – Connecting and Initial Programming, I ended up getting the display to show button presses and joystick movements. Here is the resulting sketch:

 

USB HID

You can use the Leonardo, in conjunction with the joystick shield as an, albeit not particularly practical, input device for a PC.

Here are a couple of links, from , although they are not the links that inspired me in the first place:

I originally coded up my HID using the code from the Arduino tutorial, Joystick Mouse Control.

/*
  JoystickMouseControl
 
 From https://www.arduino.cc/en/Tutorial/JoystickMouseControl
 
 Controls the mouse from a joystick on an Arduino Leonardo or Micro.
 Uses a pushbutton to turn on and off mouse control, and
 a second pushbutton to click the left mouse button
 
 Hardware:
 * 2-axis joystick connected to pins A0 and A1
 * pushbuttons connected to pin D2 and D3
 
 The mouse movement is always relative. This sketch reads 
 two analog inputs that range from 0 to 1023 (or less on either end)
 and translates them into ranges of -6 to 6. 
 The sketch assumes that the joystick resting values are around the 
 middle of the range, but that they vary within a threshold.
 
 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the command.
 This sketch includes a pushbutton to toggle the mouse control state, so
 you can turn on and off mouse control.
 
 created 15 Sept 2011
 updated 28 Mar 2012
 by Tom Igoe
 
 this code is in the public domain
 
 MJ - Turned off mouse by default
    - Added middle and right mouse buttons
    - Inverted y access
    - Select 2 switch turns on mouse
    - Changed the speed/sensitivity, also had to change the threashold
    - It would be noce to make it resposive to the amount that the joystick is moved, rather than binary move/!move
 
 */

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 6;      // switch to turn on and off mouse control
const int mouseButtonL = 5;    // input pin for the mouse pushButton
const int mouseButtonR = 3;    // input pin for the mouse pushButton
const int mouseButtonM = 2;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis  
const int yAxis = A1;         // joystick Y axis
const int ledPin = 13;         // Mouse control LED 

// parameters for reading the joystick:
int range = 6;               // output range of X or Y movement - was 12 originally
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/3;      // resting threshold - was /4 originally
int center = range/2;         // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = HIGH;        // previous switch state

void setup() {
  pinMode(switchPin, INPUT);       // the switch pin
  pinMode(ledPin, OUTPUT);         // the LED pin  
 // take control of the mouse:
  Mouse.begin();
}

void loop() {
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
    } 
  }
  // save switch state for next comparison:
  lastSwitchState = switchState;

  // read and scale the two axes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, -yReading, 0);
  }  

  // read the left mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButtonL) == LOW) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT); 
    }
  } 
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT); 
    }
  }

  // read the right mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButtonR) == LOW) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.press(MOUSE_RIGHT); 
    }
  } 
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.release(MOUSE_RIGHT); 
    }
  }

  // read the middle mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButtonM) == LOW) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_MIDDLE)) {
      Mouse.press(MOUSE_MIDDLE); 
    }
  } 
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_MIDDLE)) {
      Mouse.release(MOUSE_MIDDLE); 
    }
  }

  delay(responseDelay);
}

/*
  reads an axis (0 or 1 for x or y) and scales the 
 analog input range to a range from 0 to 
 */

int readAxis(int thisAxis) { 
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  } 

  // return the distance for this axis:
  return distance;
}

Mouse Prank

Whilst not strictly related to the Joystick shield, as this works best on an Arduino Micro, following on from the USB HID, you can create a joke USB HID, that creates random mouse movements and button clicks. It might not be everyone’s idea of fun and can be quite dangerous, resulting in the lost of files, so create and use at your own discretion.

void setup()
{
  pinMode(2,INPUT);
  digitalWrite(2,HIGH);

  randomSeed(millis());
}

void loop()
{
  if(digitalRead(2) == LOW)
  {
    Mouse.begin();
    
    switch(random(0,5))
    {
    case 0: //Press the right mouse button
      Mouse.press(MOUSE_RIGHT);
      delay(100);
      Mouse.release(MOUSE_RIGHT);
      break;

    case 1: //Jiggle the mouse
      Mouse.move(random(-128,128),random(-128,128), 0);
      break;
      
    case 2: //Double click left mouse button
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      delay(100);
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      break;

    case 3: //Triple click left mouse button
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      delay(100);
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      delay(100);
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      break;

    case 4: //Jiggle the wheel
      Mouse.move(0,0, random(-128,128));
      break;
      
    }
    
    delay(random(60*1000,5*60*1000));
  }
  else
  {
    Mouse.end();
  }
}

Snake Game

Snake Game on the FRDM-KL25Z with Nokia 5110 Display

The code can also be obtained here from Joystick shield.

Advertisements

2 thoughts on “Arduino Joystick shield, Nokia 5110 display and BlueTooth”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s