— boreal-kiss.net

Archive
Tag "arduino"

JoystickController is an Arduino library that is fully compatible with SparkFun’s Joystick Shield. The library lets you easily fetch the current status of the joystick. Implementation is quite simple;

  JoystickController(int low = DEFAULT_LOW, int high = DEFAULT_HIGH)

is a constructor. The first argument specifies the lower threshold value for the directional key pad, below which the controller is assumed to set left/down in the horizontal/vertical direction. The second argument specifies the upper threshold value, above which the controller is assumed to set right/up in the horizontal/vertical direction. If arguments are not specified, the following default values will be used:

  static const int DEFAULT_LOW  = 490;
  static const int DEFAULT_HIGH  = 530;

(Note these variables are defined as private so that you can not directly access them) For more information about the threshold see the following article: How do I find the current direction of the joystick?

 void update()

must get called in the main loop in order for the library to communicate with the joystick.

  int getDirection()

returns the current direction of the directional keypad as integer values that are defined as class static variables:

  static const int DIRECTION_NONE = 0;
  static const int DIRECTION_UP = 1;
  static const int DIRECTION_UP_RIGHT = 2;
  static const int DIRECTION_RIGHT = 3;
  static const int DIRECTION_DOWN_RIGHT = 4;
  static const int DIRECTION_DOWN = 5;
  static const int DIRECTION_DOWN_LEFT = 6;
  static const int DIRECTION_LEFT = 7;
  static const int DIRECTION_UP_LEFT = 8;

Finally buttons’ status can be fetched from the following functions:

  bool upButtonPressed()
  bool downButtonPressed()
  bool leftButtonPressed()
  bool rightButtonPressed()
  bool selectButtonPressed()

They return true if a button is being pressed, otherwise false. 

Example

Here is a simple example for the library implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "JoystickController.h"
 
JoystickController controller;
 
void setup(){
  Serial.begin(9600);
}
 
void loop(){
  controller.update();
 
  if (controller.selectButtonPressed()){
    Serial.println("S"); 
  }
 
  if (controller.upButtonPressed()){
    Serial.println("U"); 
  }
 
  if (controller.downButtonPressed()){
    Serial.println("D"); 
  }
 
  if (controller.leftButtonPressed()){
    Serial.println("L"); 
  }
 
  if (controller.rightButtonPressed()){
    Serial.println("R"); 
  }
 
  int direct = controller.getDirection();
 
  switch (direct){
    case  JoystickController::DIRECTION_UP:
      Serial.println("UP");
      break;
    case  JoystickController::DIRECTION_UP_RIGHT:
      Serial.println("UP-RIGHT");
      break;
    case  JoystickController::DIRECTION_RIGHT:
      Serial.println("RIGHT");
      break;
    case  JoystickController::DIRECTION_DOWN_RIGHT:
      Serial.println("DOWN-RIGHT");
      break;
    case  JoystickController::DIRECTION_DOWN:
      Serial.println("DOWN");
      break;
    case  JoystickController::DIRECTION_DOWN_LEFT:
      Serial.println("DOWN-LEFT");
      break;
    case  JoystickController::DIRECTION_LEFT:
      Serial.println("LEFT");
      break;
    case  JoystickController::DIRECTION_UP_LEFT:
      Serial.println("UP-LEFT");
      break;
  }
 
  delay(100);
}

Download

The library is available from my Github repository: JoystickController

Read More

I used a potentiometer as a joystick controlling the graphics on the computer. For demonstration purposes, I’ll show partial source code used in the above application (communication part only).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Arduino
 
int _out = 0;
float _vMax = 5.0;
 
void setup(){
  Serial.begin(9600);
}
 
void loop(){
  float v = 5.0 * analogRead(_out) / 1023.0;
  sendMessage(v);
  delay(100);
}
 
void sendMessage(float input){
  float dv = _vMax / 5;
 
  if (input < 1 * dv){
    Serial.print("D");
  }
  else if (input < 2 * dv){
    Serial.print("R");
  }
  else if (input < 3 * dv){
    Serial.print("U");
  }
  else if (input < 4 * dv){
    Serial.print("L");
  }
  else{
    Serial.print("D");
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Processing
 
import processing.serial.Serial;
 
Serial _port;
 
void setup(){
  //Setup drawing. 
}
 
void draw(){
  this.receiveMessage();
  //Update drawing.
}
 
void receiveMessage(){
  if (_port == null){
    _port = new Serial(this, Serial.list()[0], 9600);
  }
 
  if (_port.available() > 0){
    String message = _port.readString();
 
  if (message.equals("U")){
    //UP
  }
 
  if (message.equals("D")){
    //DOWN
  }
 
  if (message.equals("L")){  
    //LEFT
  }
 
  if (message.equals("R")){
    //RIGHT
  }
}
Read More