printing serial input in processing & arduino
in
Integration and Hardware
•
7 months ago
Hi,
I have a very simple script in Arduino that reads user input and writes it back to the serial monitor. I'm trying to read the value in Processing (using port.readString()). However, it seems like half the time the value is printed out in the Processing console and the other half it's printed out in Arduino. Is there any way to capture the full, uninterrupted value of the input in Processing? My code is below for reference. Note that right now I'm just printing out the character 'a' for the sake of debugging.
Thanks!
Processing code
- import processing.serial.*;
- import cc.arduino.*;
- //Arduino arduino;
- String myString = null;
- int roundNum=0;
- Serial port;
- int val;
- void setup(){
- port = new Serial(this, Serial.list()[0], 9600);
- }
- void draw(){
- while(port.available()>0 && roundNum<6){
- String inBuffer = port.readString();
- if(inBuffer !=null){
- println(inBuffer);
- }
- myString+= inBuffer;
- roundNum += 1;
- }
- if(roundNum==5){
- println("Final string: " + myString);
- }
- String text="";
- boolean done = false;
- void setup(){
- Serial.begin(9600);
- }
- void loop(){
- while(Serial.available()){
- char next = (char)Serial.read();
- if(next=='\n'){
- done=true;
- continue;
- }
- text+=next;
- }
- if(done){
- Serial.write('a');
- text="";
- done=false;
- }
- }
1