Please help: can not connect with Arduino serial port

Tins Su's icon

Hi, I am working on my master project and using Max for the first time.

I would like to use a touch sensor to control the play and stop of a music file on my laptop, so I connect the sensor to the Arduino board and want to use Max for playing the music. However, my Max console can find my serial port connected but cannot print any information sent from Arduino. I don't know why, please help!

Here is my Max code:

by clicking on the toggles, I can manually control the play and stop of the music, but I cannot use sensor to control it.

Here is my Arduino code:

#define TOUCH_SENSOR_PIN 2 // Change to the pin you have the touch sensor connected to

void setup() {
  Serial.begin(9600); // Start serial communication at 9600 baud rate
  pinMode(TOUCH_SENSOR_PIN, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int touchState = digitalRead(TOUCH_SENSOR_PIN); // Read the touch sensor state

  // If the sensor is touched, send a signal
  if (touchState == HIGH) {
    Serial.println("play"); // Send play command
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500); // Debounce delay
  }
  if (touchState == LOW) {
    Serial.println("stop"); // Send play command
    digitalWrite(LED_BUILTIN, LOW);
    delay(500); // Debounce delay
  }
  delay(10); // Short delay to prevent overwhelming the serial port
}

When I clicked the print message on the first line, here is the console:

Thank you guys so much!!!!!

Tins Su's icon

I add this metro part and it is now getting numbers

Source Audio's icon

metro 2400 Hz ??

do you realise what that makes in ms ?

you send in shortest interval of 500 ms, but poll at 1000/2400 = 0.4166 ... ms

then, you send "play" and "stop",

but arduino sends ascii numbers which you need to convert

into chars again, and also catch newline, which is sent

by println in your code.

if you want to go that simple then use

Serial.write(1); and Serial.write(0); instead

you don't need metro, use message poll with wished interval,

like poll 10 or poll 50

next mistake :

serial baud set to 9600 in arduino, 115200 in max ?

to continue ...

your code will send at 500ms interval repeatedly same value

which would cause sfplay to restart in 500 ms interval when touch = HIGH

and stop when touch = LOW

what kind of play/stop logic do you intend to use ?

maybe toggle play state when touch gets HIGH ?