For a school project, I'm currently trying to build a vending machine. The brain of the opperation would be my laptop, using an arduino to control the hardware.
I am using the following code to trigger a vend:
public class App {
public App() {
}
public static void main(String[] args) {
System.out.println("Hoi!");
SerialPort comPort = SerialPort.getCommPorts()[0];
comPort.openPort();
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
InputStream in = comPort.getInputStream();
OutputStream out = comPort.getOutputStream();
try {
out.write('g');
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
And the following code to in the arduino:
#include <Stepper.h>
const int stepsPerRev = 2048;
Stepper motor1(stepsPerRev, 8, 9, 10, 11);
char derp;
void setup() {
Serial.begin(9600);
Serial.println("Start");
motor1.setSpeed(6);
}
void loop() {
if (Serial.available() > 0) {
derp = Serial.read();
}
else {
derp = 'k';
}
if(derp == 'g'){
motor1.step(stepsPerRev);
}
delay(100);
}
Unfortunately, this does not seem to work. When I place the out.write in a while(true) block, it does seem to work. But this triggers an infinite amount of vends.
Any ideas how I could do this better?