1
votes

My Arduino Code:

#include<EngduinoThermistor.h>
void setup()
{
  Serial.begin(9600);
  EngduinoThermistor.begin();
}void loop()
{
  float temp;
  temp = EngduinoThermistor.temperature();
  Serial.println(temp);
  delay(1000);
}

My processing code:

 import processing.serial.*;
    Serial port;
    float x = 0;

void setup() {
  size(600, 200);
  smooth();
  background(#9F9694);
  String port = Serial.list()[3];
  port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
}

void draw() {
  stroke(50, 50, 50);
  float inByte = port.read();
  stroke(#791F33);
  strokeWeight(3);
  line(x, height, x, height-inByte); 
  print(inByte);
  if (x >=width) {
    x=0;
    background(255);
  }
  x++;
}

Here, I am unable to send the data on my arduino to processing as there is always an error saying: Type mismatch processing.serial.Serial does not match with java.lang.String because of the two statements:

  String port = Serial.list()[3];
  port = new Serial(this, "/dev/tty.usbmodem1411", 9600);

I looked it up online but everyone use the same code to connect arduino with processing. What is the problem of my processing code? The arduino code works properly.

2

2 Answers

0
votes

This doesn't make sense:

String port = Serial.list()[3];
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);

You're creating a String variable named port. That means that the port variable can only point to String values. Then in the next line you try to point it to an instance of Serial. An instance of Serial is not an instance of String, which is why you get the error.

You need to split your String variable and your Serial variable up into two separate variables. Something like this:

String portString = Serial.list()[3];
Serial portSerial = new Serial(this, "/dev/tty.usbmodem1411", 9600);

Please also note that this is exactly how every example in the Serial documentation does it.

0
votes

Kevin's done an excellent job at explaining what the syntax error you got means and how to tackle it.

In addition to that I'd like to point out an issue I spotted in Simon's question as well:

float inByte = port.read();

According to Serial's documentaiton on read():

Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first cheacking available() to see if data is available.

There are couple of issues:

  1. EngduinoThermistor returns temperature as a float and that is not what read() returns. It returns one byte at a time. Let's say the temperature is 25.71. That will be 5 bytes (2 5 . 7 1 which as bytes represented from 0 to 255 would be 50 53 46 55 49). You would need to convert each byte to a character then append each character to a String until the new line (\n) character is found, then the byte counter would reset. After the full string is put together, it can be converted to a float. I recommend using either readStringUntil() or a combination of bufferUntil() and readString()
  2. You are not checking if read() returns -1 which you should always do unless you use serialEvent()

I recommend double checking if the data is displayed correctly in Arduino's Serial Monitor first. If it is, close Serial Monitor (as you can open a single connection to a serial port at a time), then run your Arduino code.

You can also take advantage of this one weird trick: Arduino already knows what serial port and baud rate you're using and saves this as a preference (so you don't have to select the port every single time you restart it). This is actually stored in a Java Properties format that you can easily parse in Processing.

You can easily find the location to this file from the Arduino Preferences panel: Arduino Preferences

Here's a basic sketch that tries to read the serial port name and baud rate from Arduino preferences then read a string and print it to the console (while trying to handle some errors):

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import processing.serial.*;

String portName = null;
int baudRate = 9600;

Serial arduino;

void setup(){
  readArduinoPrefs("/Users/George/Library/Arduino15/preferences.txt");
  println("last used Arduino port",portName,"with baud rate",baudRate);

  try{
    arduino = new Serial(this,portName,baudRate);
  }catch(Exception e){
    System.err.println("ERROR connecting to Arduino port " + portName);
    String message = e.getMessage();
    if(message.contains("not found")) println("Please make sure your Arduino is connected via USB!");
    if(message.contains("busy")) println("Please make sure the port isn't already opened/used by Serial Monitor or other programs!");
    e.printStackTrace();
    exit();
  }
}
void draw(){
}
void serialEvent(Serial s){
  println("received string",s.readStringUntil('\n'));
}
void readArduinoPrefs(String prefsPath){
  Properties prop = new Properties();
  InputStream input = null;

  try {

    input = new FileInputStream(prefsPath);

    prop.load(input);

    portName = prop.getProperty("serial.port");
    baudRate = int(prop.getProperty("serial.debug_rate"));

  } catch (IOException ex) {
    ex.printStackTrace();
  } finally {
    if (input != null) {
      try {
        input.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

}

On the pragmatic side, you can always type in the port name and baud rate as you configure it in Arduino manually, just remember to double check the syntax, in your case:

Serial(parent, portName, baudRate)

e.g.

   new Serial(this, "/dev/tty.usbmodem1411", 9600);

as Kevin pointed out