LED unresponsive when sending data from Processing

Hi everyone,

I'm fairly new to Arduino and have been playing around with the communication end.

I'm trying to send data from Processing software to the Arduino to control an LED. I'm using MacOs 11.4, Arduino 1.8.15 and Processing 3.5.4.

When I click on the Processing display window, I expect the LED to turn on. When the Processing display window is not clicked, I expect the LED to turn off.

I receive no errors, but when I click the Processing display window the LED does not respond. I have checked the Processing software is connected to the Arduino port, and the Arduino sketch has been successfully uploaded to the Arduino. I've also ran this chunk of code sudo mkdir -p /var/lock sudo chmod 777 /var/lock that seems to have helped some others. Unfortunately, I still can't get it to work.

The Arduino is using port /dev/cu.usbmodem14201 and Processing is using port /dev/tty.usbmodem14201. I have tried changing the Processing port to /dev/cu/usbmodem14201, but to no avail.

The Arduino sketch is as follows:

 char val; // Data received from the serial port
 int ledPin = 13; // Set the pin to digital I/O 13
 void setup() {
   pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
   Serial.begin(9600); // Start serial communication at 9600 bps
 }
 void loop() {
   if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if (val == '1') 
   { // If 1 was received
     digitalWrite(ledPin, HIGH); // turn the LED on
   } else {
     digitalWrite(ledPin, LOW); // otherwise turn it off
   }
   delay(10); // Wait 10 milliseconds for next reading
}

And the Processing sketch is:

import processing.serial.*;

Serial myPort;  // Create object from Serial class


void setup() 
{
  println(Serial.list());
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  if (mousePressed == true) 
  {                           //if we clicked in the window
   myPort.write('1');         //send a 1
   //println("1");   
  } else 
  {                           //otherwise
  myPort.write('0');          //send a 0
  }   
}

If anyone can offer some advice, I would greatly appreciate it!

Thanks so much for your time!

Nice job with code tags and well formatted code on your first posting.

The Arduino sketch works as intended with input from the serial monitor if it is set for no line ending.

If there is any line ending like '\n' or '\r' it is seen as a new character other than '1' and the led goes off 10ms after it goes on, and it is just a brief blink.

The issue must be on the processing side, either with the definition and opening of the port, or with a line ending appended to the message.

I'm not familiar with processing, so I can't really tell you what to check.

1 Like

I tested both sketch.
However, I changed serial port number, but it necessary for test with my environment.

  • Windows 7 & Arduino Nano
  • Arduino IDE 1.8.13
  • Processing 3.4

Compile done success at both IDE, and completery worked.
Couldn't find the problem. :thinking:

1 Like

Thank you both for your replies. I’m glad to hear it worked on Arduino and also on Windows.

Just to clarify it isn't an issue with my hardware setup, would a USB connection be sufficient for a serial connection?

If USB serial isn't working, it means you can't upload sketches.
But It isn't, you can success upload sketch. It means that USB serial is available.
I've never used an Apple product for religious reasons and I don't know anything about Mac.
So I can only give report #3.

I'm not really able to help you.
I'm a Windows user, and my PC coding is in Python. So MAC and processing are two steps removed. :frowning_face:

You could try to use a terminal app like Cool Term or Real Term to test communications. They will have a simple usb connection between the pc and the arduino. I believe they have Mac versions.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.