Problem with receiving correct data over serial
Hi
I am working on a project using an arduino with proximity sensor to control start and stop playback of audio depending on how close the viewer is.
the arduino is setup to send "play" and "stop" over serial, in the arduino serial monitor and in processing that is whats received but in max all I get is strings of numbers that`s not corresponding to distance from the sensor.
attaching both arduino code and a test patch
[code]
int inputPin=2; //ECHO pin
int outputPin=4; //TRIG pin
void setup()
{
Serial.begin(9600);
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
}
void loop()
{
digitalWrite(outputPin, HIGH); //Trigger ultrasonic detection
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
int distance = pulseIn(inputPin, HIGH); //Read ultrasonic reflection
distance= distance/58; //Calculate distance
if(distance < 30) {
Serial.println("play"); } //
delay(100);
if(distance > 30) {
Serial.println("stop"); } //
delay(100);
}
[/code]
In max You need to convert ascii string to readable format.
If that play or stop is the only thing Arduino should be sending to Max
than better choice would be to send 1 for play and 0 for stop.
And pass it through change in max.
Numbers can be sent as chars so no need to convert from ascii in Max.
Serial.write(1); for play Serial.write(0); for stop
And I would optimise arduino code to send values only if state changed.
------------------------------------
int inputPin=2; //ECHO pin
int outputPin=4; //TRIG pin
int State = 0; // current state
int lastState = 0; // previous state
void setup() {Serial.begin(9600); pinMode(inputPin, INPUT); pinMode(outputPin, OUTPUT); }
void loop() {
digitalWrite(outputPin, HIGH); //Trigger ultrasonic detection
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
int distance = pulseIn(inputPin, HIGH); //Read ultrasonic reflection
distance= distance/58; //Calculate distance
if(distance < 30) {State = 1;}
if(distance > 30) {State = 0;}
if (State != lastState) { Serial.write(State); }
lastState = State;
delay(100);
}
-------------------------
In that case there is no need for change object in Max
Thank you very much!!! that arduino code fixed it for me!!!!