const int dirPin = 2;
const int stepPin = 3;
// Define motor interface type
#define motorInterfaceType 1
#include <AccelStepper.h>
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
/*
Stepper Motor Control - speed control
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
A potentiometer is connected to analog input 0.
The motor will rotate in a clockwise direction. The higher the potentiometer value,
the faster the motor speed. Because setSpeed() sets the delay between steps,
you may notice the motor is less responsive to changes in the sensor value at
low speeds.
Created 30 Nov. 2009
Modified 28 Oct 2010
by Tom Igoe
*/
///
#include <Vector.h>
//const int AIn1 = 8;
//const int AIn2 = 9;
//const int BIn1 = 10;
//const int BIn2 = 11;
Vector<int> Istep;
const int maxSpeedLimit = 560; // change this to fit the number of steps per revolution
const float Pi = 3.1415926;
// for your motor
int switchval = 0;
float radius = 0.03;
unsigned long startMillis;
unsigned long currentMillis;
float distance = 1;
float averageVelocity;
// initialize the stepper library on pins 8 through 11:
//AccelStepper myStepper(AccelStepper::FULL4WIRE, AIn1, AIn2, BIn1, BIn2);
int masterSwitch = 4;
int stepCount = 0;
int progress;
int steps = distance*200/(Pi*radius*2);
int step; // number of steps the motor has taken
int i;
void setup() {
// nothing to do inside the setup
startMillis = millis();
myStepper.setCurrentPosition(1);
myStepper.setMaxSpeed(maxSpeedLimit);
myStepper.moveTo(steps);
pinMode(masterSwitch, INPUT);
digitalWrite(masterSwitch,HIGH);
float StepVelocity(int step, int steps);
return (1/steps)*(sin(2*Pi*step/steps-Pi/2)+1);
for (i = 0; i <= steps; i++){
Istep.push_back(StepVelocity(i,steps));
Serial.print(i);
}
//Serial.print(steps);
//Serial.println(Istep[1]);
Serial.begin(9600);
i = 0;
}
void loop() {
Serial.println(i);
delay(500);
//Serial.println("...");
currentMillis = millis();
switchval = digitalRead(masterSwitch);
int position = myStepper.currentPosition();
//Serial.println(Istep[i]);
//Serial.println(switchval);
if (switchval == 0) {
// read the sensor value:
int potReading = analogRead(A0);
// map it to a range from 0 to 100:
int masterTime = map(potReading, 0, 1023, 10, 30);
averageVelocity = steps/masterTime;
//Serial.println(averageVelocity);
if (position < steps+1) {
//myStepper.moveTo(steps+1);
progress = currentMillis - startMillis;
//Serial.println(masterTime);
//Serial.println(currentMillis-startMillis);
//Serial.println(currentMillis);
//Serial.println(startMillis);
// set the motor speed:
float acceleration = abs(100*averageVelocity*2*Pi*(sin(2*Pi*position/steps))/steps);
float velocity = 100*averageVelocity*(sin(2*Pi*position/(steps)-Pi/2)+1);
float motorSpeed = velocity;
//Serial.println(velocity);
//Serial.println(acceleration);
//Serial.println(motorSpeed);
//Serial.println((sin(2*Pi*progress/(1000*masterTime)-Pi/2)+1)/(masterTime));
//Serial.println(progress);
//Serial.println("");
//progress = progress + 1;
if (motorSpeed > 0) {
myStepper.setAcceleration(acceleration);
// step 1/100 of a revolution:
myStepper.run();
//Serial.println(myStepper.currentPosition());
}
}
else {
//Serial.println(currentMillis-startMillis);
stepCount = 0;
startMillis = currentMillis;
}
}
i++;
//delay(400);
}