— boreal-kiss.net

Controlling Processing graphics from Arduino

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
  }
}
0 comments
Submit comment