Hi!
I have given a shot at making a state machine based on the Processing language (similar to Arduino).
The program sits in the background and waits for an input array of 4 buckets with different times.
Each solenoid is turned on if the value of that bucket is != 0. Once the solenoid is turned on for the allotted time, it is switched off, and that bucket is set to 0. This occurs for every bucket, until they are all reset to 0, and then the cycle resets (i.e., the program sits an waits for the next string of numbers).
I think I have the thing working, but I'd love to get a set of experienced eyes on the code!
Please find it here: import processing.net.*;import cc.arduino.*;import processing.serial.*;/ - Pastebin.com or below:
import processing.net.*;
import cc.arduino.*;
import processing.serial.*;
// Arduino arduino;
Server myServer;
int solenoid_0 = 2; // the pin numbers for the solenoids
int solenoid_1 = 3;
int solenoid_2 = 4;
int solenoid_3 = 5;
byte solenoid_0state = 0;
byte solenoid_1state = 0;
byte solenoid_2state = 0;
byte solenoid_3state = 0;
int[] solenoidTime;
long currentMillis = 0; // stores the value of millis() in each iteration of loop()
long startMillis = 0;
void setup() {
myServer = new Server(this, 5204);
// Prints out the available serial ports.
/*
println(Arduino.list());
// Modify this line, by changing the "0" to the index of the serial
// port corresponding to your Arduino board (as it appears in the list
// printed by the line above).
arduino = new Arduino(this, Arduino.list()[0], 57600);
// Configure digital pins 2 to 5 to control relays that control the solenoids.
arduino.pinMode(solenoid_0, OUTPUT);
arduino.pinMode(solenoid_1, OUTPUT);
arduino.pinMode(solenoid_2, OUTPUT);
arduino.pinMode(solenoid_3, OUTPUT);
// Set servo motors to drive to locked position.
arduino.digitalWrite(solenoid_0, solenoid_0state);
arduino.digitalWrite(solenoid_1, solenoid_1state);
arduino.digitalWrite(solenoid_2, solenoid_2state);
arduino.digitalWrite(solenoid_3, solenoid_3state);
*/
}
void draw() {
Client thisClient = myServer.available();
if (thisClient !=null){
String whatClientSaid = thisClient.readString();
String trimWhatClientSaid = trim(whatClientSaid);
println(trimWhatClientSaid);
solenoidTime = int(split(trimWhatClientSaid, ' '));
startMillis=millis();
while (max(solenoidTime) != 0){
currentMillis = millis();
updateSolenoid_0();
updateSolenoid_1();
updateSolenoid_2();
updateSolenoid_3();
switchSolenoids();
}
println("Next Round");
}
}
void updateSolenoid_0() {
if (solenoidTime[0] != 0){
if (solenoid_0state == 1) {
if (currentMillis >= startMillis + solenoidTime[0]) {
//arduino.digitalWrite(solenoid_0, 0);
solenoidTime[0]=0;
println("Solenoid 0 OFF");
}
}
else {
//arduino.digitalWrite(solenoid_0, 1);
println("Solenoid 0 ON");
}
}
}
void updateSolenoid_1() {
if (solenoidTime[1] != 0){
if (solenoid_1state == 1) {
if (currentMillis >= startMillis + solenoidTime[1]) {
//arduino.digitalWrite(solenoid_3, 0);
solenoidTime[1]=0;
println("Solenoid 1 OFF");
}
}
else {
//arduino.digitalWrite(solenoid_1, 1);
println("Solenoid 1 ON");
}
}
}
void updateSolenoid_2() {
if (solenoidTime[2] != 0){
if (solenoid_2state == 1) {
if (currentMillis >= startMillis + solenoidTime[2]) {
//arduino.digitalWrite(solenoid_3, 0);
solenoidTime[2]=0;
println("Solenoid 2 OFF");
}
}
else {
//arduino.digitalWrite(solenoid_2, 1);
println("Solenoid 2 ON");
}
}
}
void updateSolenoid_3() {
if (solenoidTime[3] != 0){
if (solenoid_3state == 1) {
if (currentMillis >= startMillis + solenoidTime[3]) {
//arduino.digitalWrite(solenoid_3, 0);
solenoidTime[3]=0;
println("Solenoid 3 OFF");
}
}
else {
//arduino.digitalWrite(solenoid_3, 1);
println("Solenoid 3 ON");
}
}
}
The main loop is the draw() loop, which calls each of the separate update processes of the solenoids.
Thanks for any input!