Dear all,
I have just started experiencing with Processing, and ran into the following problem.
I am trying to blink the built-in LED on an Arduino board (Mega2560), and am experiencing different behavior between the Processing GUI and the Arduino Serial Monitor.
What my code tries to do is blink the LED with a delay of 2000 when button ‘a’ is pressed, and do the same with a delay of 50 when button ‘z’ is pressed.
My code is working as expected when inputting the char ‘a’ or ‘z’ in the Serial Monitor in Arduino IDE, but there are problems when trying to use the Processing GUI. In the latter case, the LED is not blinking and the TX led on the Arduino is stuck (constantly lit).
Here are my codes:
Arduino:
int pinLed = 13;
char option;
int del = 2000;
void setup() {
// put your setup code here, to run once:
pinMode(pinLed, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available())
{
option = Serial.read();
}
switch (option)
{
case 'a':
del = 2000;
Serial.println("a");
break;
case 'z':
del = 50;
Serial.println("z");
break;
default:
digitalWrite(pinLed, HIGH);
delay(del);
digitalWrite(pinLed, LOW);
delay(del);
break;
}
}
Processing:
import processing.serial.*;
import controlP5.*; // import controlP5 library
Serial myPort;
String val;
ControlP5 cp5; // create controlP5 object
PFont font;
PFont titlefont;
void setup()
{
size(600, 900); // window size (width, height)
printArray(Serial.list()); // prints all available serial ports
myPort = new Serial(this, "COM3", 9600); // Arduino connected to COM3 port
cp5 = new ControlP5(this);
font = createFont("calibri light", 20); // custom fonts for buttons
titlefont = createFont("calibri light", 40); // for title
cp5.addButton("a") // button name
.setPosition(230, 80) // x, y coord. of top left corner
.setSize(120, 90) // (width, height)
.setFont(font)
;
cp5.addButton("z") // button name
.setPosition(230, 200) // x, y coord. of top left corner
.setSize(120, 90) // (width, height)
.setFont(font)
;
}
void draw()
{
background(255, 255, 255); //background color
fill(0, 0, 0);
textFont(titlefont);
text("LED BLINK", 210, 40);
}
// Adding some functions to buttons
void a() {
myPort.write('a');
println("a");
}
void z() {
myPort.write('z');
println("z");
}
What could be the problem?
Thank you in advance for your answers!