1

I'm new to serial communication and trying to send mouseX and mouseY from Processing to Arduino. Serial monitor doesn't show anything.

Processing code:

import processing.serial.*;
Serial port;
void setup() {
  size(500,300);
  port = new Serial(this , Serial.list()[0], 9600);
}

void draw() {
   text("X Y mouse",250,50);
   if (port.available() > 0) {      
    port.write(str(mouseX)+' '+str(mouseY));
   }
}

Arduino code:

int x = 0;
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    x = Serial.read();
    Serial.print("I received: ");
    Serial.println(x);
  }
}

when I close Processing window and enter "ok" in Arduino serial monitor I see: enter image description here

  • 1
    please add the snapshot of serial monitor with the data you are sending. – Vaibhav Apr 20 at 10:08
  • 1
    Only one program on your pc can communicate over the serial port at once. When processing is connected to it, the Serial Monitor is not ans vice versa – chrisl Apr 20 at 10:54
  • 1
    @Vaibhav There's nothing to show...unfortunately. – 2012User Apr 20 at 10:58
  • 1
    @chrisl if that means I can't use serial monitor, Can I use mouseX and mouseY data in my arduino program? – 2012User Apr 20 at 11:01
  • 1
    Sure can you use the mouse position on the Arduino. You rust have to send them to it via Serial and the Arduino code has to decode it correctly. Currently you are sending an ASCII string but decoding it as byte data. Finish your data, that you send with \n (newline). On the Arduino read the entire message until you receive \n and put all received bytes into a string (or better a char array). Then you can use functions to convert the string to integers – chrisl Apr 20 at 16:09
1

Since you can't use the serial monitor to "see" the incoming data from Processing, here are 2 test sketches that may help you determine if data is received on the Arduino.

Upload the Arduino sketch first. Leave the serial monitor closed. Now run the Processing sketch.

I'm using frameRate(1) in the Processing sketch to send the data once per second, so the mouse X/Y values in the Processing window will be slow to update.

You should see the LED on the Arduino flash on then off quickly as the data is received. If you have a I2C LCD available, you could connect it to the Arduino and print out the incoming data.

As chrisl mentions in the comments, there is still much work to be done to get the raw data in the format you require.

Arduino

int x = 0;
const byte ledPin = 2;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop(){
  if(Serial.available() > 0){
    x = Serial.read();
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(ledPin, LOW);
  }
}

Processing

import processing.serial.*;
Serial port;
void setup() {
  size(500,300);
  background(148);
  try{
    port = new Serial(this , Serial.list()[0], 9600);
  }
  catch(Exception e){
    System.err.println(e);
    e.printStackTrace();
  }
  frameRate(1);
}

void draw() {
  background(148);
  //@chrisl comment: Finish your data, that you send with \n (newline).
  port.write(str(mouseX)+' '+str(mouseY) + "\n");
  text(mouseX,250,50);
  text(mouseY,250,70);
}

EDIT

Here is a second Arduino sketch that will display the mouse X/Y values sent from Processing on a LCD. You could change the Processing sketch's frameRate() value to 10.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

char inputBuffer[32 + 1];

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup(){
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}

void loop(){
  if(Serial.available() > 0){
    lcd.print("                ");
    Serial.readBytesUntil('\n', inputBuffer, 32);
    lcd.setCursor(0, 0);
    lcd.print(inputBuffer);
    memset(inputBuffer, 0, sizeof(inputBuffer));
  }
}
  • fantastic! I'm going to learn more about LCDs. I used an lcd once with parallel wiring... it was exhausting. – 2012User Apr 21 at 18:11
  • If you solder a Serial Interface Board or I2C LCD "backpack" to the LCD, then there are only 4 wires to connect :) aliexpress.com/item/… – VE7JRO Apr 21 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.