Serial Monitor affected by Serial.Read?

Hey guys, I'm using keystrokes in Processing to control a stepper motor through Arduino. Simply put, when i press the s key on my keyboard, Processing will myPort.write an 's' over the serial port to Arduino which executes a functions for the stepper motor. Arduino is reading it with:

if(Serial.available()) {  // If data is available to read,     
             char val = Serial.read(); // read it and store it in val
              }

counter++:   

if (val == 's') { 

	Serial.println(counter);
}

the serial monitor is where i run into issues. either serial.available() or serial.read() seems to interfere with Arduinos ability to Serial.print correctly. the serial monitor will randomly write an incomplete or partial line or print the value with some of the next lines letters tacked on the end, and so on. Its all over the place and jittery.

example;

position
p
pos
p
p
position: 20po
sition: 22position:

it seems like when Arduino is reading serial data from Processing it messes with the data i'm trying to print (Serial.print) in the serial monitor. Almost like the Arduino is “listening” too much and its messing ability to print to the serial monitor correctly. I suspect there is some way free up the serial port so Serial.read will allow Serial.print to send data uninterrupted or whole. Or it could be something else completely? Any help would be much appreciated.

Dan

I don't immediately see why you should have a problem.

Post your complete Arduino code and an explanation of what Processing is doing.

...R

First load to arduino, then open arduino serial monitor. Over in Processing you can now run its sketch, a small grey window appears, nothing else its just for the keyboard functionality at this point. as you press z you will see scattered data appear in the serial monitor. For debugging and such i'd like the Serial Monitor issue to be cleared up, eventually i will have data sent back to processing for display.

On the Arduino:

char val;
long a;

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

void loop(){
  if(Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val  
   }
   
  a++;
    
    if (val == 'z') { 
      Serial.print("a = ");
      Serial.println(a); 
      } 
}

And in Processing:

import processing.serial.*;
Serial myPort;  // Create object from Serial class



void setup() 
{
  size(200, 200); 
//change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, Serial.list()[5], 9600);
}

void draw() {

  background(80);
}

void keyPressed() {
  if (key == 'z')         
  {                           
    myPort.write('z');         
    println("z");
    myPort.write('0');         
    println("0");
  }
}

If there's no serial input available you process it as if there was. Try this:

  if(Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val  
     a++;
      Serial.print("a = ");
      Serial.println(a); 
    }

Pete

thanks Pete, when i use the code you posted, I actually end up getting even shorter broken up print outs in the serial monitor than before.

Also, I was putting Serial.print inside the if(val == 'z') statement so that down the road different key strokes can tell arduino to Serial.print different variables.

My other suspect area is in Processing. I have a keystroke send myPort.write 'z' immediately followed by an arbitrary myPort.write '0'. The zero is unspecified/unrecognized in Arduino, resulting in no action, otherwise the 'z' command in will infinitely be executed in Arduino. Is there a way to have the keystroke in Processing myPort.write 'z' once then not send anything until the next keystroke?. Or inversely is there a way to have Arduino read the char from processing only once, until another char is sent?

Do you have serial monitor and the processing app talking to the same com port?

Does your Arduino code work properly if you just send it data with the Serial Monitor?

If it does, it suggests the problem lies with the Processing code.

end up getting even shorter broken up print outs in the serial monitor than before.

I don't understand this at all.

How can you have the Serial Monitor and Processing connected to the Arduino at the same time?

Can you take a screen shot of the "broken up print outs" and post it?

...R

Hey thank you to everyone for the help and such speedy responses.

Robin2 ,
If I remove the Serial.available/Serial.read portion of the Arduino code the Serial Monitor will print fine but then I have no communication with Processing.

my apologies for the poor use of words. by "end up getting even shorter broken up print outs in the serial monitor than before. " I meant in the Serial Monitor I was originally seeing:

position
p
pos
p
p
position: 20po
sition: 22position:

And with your piece of code I even get shorter incomplete data in the serial monitor:

po
p
pos1
p11
p
po
P22

Groundfungus,
in Arduino I am using /dev/tty.usbmodem2013 to upload, i haven't seen any other options for Serial.read/Serial.available on Arduino. In processing you choose via array number from Serial.list[ ]. On differant computers you have to find out which number is the same port you are using in Arduino. Thats as far as i understand the serial communication between the two.

Dshep101:
Robin2 ,
If I remove the Serial.available/Serial.read portion of the Arduino code the Serial Monitor will print fine but then I have no communication with Processing.

I didn't ask you about that, and you didn't answer the first question I asked - whether the Arduino code works properly with the Serial Monitor (and without Processing).

You also didn't explain how you can have Processing and the Serial Monitor connected at the same time.

If I am to be able to help I need to build up a picture of your arrangements by getting accurate answers to my questions.

...R