Receiving integer from arduino

Hello, I’m a very beginner.
I would like to send the value of the potentiometer connected to Arduino to the openframeworks.

The size of the numbers increase or decrease depending on my potentiometer manupulating.
However, the numbers are wrong and only numbers from 1 to 12 are printed. The numbers should be 0 to 1023.

I don’t think openframeworks is getting the numbers right.

I think there is something wrong in update() but I don’t know what to do.

arduino code

int sensorPin = A0;
void setup() {
  Serial.begin(9600);
  pinMode(sensorPin,INPUT);
  

}

void loop() {
  int value = analogRead(sensorPin);
  Serial.print(value);
  delay(100);

}

ofApp.cpp

#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
	bSendSerialMessage = false;


	serial.setup("COM3", 9600);
}

//--------------------------------------------------------------
void ofApp::update(){
	if (bSendSerialMessage) {
		unsigned char bytesReturned[NUM_BYTES];
		
		int result = serial.readBytes(bytesReturned, NUM_BYTES);
		printf("%d\n", result);
	}

	bSendSerialMessage = false;

	countCycles++;
	if (countCycles == 10) {
		bSendSerialMessage = true;
		countCycles = 0;
	}
	 
}

ofApp.h

#pragma once

#include "ofMain.h"

#define NUM_BYTES 16
class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();

		bool bSendSerialMessage;
		

		int countCycles;
		ofSerial serial;
};

Hi,

The problem is that result is actually the number of bytes read.
The actual message is stored (as individual bytes) in the array bytesReturned.
So that’s where you should look.
Next thing is that Arduino sends the number 999 as three individual bytes (with a simple Serial.print): ‘9’, ‘9’ and ‘9’.
Here’s a way to construct these three (or two, or one) bytes into an integer (via a string):

unsigned char bytesReturned[NUM_BYTES];
memset(bytesReturned, 0, NUM_BYTES); // Setting all values to 0 (so the string will be null-terminated)
int result = serial.readBytes(bytesReturned, NUM_BYTES); // Array is now: ['9', '9', '9', 0, 0, 0, 0, 0]
string msg = string(reinterpret_cast<char*>(bytesReturned));
int number = ofToInt(msg);

Not sure if this is the easiest way, but this comes to mind.
Also: when you’re Arduino value isn’t going to be longer than 4 digits (bytes), the NUM_BYTES could be set to 4.

I haven’t tested the code, so let me know if it doesn’t work. Good luck.

Thank you very much for your help. I’ve printed out the numbers, and something’s a little weird. But to some extent, it’s printing the right numbers. I don’t know why it’s making strange noise…

But It’s been quite solved by increasing the delay value in arduino! thanks again

Ah,
The nonsense numbers are coming from reading when the Arduino hasn’t send anything yet.
Try this:

if(serial.available()) { // Read when there is at least 1 byte available
	unsigned char bytesReturned[NUM_BYTES];
	memset(bytesReturned, 0, NUM_BYTES); // Setting all values to 0 (so the string will be null-terminated)
	int result = serial.readBytes(bytesReturned, NUM_BYTES); // Array is now: ['9', '9', '9', 0, 0, 0, 0, 0]
	string msg = string(reinterpret_cast<char*>(bytesReturned));
	int number = ofToInt(msg);
}

And the Arduino is making noise? :sweat_smile:

Thank you for your kind explanation! I think it’s working correctly.
the noise is… I think I misrepresented it because I’m not a good English speaker.You don’t have to care about it, sorry :sweat_smile: