1
votes

I'm attempting to create a data exchange between sensors on an Ardunio (well actually a AtMega328PU based clone built on a breadboard, but I don't believe that that's the source of my problem) and a Processing script, and I'm getting some unexpected results. I was following the method of serial communication detailed here, but I get stuck with what appears to be no data actually traveling over the serial connection.

The Arduino code:

int buttonPin = 9, photodiodePin = A0;
char dataToSend;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, OUTPUT);
  pinMode(photodiodePin, INPUT);
}

void loop() {  
  if (Serial.available() > 0)
  {
    dataToSend = Serial.read();    

    if (dataToSend == 'B')
    {
      Serial.println(digitalRead(buttonPin));
    }
    else if (dataToSend == 'L')
    {
      Serial.println(analogRead(photodiodePin));
    }
  }
}

And the relevant parts of the Processing code:

import processing.serial.*;

Serial myPort;

void setup()
{
 size(1600,900);

 String portName = Serial.list()[1];
 myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if(getButtonData() == 1)
  {
    //Do Stuff
  }
}

int getLightData()
{
  myPort.write('L');
  println("L");
  while(!(myPort.available() > 0)){
  }
  int lightValue = int(myPort.readStringUntil('\n'));
  return lightValue;
}

int getButtonData()
{
  myPort.write('B');
  println("B");
  while(!(myPort.available() > 0)){
    println("stuck in here");
    delay(500);
  }
  int buttonValue = int(myPort.readStringUntil('\n'));
  return buttonValue;
}

And in Processing I get an output of:

B
stuck in here
stuck in here
stuck in here
stuck in here
...

Note: I know I have the correct port selected from the list, so that is not the issue.

I've tried debugging the issue and searching the internet for similar problems, both to no avail. Any help on this issue is greatly appreciated, Thanks!

1
You don't need to put the ".available" check in a while (in getButtonData) because the draw method is called in a loop until the program is stopped. You need to get rid of the ! in that while available check and only do things if and when data is available. Adapt the sending of the B accordingly. Do the same for getLightData.Blurry Sterk
First test the serial comms first without the rest of your code. In the Draw method write to your serial port and see if you can get anything at the Arduino side. Also read some documentaion on how the Serial is setup in Processing. Make sure of baud rates, stop bits, and the rest of the settings aspecially flow control.Blurry Sterk
Also make sure Processing is actually sending the B as just one byte and not in another form which may entail multi-byte.Blurry Sterk

1 Answers

0
votes

I would first check if simply sending a simple message from Arduino to Processing works. Try something like this in Arduino:

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("test");
  delay(1000);
}

and this in Processing:

import processing.serial.*;


Serial arduino;
void setup(){
  String portName = Serial.list()[1];
  try{
    arduino = new Serial(this, portName, 9600);
    arduino.bufferUntil('\n');
  }catch(Exception e){
    System.err.println("Error initializing Serial port!\nPlease check connections and port settings");
    e.printStackTrace();
  }
}
void draw(){

}
void serialEvent(Serial s){
  println("received arduino message: " + s.readString());
}

If this simple setup works, you should be fine to move on to the next step and have two way communication. If the above doesn't work then there's something else causing issues with serial communication (in which case please report what errors you're receiving, if any or what behaviour you're experiencing)

In case the above works, you can try two way communication. Notice I'm making use of serialEvent() and bufferUntil. It's best to avoid blocking while loops wherever possible.

Arduino code:

int buttonPin = 9, photodiodePin = A0;
char dataToSend;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, OUTPUT);
  pinMode(photodiodePin, INPUT);
}

void loop() {  

}
void serialEvent(){
  if(Serial.available() > 0){
    char cmd = Serial.read();
    if(cmd == 'B'){
      Serial.println(digitalRead(buttonPin));
    }
    if(cmd == 'L'){
      Serial.println(analogRead(photodiodePin));
    }
  }
}

Processing code:

import processing.serial.*;


Serial arduino;

int digitalValue;
int analogValue;

int lastCMD;

void setup(){
  size(200,100);
  String portName = Serial.list()[1];
  try{
    arduino = new Serial(this, portName, 9600);
    arduino.bufferUntil('\n');
  }catch(Exception e){
    System.err.println("Error initializing Serial port!\nPlease check connections and port settings");
    e.printStackTrace();
  }
}
void draw(){
  background(0);
  text("Press 'B' to read digital pin\nPress 'L' to read analog pin\n\n"+
       "digital pin: " + digitalValue + "\nanalog pin: " + analogValue,10,25);
}
void keyReleased(){
  if(key == 'B' || key == 'L'){
    lastCMD = key;
    println("sending command" + lastCMD);

    if(arduino != null){
      arduino.write(lastCMD);
    }else println("Arduino is not initializing, not writing to serial port");
  } 
}
void serialEvent(Serial s){
  String rawString = s.readString();
  println("received arduino message" + rawString);
  try{
    rawString = rawString.trim();//remove any white space characters (if present)
    if(lastCMD == 'B') digitalValue = int(rawString);
    if(lastCMD == 'L') analogValue  = int(rawString);
  }catch(Exception e){
    println("Error parsing String from serial port:");
    e.printStackTrace();
  }

}