Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2014 20:19
Show Gist options
  • Save anonymous/8245756 to your computer and use it in GitHub Desktop.
Save anonymous/8245756 to your computer and use it in GitHub Desktop.
#include <AccelStepper.h>
//AccelStepper stepper;
const int buttonPin = 8;
int motorDirPin = 2; //digital pin 2
int motorStepPin = 3; //digital pin 3
int buttonState = 0;
int stepper_speed= 0;
int motorAccel = 80000;
unsigned long time = 0;
int step_position = 0;
int count = 0;
int timeDelay= 5000;
//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin);
void setup ()
{
Serial.begin(9600);
stepper.setMaxSpeed(stepper_speed);
stepper.setSpeed(stepper_speed);
pinMode(buttonPin, INPUT);
stepper.setAcceleration(40000);
}
void loop()
{
int sensorValue = analogRead(A0);
int sensorValue2= analogRead(A5);
buttonState = digitalRead(buttonPin);
byte temp2;
stepper.setMaxSpeed(stepper_speed);
stepper.setSpeed(stepper_speed);
stepper.setAcceleration(motorAccel);
stepper.runSpeed();
if (Serial.available() > 0 )
{
int temp = Serial.parseInt();
if (temp >= -9600 && temp <= 9600)
{
stepper_speed = temp;
stepper.setSpeed(stepper_speed);
Serial.print("Speed set to: ");
Serial.print (stepper_speed);
Serial.print ('\n');
Serial.flush();
}
else if(temp==9601)
{
Serial.flush();
Serial.print(sensorValue);
Serial.print('\n');
}
else if (temp==9602)
{
Serial.flush();
Serial.print(sensorValue2);
Serial.print('\n');
}
else if (temp == 9603)
{
step_position = stepper.currentPosition();
Serial.print ("Current step position: ");
Serial.print('step_position');
Serial.print('\n');
}
else
{
Serial.print("Invalid Input");
Serial.print ('\n');
Serial.flush();
}
}
if (buttonState ==HIGH && count == 0)
{
time = millis();
Serial.print(time);
Serial.print( '\n');
count ++;
}
else if (count > 0)
{
if ( millis()- time > timeDelay)
{
count = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment