I would like to use Processing to control the PWM of motors and also delay times. I am thinking of sending lists through Serial after inputting and the recieving that in Arduino IDE and for example:
setting variable2 as item 2 of list and so on.
I have already programmed a GUI in processing but am struggling with the serial part and whether or not my code works:
import controlP5.*;
import processing.serial.*;
Serial port;
ControlP5 cp5;
int motor1;
int motor2;
void setup(){
size(800,500);
cp5 = new ControlP5(this);
cp5.addTextfield("motor1").setColorBackground(0xffffffff).setColorValue(0x00000000) .setPosition(300,40).setAutoClear(false);
cp5.addTextfield("motor2").setColorBackground(0xffffffff).setColorValue(0x00000000).setPosition(300,65).setAutoClear(false);
cp5.addBang("submit").setColorBackground(0x00000000).setColorValue(0xfffffff).setPosition(300,95);
printArray(Serial.list());
port = new Serial(this, "???",9600);
}
void draw(){
background(250,250,250);
fill(0,0,0);
textSize(20);
text("motor",290,25);
}
void Submit(){
print("submit request sent");
motor1 = int(cp5.get(Textfield.class,"motor1").getText());
motor2 = int(cp5.get(Textfield.class,"motor2").getText());
}
void motor1(){
port.write('');
}
void motor2(){
port.write('');
}
Here is my arduino sketch code:
int repeatcycle = 2;
int motor1speed = 50;
int motor1duration = 1000;
int pause1 = 1000;
int motor2speed = 50;
int motor2duration = 2000;
int pause2 = 1000;
int cyclecount = 0;
int motor1pin1 = 22;
int motor1pin2 = 23;
int motor1pwm = 2;
int motor2pin1 = 24;
int motor2pin2 = 25;
int motor2pwm = 3;
void setup ()
{
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor1pwm, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(motor2pwm, OUTPUT);
Serial.begin(9600);
}
void loop()
{
while (cyclecount<repeatcycle) {
cyclecount = cyclecount+1;
analogWrite(motor1pwm, motor1speed);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
delay(motor1duration);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
delay(pause1);
analogWrite(motor2pwm, pump2speed);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(motor2duration);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(pause2);
}
}