Hi there!
I wrote the following Scripts to control a Stepper-Motor via Processing:
The Arduino Script looks like this:
/*
* interface map with Processing to light up LEDs on arduino
* Works with Processing sketch 'pdxMap'
*/
#include <Stepper.h>
// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;
Stepper myStepper(STEPS, dirA, dirB);
int Pin[] = {0,2,3,4,5,6,7,8,9}; //Array of pins to controll lights
int value = 0; // variable to keep the actual value
void setup()
{
//Initialize pins as OUTPUTS and begin serial connection
for(int i=1; i<9 ; i++){
pinMode(Pin[i], OUTPUT);
}
// Set the RPM of the motor
myStepper.setSpeed(mm);
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);
// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
Serial.begin(9600);
}
void loop()
{
int input = Serial.read(); // read serial
switch (input){
case 1: //If processing passes a '1' do case one
if(input == 1){
// Pause
// Move the motor X amount of steps the other way
myStepper.step(-STEPS);
Serial.println(-5*STEPS);
// Pause
}
break;
case 2:
if(input == 2){ //If Processing passes a '2' do case two
myStepper.step(STEPS);
Serial.println(5*STEPS);
}
break;
}
}
And the crucial part of my Processing Script like this:
import g4p_controls.*;
import processing.serial.*;
Serial myPort;
int a = 200;
public void setup(){
size(480, 320, JAVA2D);
createGUI();
customGUI();
// Place your setup code here
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.buffer(1);
public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:475985:
println("button1 - GButton event occured " + System.currentTimeMillis()%10000000 );
label1.setText ("Hebt");
myPort.write(1);
} //_CODE_:button1:475985:
public void button2_click1(GButton source, GEvent event) { //_CODE_:button2:790988:
println("button2 - GButton event occured " + System.currentTimeMillis()%10000000 );
label1.setText ("Senkt");
myPort.write(2);
} //_CODE_:button2:790988:
public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:mm:249761:
println("textfield1 - GTextField event occured " + System.currentTimeMillis()%10000000 );
} //_CODE_:mm:249761:
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setCursor(ARROW);
if(frame != null)
frame.setTitle("Input");
button1 = new GButton(this, 288, 48, 80, 30);
button1.setTextBold();
button1.setText("Heben");
button1.setLocalColorScheme(GCScheme.GREEN_SCHEME);
button1.addEventHandler(this, "button1_click1");
button2 = new GButton(this, 288, 104, 80, 30);
button2.setTextBold();
button2.setText("Senken");
button2.setLocalColorScheme(GCScheme.RED_SCHEME);
button2.addEventHandler(this, "button2_click1");
label1 = new GLabel(this, 384, 72, 80, 40);
label1.setTextBold();
label1.setOpaque(true);
mm = new GTextField(this, 8, 120, 32, 16, G4P.SCROLLBARS_NONE);
mm.setOpaque(true);
mm.addEventHandler(this, "textfield1_change1");
}
// Variable declarations
// autogenerated do not edit
GButton button1;
GButton button2;
GLabel label1;
GTextField mm;
I made the Processing Script with the help of the awesome GUIbuilder ( Quarks Place )
What i was able to do so far: When i press "button1", the Stepper drives a fixed amount of steps in one direction, "button2" in the other direction.
As i'm quite new to all of this, i lack the knowledge of some basics.
I basically want to type a number in my Textfield1
public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:mm:249761:
println("textfield1 - GTextField event occured " + System.currentTimeMillis()%10000000 );
} //_CODE_:mm:249761:
which defines my Variable "mm" which works so far.
Now i want to have this Variable as my motor speed on my Arduino
myStepper.setSpeed(mm)
I know it can't work like i this, it is just for the illustration.
As far as i understood it, i can just send specific digits or combination of digits over to my Arduino uni via serial I/O. (Like "A", or in my case 1 and 2)
Now what method could i use to make my Arduino recognize, that the number i just sent over (lets say i typed "80" in my textfield1), is the number he should put in the setSpeed brackets?
Is there a way at all?
Thank you for your time and answers! I hope i could make myself clear, if not feel free to ask.