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!