Problems reading SERIAL data with Arduino - freezing and random numbers
I am running an Arduino UNO with a HC-SR04 ultrasonic distance sensor. The Arduino sketch is attached, using the NewPing library. When the Arduino sketch is running solo, the program does as it is supposed to and continuously prints integers to the Serial Monitor.
My aim is to read these integers into MaxMSP. So I open Max and create a 'serial' object set to the same serial port, with a metro and toggle (screen shot attached). As soon as the toggle is triggered Max prints strange integers - namely repeated 10s, 50s and 47s. When using the 'atoi' object pairs of 49/48 are produced, whilst the Arduino Serial Monitor continues to produce the expected smoothly changing values. How-oh-how do I get those same smooth numbers into a Max number box.
Any thoughts?
Happy to elaborate further, thanks in advance,
ARDUINO SKETCH:
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
// Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.print('\n'); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.println("cm");
}
try the following changes to your Arduino sketch :
// —————————————————————————
// Example NewPing library sketch that does a ping about 20 times per second.
// —————————————————————————
#include
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.println(uS / US_ROUNDTRIP_CM);
}
Serial.println sends a newline character (and a carriage return) for you. No need to break it out separately.
and the max patch :
You're receiving ASCII encoded values from the Arduino and interpreting them as a bytes. using the newline char (ascii 10), you can identify when a full packet of information has arrived in Max.
The Arduino serial monitor expects ASCII encoded values by default and does that interpretation for you.
mac or Pc
looks like you need to defin baud r speed.
try this patch ( mac serial )
and this arduino code
/*
HC-SR04 Ping distance sensor]
Echo to Arduino pin 13 Trig to Arduino pin 12
led 1 to Arduino pin 11
led2 to Arduino pin 10
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (57600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print("P ");
Serial.println(distance);
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance
And how do you do this with a ping))) sensor from Parallax (GND, 5V, Digital Signal)?
cheers,
fridtjof