I finally got it to do what I had originally intended. The delays could use some work to speed up the process, and I could filter out some noise a little better, but here is the code I have:
//Arduino Uno, Easydriver, Stepper Motor, Accelerometer Control
//doublec4 2011
//Easydriver pins
int dirPin = 3;
int stepperPin = 2;
int SLEEP = 8;
int MS1 = 7;
int MS2 = 9;
//Accelerometer pins (analog)
const int pinx = 1 ;
const int piny = 2 ;
const int pinz = 3 ;
const int SLP = 10;
//variables to store accelerometer output and keep track of stepper position
int valx = 0;
int valy = 0;
int valz = 0;
int calib = 0;
int position = 0;
int count = 0;
//setup
void setup() {
Serial.begin(9600);
pinMode(SLP, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(SLEEP, OUTPUT);
}
void loop(){
digitalWrite(SLP, HIGH); //Make sure the accelerometer is "awake"
digitalWrite(SLEEP, HIGH);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
valy = analogRead(piny);
if (count > 2) {
if (calib == 0) { //For the first time through the loop, set the "calibration" value to y input from the accelerometer
calib = 300;
position = calib;
}
//while(valy > (position + 5) || valy < (position - 5)) {
//digitalWrite(dirPin,HIGH);
//digitalWrite(stepperPin, LOW);
//digitalWrite(stepperPin, HIGH);
//delayMicroseconds(1500);
//}
Serial.print("Calibration: ");
Serial.print(calib);
Serial.print(" Position: ");
Serial.print(position);
Serial.print(" Valy: ");
Serial.println(valy);
if ((valy > (calib + 10)) || (valy < (calib - 10))) {
if ((valy - position) > 0) { //If the G reading (positive direction) is greater than the current position, move up towards that position
digitalWrite(dirPin,HIGH);
digitalWrite(stepperPin, LOW);
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1200);
position++;
}
if (((calib - valy + calib) - position) > 0) { //If the G reading (negative direction) is greater than the current position, move up towards that position
digitalWrite(dirPin,HIGH);
digitalWrite(stepperPin, LOW);
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1200);
position++;
}
if (((valy - position) < 0) && (position > calib) && (valy > calib)) { //If the G reading (positive direction) is less than the current position, move down towards that position
digitalWrite(dirPin,LOW);
digitalWrite(stepperPin, LOW);
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1200);
position--;
}
if ((((calib - valy + calib) - position) < 0) && (position > calib) && (valy < calib)) { //If the G reading (negative direction) is less than the current position, move down towards that position
digitalWrite(dirPin,LOW);
digitalWrite(stepperPin, LOW);
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1200);
position--;
}
}
if ((valy < (calib + 10)) && (valy > (calib - 10))) {
if (position > calib){
digitalWrite(dirPin,LOW);
digitalWrite(stepperPin, LOW);
digitalWrite(stepperPin, HIGH);
delayMicroseconds(1200);
position--;
}
}
}
count++;
}
Basically, the stepper motor travels in one direction to a position related to a G force. The higher the G, the greater the position away. Right now only using the y axis. When no G is detected, the stepper returns to the original position.
Comments and critiques welcome. Not sure if I could have done this in a more elegant and robust way? I'd love to know! Thanks!