Dear all;
I am trying to make a guy with a slider and two buttons talking to Arduino. Unfortunately, I cannot detect the signals send from processing to Arduino. I think I am struggling to separately read the serial signal from slider and button in Arduino code. I guess I should differentiate slider and buttons as separate objects so that port.write can send it as separate variable.
Any suggestions?
Processing code:
import controlP5.*; //library
import processing.serial.*; //library
Serial port; //do not change
ControlP5 cp5; //create ControlP5 object
PFont font;
//int led;
void setup() {
size(500, 300); //window size, (width, height)
printArray(Serial.list());
port = new Serial(this, "/dev/cu.usbmodem14101", 9600); //connected arduino port
cp5 = new ControlP5(this); //do not change
font = createFont("calibri", 20); // custom fonts for buttons and title
cp5.addButton("SFL_ON") //"Turn On" is the name of button
.setPosition(300, 50) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont(font)
;
cp5.addButton("SFL_OFF") //"Turn On" is the name of button
.setPosition(300, 150) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont(font)
;
cp5.addSlider("led")
.setPosition(125, 20) //x and y upper left corner
.setSize(50, 250) //(width, height)
.setRange(0, 255) //slider range low,high
.setValue(4) //start val
.setColorBackground(color(0, 0, 255)) //top of slider color r,g,b
.setColorForeground(color(0, 255, 0)) //botom of slider color r,g,b
.setColorValue(color(255, 255, 255)) //vall color r,g,b
.setColorActive(color(255, 0, 0)) //mouse over color
;
}
void draw() {
background(0, 0, 0); // background color of window (r, g, b)
textFont(font);
text("SFL CONTROL", 300, 30);
}
void led(int led)
{
port.write(led);
}
void SFL_ON(){
port.write('y');
}
void SFL_OFF(){
port.write('n');
}
Arduino code
const int NumberRepeats = 5;
const int washtime = 1000;
const int flowdelaytime = 1000;
const int exposetime = 1000;
void setup() {
pinMode(3, OUTPUT); // SELENOID VALVE/ FLOW
pinMode(4, OUTPUT); // UV sourece
Serial.begin(9600); //start serial
Serial.println("<Arduino is ready>");
}
void loop() {
if (Serial.available()) { //if data available
//char SFL=Serial.readString();
char SFL = Serial.read();
// int val = Serial.read();
if (SFL == 'y') { //if n received
for (int i = 1; i < NumberRepeats; i++) {
//Turn on the flow i.e. air pressure
digitalWrite(3, HIGH);
//Wait for the flow to wash out particles
delay(washtime);// washtime
//Turn off the flow
digitalWrite(3, LOW);
//Wait for the flow to stop
delay(flowdelaytime);//flowdelaytime
//Turn the UV light on i.e. open shutter
digitalWrite(4, HIGH);
//Expose the polymer to UV
delay(exposetime);// exposetime
//Turn the UV light off i.e. close shutter
digitalWrite(4, LOW);
}
}
// // Exit the loop if SFL_OFF received
// if (SFL == 'n') { //if n received
// exit(0);//The 0 is required to prevent compile error
// }
}
}