HC-05/Arduino receiving spam data I connected everything properly. On the first try I did receive data as  I was supposed to. I then wrote some logic, re-uploaded and started receiving crazy spam. 

My serial monitor is getting spammed with:


question marks if the variable is char, or;
25252525252 if the variable is int, or;
-1 if variable is String.


I'm monitoring at 38400 baud which is the same as my Serial.begin().

Any ideas ?

Code:

#include &lt;Wire.h&gt;
#include &lt;Adafruit_PWMServoDriver.h&gt;

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50

char data = 'a';

void setup()
{
  analogReference(DEFAULT);

  Serial.begin(38400);

  pwm.begin();
  pwm.setPWMFreq(FREQUENCY);
  Serial.println("hello world");
}

void loop()
{
  data = Serial.read();
  Serial.println(data);
  Serial.flush();
  if (data == 'a'){
    pwm.setPWM(0, 0, pulseWidth(90));
  }
  if (data == 's'){
    pwm.setPWM(0, 0, pulseWidth(30));
  }
  delay(1000);
  //printing time in seconds  
}

int pulseWidth(int angle)
{
  int pulse_wide, analog_value;
  pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);

  return analog_value;
}
Serial.read() returns -1 if no data is available. It doesn't wait for the byte. Test for -1 or use Serial.available() as any example does.

Replace Serial.print(data) with Serial.write(data) to get the data echoed back unchanged.