Can't transfer an integer to Processing

I have an Pico spitting out azimuth integers via Serial.write(azimuth). Before using Serial.write(azimuth), I checked with Serial.print(azimuth) on my serial monitor and it shows the correct sequence of values on the monitor.

I need to read these azimuth integers on Processing, using the serial port, to display on a radar-like display. On Processing, my code is:

void draw(){
    if (azimuth.available()>0) {   //checking whether there is incoming serial values
       currentAzimuth=azimuth.read();
    }
    (some other code)
    print(currentAzimuth);
}

At the Arduino end, azimuth in Serial.write(azimuth) is an INT
At the Processing end, currentAzimuth is an INT

I receive the azimuth in sequence with the following errors and observations:

  1. Sometimes the same azimuth will get received twice or even thrice.
  2. In one cycle, the azimuth will change from 0 to 255 and suddenly back to 0 and begin counting up again from 0. It the next cycle, it will change from 103 to zero, when the correct sequence is 0 to 359. Serial bitrate is 115200 and the time between successive azimuth values from the Pico, is about 28 milliseconds.
  3. If I use Serial.println(azimuth) on the Arduino, I get a perfect count from 0 to 359, but this crazy sequence when I use Serial.write(azimuth).

Considering that Arduino is sending the ASCII for correctly in the form Serial.println(azimuth), can anyone help me to capture the ASCII form numbers and convert them to INT at the Processing end?

Thank you in advance.

Hi,
I have a sketch I developed when trying to do this type of thing myself which may be of help/interest?

With a print() or println() the Arduino outputs numbers in ASCII so that 253 would be three bytes representing the characters '2', '5', and '3'. I don't know how to handle that at the Processing end. If you can help me on the Processing end, println(int, DEC) is sending data perfectly.

I tried this code with println(int, DEC), but it throws an error...

void draw(){ 
  if (azimuth.available()>0){ //checking whether there is incomming serial values
    azimuthString = azimuth.readStringUntil('\n');
    //println(azimuthString);
    currentAzimuth = Integer.parseInt(azimuthString);
    print(currentAzimuth);
    (other code)
}

Is the error a message a secret or will you share it with others who are trying to help you?

Post your code.

"It doesn't work" tells me Poo!

Post images of your wired project.

POST ALL YOUR CODE.

Nothing secret about the code but for it to make any sense, I will also have to send you two png files, which this forum doesn't seem to support.

I am posting Arduino code, as under...

int azimuth = 0;
int count = 0;

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

void loop() {
  for(int i=0; i<360; i++){
    Serial.write(i);
    //Serial.println(i);
    delayMicroseconds(28000);
  }
}

And the entire Processing code this interacts with... However, the important thing here, is to see the azimuth values that are streaming in from the Arduino via Serial.write() - in one cycle they go from 0 to 103 and back to 0. in the next cycle they go from 0 to 255 and back to 0, ad infinitum, when they should just keep cycling from 0 to 359 and so on...

import processing.serial.*; //importing serial library 
PImage beam; // to store timebase image
PImage display; // to store radar rose image 
float increment;
int previousAzimuth = 0;
int currentAzimuth = 0;
int plottedAzimuth;
String azimuthString;
Serial azimuth; //naming our serial port as "azimuth"

void setup(){ //only runs one time when program starts
  background(0); //making background color as black
  size(1000, 1000); //output window size 1000 x 1000 pixel
  //fullScreen(); //output window size full screen
  beam=loadImage("RadarBeam.png"); //loading timebase image
  display=loadImage("RadarDisplay.png"); //loading radar rose image
  //printArray(Serial.list()); //listing serial port to find the port in which arduino is connected
  azimuth = new Serial(this, Serial.list()[0], 115200); //initializing port  
}

void draw(){ //this will loop by 60 frame per second by default , frame rate can be changed by frameRate() function
  if (azimuth.available()>0){ //checking whether there is incomming serial values
    currentAzimuth=azimuth.read(); //storing incoming values to variable inByte
    if (currentAzimuth != previousAzimuth){
      plottedAzimuth = currentAzimuth;
      previousAzimuth = currentAzimuth;
    }
    else{
      plottedAzimuth = previousAzimuth;
    }
  }
  
  imageMode(CENTER); //draw image using center mode
  image(display, width/2, height/2); //drawing meter image 
  pushMatrix(); //saving current transformation matrix onto the matrix stack
  imageMode(CORNERS); //draw image using corners mode
  translate((width/2)+0, (height/2)+2); //translating "(width/2)+2, (height/2)+15" to "0,0"
  //rotate(((HALF_PI)-19.69)+ plottedAzimuth); //rotation needle image to position to zero in meter image
  rotate(-(HALF_PI) + radians(currentAzimuth));
  image(beam, 0, 0, 350, 2); //drawing needle image
  popMatrix();//removing the current transformation matrix off the matrix stack
  
  println(currentAzimuth);
}

Here is the Arduino code again:

int azimuth = 0;

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

void loop() {
  for(int i=0; i<360; i++){
    Serial.write(i);
    //Serial.println(i);
    delayMicroseconds(28000);
  }
}

And simplified Processing code:

import processing.serial.*; //importing serial library 

int previousAzimuth = 0;
int currentAzimuth = 0;
int plottedAzimuth;
Serial azimuth;

void setup(){ //only runs one time when program starts
  background(0); //making background color as black
  size(100, 100); //output window size 1000 x 1000 pixel
  //printArray(Serial.list()); //listing serial port to find the port in which arduino is connected
  azimuth = new Serial(this, Serial.list()[0], 115200); //initializing port /dev/ttyACM0
}

void draw(){ //this will loop by 60 frame per second by default , frame rate can be changed by frameRate() function
  if (azimuth.available()>0){ //checking whether there is incomming serial values
    currentAzimuth=azimuth.read(); //storing incoming values to variable inByte
    if (currentAzimuth != previousAzimuth){
      plottedAzimuth = currentAzimuth;
      previousAzimuth = currentAzimuth;
    }
    else{
      plottedAzimuth = previousAzimuth;
    }
  }
  
  println(currentAzimuth);
}

How many serial ports does the MCU you are using have? You are sending data to the Serial monitor but not to another serial port that can be then transferred to another MCU.

For example, the little snippet below uses 3 hardware serial ports.


  Serial.begin( SerialDataBits );
  SerialBrain.begin( SerialDataBits );
  SerialTFMini.begin(  SerialDataBits, SERIAL_8N1, 27, 26 );

The serial port Serial is being used to send data to the monitor.
The serial port SerialBrain is being used to send data to another MCU.
The serial port SerialTFMini is being used to receive data from a connected module.

If you want to send data from the Pico to another MCU over serial then you will want to search the internet on using the other serial ports on a Pico or whatever else MCU you are using.

I'm not sending to a serial monitor. I'm sending this over USB, from a Pico to a PC running the Processing code. If you run the simplified Processing code I have posted, you will see the strange error I speak of...

However, if you view the output of the above program on a serial monitor as well, you will see the breaks at 103 and 255. The best is to view this on Putty in hex mode, instead of the simple Arduino serial monitor...

I have found a solution to this problem by sending the azimuth data from Arduino to Processing in ASCII form and then parsing it in Processing, instead of in binary form as I was doing earlier. It is computationally expensive, but works. I will determine the cause of the strange behaviour with binary data later.

Thank you all for the interest and effort you have taken in trying to solve my problem...

Regards.

RT

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.