"Write an application that interfaces with an input &/or output device."
I decided to use the Arduino IDE and fabricate a Fabduino to control the stepper motors, analog joystick, limit switches, electromagnet and anything else that was needed. I control the stepper motors with a Stepper Motor Driver v3.3 off an old Makerbot (version 1).
The Fabduino development from Week 10:
The Stepper Motor Driver v3.3 board. There are many to choose from, including a Fab version. However, this one was free and ready to go. It is also very configurable.
Using the Arduino IDE I coded the following to control the motors:
//Magnetic Sphere Sphere Game
//This uses a dual potentiometer joystick to control two stepper motors in X and Y axis
//Need to add limit switches
//declare pins for X axis
int potPin = 1;
int Step_X = 13;
int Dir_X = 12;
int Enable_X = 8;
//declare pins for Y axis
int potPin2 = 2;
int Step_Y2 = 11;
int Dir_Y2 = 10;
int Enable_Y2 = 7;
//declare values
int Speed_X = 0; //step speed (delay between steps)
int val= 0;
int j = 0;
int Speed_Y2 = 0; //step speed (delay between steps)
int val2= 0;
int j2 = 0;
void setup() {
pinMode(Step_X, OUTPUT);
pinMode(Dir_X, OUTPUT);
pinMode(Enable_X, OUTPUT);
pinMode(Step_Y2, OUTPUT);
pinMode(Dir_Y2, OUTPUT);
pinMode(Enable_Y2, OUTPUT);
//Serial.begin(9600); // used to debug, must add writes after joystick valued reads
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
j = val - 432; // 517 is center positions - how far from center?
j = abs(j); //absolute value
Speed_X = 10000/j; //This math inverts the value and scales as needed
//(value found through trial and error)
// The delay between steps will determine the speed of the motor
// So, delay up = speed down
val2 = analogRead(potPin2); // read the value from the sensor
j2 = val2 - 432; // 432 is center positions - how far from center?
j2 = abs(j2); //absolute value
Speed_Y2 = 10000/j2; //This math inverts the value and scales as needed
//(value found through trial and error)
// The delay between steps will determine the speed of the motor
// So, delay up = speed down
if (val >= 442){
digitalWrite(Enable_X,LOW); // enable
digitalWrite(Dir_X, HIGH); // Set direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);
}
if (val2 >= 442){
digitalWrite(Enable_Y2,LOW); // enable
digitalWrite(Dir_Y2, HIGH); // Set direction
digitalWrite(Step_Y2,HIGH);
delayMicroseconds(2);
digitalWrite(Step_Y2,LOW);
delayMicroseconds(Speed_Y2);
}
if (val <= 422) {
digitalWrite(Enable_X,LOW);// enable
digitalWrite(Dir_X, LOW); // Other direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);
}
if (val2 <= 422) {
digitalWrite(Enable_Y2,LOW);// enable
digitalWrite(Dir_Y2, LOW); // Other direction
digitalWrite(Step_Y2,HIGH);
delayMicroseconds(2);
digitalWrite(Step_Y2,LOW);
delayMicroseconds(Speed_Y2);
}
if (val <=442 && val >= 422) {
digitalWrite(Enable_X,HIGH); // disable the stepper X motor if the X axis is centered
}
if (val2 <=442 && val2 >= 422) {
digitalWrite(Enable_Y2,HIGH); // disable the stepper Y motor if the Y axis is in the center
}}