Processing export to OSX, did not work with serial port communication
in
Integration and Hardware
•
1 month ago
Dear all
It works when I run the code from Processing window. But when exporting to OSX app, it stops working! I try to debug and isolate the problem a bit: i think it is the serial communication. any solution??
Processing code:
- //this code reads from serial port and move Mario around
- //based on the switch pressed on Arduino board
- // import the serial library
- import processing.serial.*;
- import gifAnimation.*;
- // create an instance of the serial library
- Serial myPort;
- // create an instance of PImage
- //PImage[] animation;
- Gif loopingGif;
- Gif loopingGif2;
- int keypress =0;
- void setup() {
- size (500, 500);
- frameRate(100);
- loopingGif = new Gif(this, "mario3.gif");
- loopingGif.loop();
- loopingGif2 = new Gif(this, "mario14.gif");
- loopingGif2.loop();
- println("Available serial ports:");
- println(Serial.list());
- myPort = new Serial(this, Serial.list()[0], 9600);
- }
- int x=0;
- int y=50;
- void draw() {
- if ( myPort.available() > 0) {
- keypress = myPort.read();
- // print the value to the status window
- println(keypress);
- }
- background(255);
- image(loopingGif, x, y, loopingGif.width* 2, loopingGif.height*2);
- if (keypress == 1) {
- x ++ ;
- image(loopingGif, x, y, loopingGif.width* 2, loopingGif.height*2);
- }else if (keypress == 2){
- x --;
- image(loopingGif, x, y, loopingGif.width* 2, loopingGif.height*2);
- }else if (keypress == 3){
- background(255);
- image(loopingGif2, x, y-20, loopingGif2.width* 2, loopingGif2.height*2);
- }
- }
Arduino code
- //this code write byte into serial port based on the switch pressed
- //processing will read the serial port and move Mario spirite on the screen
- //it is a basic game controller
- void setup() {
- Serial.begin(9600);
- pinMode(A0, INPUT);
- }
- void loop(){
- int keypress = analogRead(A0);
- //Serial.println(analogRead(A0));
- if (keypress >=1022) { //first key pressed
- //Serial.println("1");
- Serial.write(1);
- } else if (keypress >1000 && keypress <=1002) { //2nd key pressed
- //Serial.println("2");
- Serial.write(2);
- } else if (keypress >500 && keypress <=999) {//third key press
- //Serial.println("3");
- Serial.write(3);
- } else if (keypress >0 && keypress <=10) {//fourth key pressed
- // Serial.println("4");
- Serial.write(4);
- } else if (keypress ==0){
- //Serial.println("0");
- Serial.write(0);
- }
- delay(100);
- }
thanks a lot!
jim
1