2
votes

I have an Arduino Mega2560 which reads analog values from a sensor (Grove Loudness Sensor) and sends them to a Raspberry Pie 3b+ via USB. The values arrive perfectly and without delay in the serial monitor of the Arduino IDE. But when I try to receive them in Processing I get mostly Zeros no matter how much noise I make, with random peaks here and there.

My first guess was that my Processing code garbles the received data, so I changed the Arduino sketch to send an incrementing Integer instead of the sensor value - and those test values show correctly in Processing! Next I tried padding the sensor value in some String like "MICRO("+theSensorValue+");". The serial monitor shows the whole String and the correct sensor value, Processing shows the whole String but the value inside is still garbage..

This doesn't make any sense to me. Processing can receive any value from the Arduino correctly, except when it's the sensor value...

Arduino:

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

void loop() {
  int micLevel = analogRead(A0);
  Serial.println(micLevel,DEC);
}

Processing:

Serial port;

void setup() {
  port = new Serial(this, "/dev/ttyACM0", 9600);
}

void draw() {
  String s = "";
  if(port.available() > 0) {  
     s = port.readStringUntil(10); 
     println(s);
  }
} 
1

1 Answers

-1
votes

In Serial.println(micLevel,DEC); why are you casting the value as decimal? Try to just Serial.println(micLevel); or convert to String if you want to send text with it Serial.println("Value is: " + String(micLevel));