I'm having troubles getting Arduino and Processing to talk to eachother. Summary: after a few parameters are met Arduino sends a "happy" message to Processing, Processing then takes a snapshot with a webcam.
Arduino code
Code:int dirpin = 2; // pin 3 is direction pin
int steppin = 12; // pin 12 "starts" motor
int val1 = 0; // val1 will store sensor1 value
int val2 = 0; // val2 will store sensor2 value
int val3 = 0; // val3 will store okbutton value
int val4 = 0; // stores STARTBUTTON count
int SENSOR1 = 4; // input pin for sensor1
int SENSOR2 = 5; // input pin for sensor2
int OKBUTTON = 6; // input pin for ok button
int STARTBUTTON = 3; // on switch
int LED1 = 9; // output pin for led1
int LED2 = 8; // output pin for led2
int LED3 = 7; // output pin for "both" led3
int firstSwitch; // variable for first switch case
int i;
void setup() {
Serial.begin(9600);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(SENSOR1, INPUT);
pinMode(SENSOR2, INPUT);
pinMode(OKBUTTON, INPUT);
pinMode(STARTBUTTON, INPUT);
}
void loop() {
Serial.println("absolute\n");
delay(100);
while (val4 == 0) {
val4 = digitalRead(STARTBUTTON); // check start button
int firstSwitch = 1; // set "starting" switch case
Serial.println("Starting Here\n");
delay(100);
}
switch (firstSwitch) {
case 1: // start up procedure
Serial.println("starting - case 1\n");
delay(100);
do {
val1 = digitalRead(SENSOR1);
val2 = digitalRead(SENSOR2);
if (val1 == HIGH) {
digitalWrite(LED1, HIGH);
} else {
digitalWrite(LED1, LOW);
}
if (val2 == HIGH) {
digitalWrite(LED2, HIGH);
} else {
digitalWrite(LED2, LOW);
}
if ((val1 == HIGH) && (val2 == HIGH)) {
digitalWrite(LED3, HIGH);
do {
val3 = digitalRead(OKBUTTON);
} while (val3 == LOW);
}
} while ((val1 == LOW) || (val2 == LOW));
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
break;
case 2: // bad procedure, add in buzzer
Serial.println("bad - case 2\n");
delay(100);
do {
val1 = digitalRead(SENSOR1);
val2 = digitalRead(SENSOR2);
if (val1 == HIGH) {
digitalWrite(LED1, HIGH);
} else {
digitalWrite(LED1, LOW);
}
if (val2 == HIGH) {
digitalWrite(LED2, HIGH);
} else {
digitalWrite(LED2, LOW);
}
if ((val1 == HIGH) && (val2 == HIGH)) {
digitalWrite(LED3, HIGH);
do {
val3 = digitalRead(OKBUTTON);
} while (val3 == LOW);
}
} while ((val1 == LOW) || (val2 == LOW));
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
break;
case 3: // good procedure
Serial.println("good - case 3\n");
delay(100);
break;
}
digitalWrite(dirpin, LOW); // Set the direction.
delay(100);
for (i = 0; i<10000; i++) { // 10000 microsteps
digitalWrite(steppin, LOW); // low to high for rising edge
digitalWrite(steppin, HIGH);
delayMicroseconds(200); // close to top speed
}
Serial.println("motor has moved\n");
delay(100);
val1 = digitalRead(SENSOR1);
val2 = digitalRead(SENSOR2);
if ((val1 == HIGH) && (val2 == HIGH)) { // if val1 & val2 are HIGH
Serial.println("sensors good\n");
delay(100);
Serial.print("Take shot\n"); // send "ok" to serial port
delay(100);
firstSwitch = 0; // set firstSwitch to zero
do {
if (Serial.available() > 0) { // if serial is availabe
firstSwitch = Serial.read(); // read serial into firstSwitch
}
} while (firstSwitch == 0); // keep checking if processing has finished
Serial.println(firstSwitch, DEC);
delay(100);
} else {
firstSwitch = 2; // if val1 or val2 are low set firstSwitch to bad
Serial.println("sensors did not line up\n");
delay(100);
}
}
Processing code
Code:import processing.serial.*;
import processing.video.*;
Serial arduinoPort;
Capture webcam;
int which = 0;
void setup() {
size(640,480);
webcam = new Capture(this,640,480);
String[] devices = Capture.list();
println(devices);
webcam.settings();
arduinoPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
while (arduinoPort.available() > 0) {
String inBuffer = arduinoPort.readStringUntil('\n');
if (inBuffer != null) {
String myString = new String(inBuffer);
if (myString.equals("Happy")) {
which = 1;
} else {
which = 2;
}
println(myString);
switch (which) {
case 1:
println("Good");
if (webcam.available() == true) {
webcam.read();
image(webcam ,0,0);
}
saveFrame("line-####.bmp");
arduinoPort.clear();
arduinoPort.write(3);
case 2:
println(myString);
arduinoPort.clear();
arduinoPort.write(2);
}
}
}
}
I pretty much have two problems
1. When I serial.println strings it always shows up multiple times in the Processing serial window, like so...
absolute
Starting Here
Starting Here
starting - case 1
starting - case 1
motor has moved
motor has moved
sensors good
sensors good
Happy
Happy
2
2
I think this happens because Processing checks the serial port multiple times before its updated, I'm not really sure how to fix this though.
2. As you can see in the above serial window post, "Happy" is received by Processing, however it doesn't trigger the webcam to take a snapshot like it should. It just returns a "2" back to the Arduino, which should happen if myString != Happy.
I've tried doing handshake methods and serialEvents() however any tutorials I've found seem to be a bit confusing. Any suggestions would be great!
Thanks