Hi
I am trying to send multiple strings from processing to arduino to turn on and off multiple relays. Ultimately the string / strings sent from processing would be if a line or multiple lines were clicked. Then all of this data sent over to arduino.
The string would be like this
000100 - the first 4 numbers are the reference for the relay to connected to an individual arduino pin
000100 - the 5th number is if the relay is on or off 0 = off 1 = on
000100 - the 6th number is for how long the relay is on for
Having read some posts some initial problems are
1 - the number of lines selected is not known but the maximum number of lines that can be selected is.
2 - Firsts attempts sending just two strings from processing has resulted in unreliable results. Relays do not switch on sometimes etc.
Having read quite a few posts and websites I think the solution is in gathering all of this information in an array and then reading it when arduino knows all of the information has been sent.
One thing that may help if timing / delays are needed when sending lots of strings from processing is that all of the relays do not have to switch on at exactly the same time.
Processing Code
import processing.serial.*;
Serial myPort;
int time;
int wire_01_timer = 1000;
int wire_02_timer = 2000;
boolean wire_01_isOn = true;
boolean wire_02_isOn = true;
void setup() {
//String portName = Serial.list()[0];
myPort = new Serial(this, "COM3", 9600);
time = millis();//store the current time
}
void draw() {
if (wire_01_isOn && (millis() - time >= wire_01_timer) ) {
myPort.write("00100,"); //this is ref: 0100 5th number means on or off 0 = off, 1 = on, 2 = on)
// 00100 the first 4 numbers are the reference / pin reference,
// the 5th number is to switch it on, off and to a set resistance
// 0 = off, 1 = on at resistance 5, 2 = on at resistance 200
// eg - 00101 = wire 1 is on at resistance 5
wire_01_isOn = false;
delay(100);
println("turned wire 1 off");
//flush();
}
if (wire_02_isOn && (millis() - time >= wire_02_timer) ) {
myPort.write("01000"); //this is ref: 0100 5th number means on or off 0 = off, 1 = on, 2 = on)
delay(1000);
wire_02_isOn = false;
println("turned wire 2 off");
//flush();
}
}
void mousePressed() {
myPort.write("00101"); //this is ref: 0010, 5th number 1 on means switch on at resistance 5
println("turned wire 1 on");
delay(1000);
myPort.write(','); //a break between the strings to separate them
delay(1000);
myPort.write("01001"); //this is ref: 0100 and 1 (for "on")
println("turned wire 2 on");
delay(1000);
time = millis();//store the current time
wire_01_isOn = true;
wire_02_isOn = true;
}
void serialEvent(Serial myPort)
{
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
println(myString);
}
Arduino Code
# define RELAY_ON 0
# define RELAY_OFF 1
String incomingByte;
void setup() {
// initialize serial communication:
Serial.begin(9600);
digitalWrite (10, RELAY_OFF);
digitalWrite (8, RELAY_OFF);
pinMode (10, OUTPUT);
pinMode (8, OUTPUT);
}
void loop() {
/*
listern to the serial port. If anything comes through
/send the whole string to a wireOperator() method below...
*/
if (Serial.available() > 0) {
String s = Serial.readString();
wireOperator(s);
Serial.println(s); //write to the serial so can see what arduino received and write this on processing
}
}
int getPin(String pinRef) {
//this is the switchboard method. It will take in a
//pin reference and send back a pin value as an int.
//This is where pins are referenced relative to string information recieved
//0010 = relay 1
if (pinRef == "0010") //Wire ref in from processing specific to that value.
return (10); //Relay 1 on pin 10
if (pinRef == "0100") //0100 = relay 2
return (8);
else
return (0);
}
void wireOperator(String incomingByte) {
/*
this method will split the string up using the
charAt(x) method. This will produce two chunks.
1. a String "ref" that will hold the 4 digit string ref
2. an int "state" which will be zero or one for "on" or "off"
*/
String ref = String( incomingByte.charAt(0)) + "" + String(incomingByte.charAt(1)) + "" + String(incomingByte.charAt(2)) + "" + String(incomingByte.charAt(3) );
int state = String(incomingByte.charAt(1)).toInt();
if (state == 1) { //on
digitalWrite(getPin(ref), RELAY_ON);
Serial.flush();
}
else if (state == 0) {//off
digitalWrite(getPin(ref), RELAY_OFF);
Serial.flush();
}
}
I am using an arduino uno at the moment