Well this is my first attempt at modifying this code. I know it won't work but I wanted to see if I could get somebody to steer me in the right direction. I commented out all the analog stuff and just tried to put in a few things to see if anything is close. What I really don't understand is where the code tells the driver to go in either direction. If anybody could point that out I'd appreciate it, or anything else that would help.
Code:
#include <AccelStepper.h> // accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)
#define button 4
#define button 5
//Pins
//const byte Analog_X_pin = A1; // x-axis readings
const byte enablePin = 7;
const byte button 4
const byte button 5
//Variables
//int Analog_X = 0; //x-axis value
//int Analog_X_AVG = 0; //x-axis value average
//----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
//----------------------------------------------------------------------------
//PINS
//pinMode(Analog_X_pin, INPUT);
//pinMode(Analog_X_pin, INPUT);
// pinMode(Analog_X_pin, INPUT);
pinMode(enablePin, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4 INPUT_PULLUP); //button 1
pinMode(5 INPUT_PULLUP); //button 2
//----------------------------------------------------------------------------
InitialValues(); // averaging the values of analog pin 0 (value from potmeter)
//----------------------------------------------------------------------------
// Stepper parameters
// setting up some default values for maximum speed and maximum acceleration
stepper.setMaxSpeed(2000); //SPEED = Steps / second
stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
stepper.setSpeed(1000);
delay(1000);
}
void loop()
{
if (digitalRead(SW1) && digitalRead(SW2)) digitalWrite(motorOn, LOW);
digitalWrite(motorOn, HIGH);
ReadLimits();
stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
}
void ReadLimits()
{
if( digitalRead(2)==LOW && stepper.speed()>0 )//read input, assuming they have pull-up resistors.
{
stepper.setSpeed(0);
}
if( digitalRead(3)==LOW && stepper.speed()<0 )//read input, assuming they have pull-up resistors.
{
stepper.setSpeed(0);
}
}
void digitalRead(4) {
if (pin 4 HIGH) {
digitalWrite(enablePin, HIGH); // enable the driver
stepper.setSpeed(2000));
} else {
digitalWrite(enablePin, LOW); // disable the driver
stepper.setSpeed(0);
}
if (pin 5 HIGH) {
digitalWrite(enablePin, HIGH); // enable the driver
stepper.setSpeed(2000));
} else {
digitalWrite(enablePin, LOW); // disable the driver
stepper.setSpeed(0);
}
/*
void ReadAnalog() {
if (abs(Analog_X-Analog_X_AVG) > 50) {
digitalWrite(enablePin, HIGH); // enable the driver
stepper.setSpeed(5*(Analog_X-Analog_X_AVG));
} else {
digitalWrite(enablePin, LOW); // disable the driver
stepper.setSpeed(0);
}
// Reading the potentiometer in the joystick:
Analog_X = analogRead(Analog_X_pin);
// if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
// This is a sort of a filter for the inaccuracy of the reading
if (abs(Analog_X-Analog_X_AVG) > 50) {
stepper.setSpeed(5 * (Analog_X-Analog_X_AVG));
} else {
stepper.setSpeed(0);
}
}
*/
void InitialValues() {
//Set the values to zero before averaging
float tempX = 0;
//----------------------------------------------------------------------------
// read the analog 50x, then calculate an average.
// they will be the reference values
for (int i = 0; i<50; i++) {
//tempX += analogRead(Analog_X_pin);
delay(10); //allowing a little time between two readings
}
//----------------------------------------------------------------------------
//Analog_X_AVG = tempX/50;
//----------------------------------------------------------------------------
//Serial.print("AVG_X: ");
//Serial.println(Analog_X_AVG);
Serial.println("Calibration finished");
}