Circuit Diagram
/*******************************************************************************
* IO DEFINITION *
*******************************************************************************/
// PWM is connected to pin 23.
const int pinOut = 25;
// DIR is connected to pin 22.
const int pinIn = 24;
/********************** *********************************************************
* PRIVATE GLOBAL VARIABLES *
*******************************************************************************/
static int Ttrial = 10000;
float Depth=1;
static int frequency = 550;
//time not moving: make sure is not less than ~70
//static float Tpause = (2.0625-Depth)*20*16;
//static float Tp = (2.0625-Depth);
static int Tpause = 140;
static int Tmoving = (frequency - Tpause - 20) / 2;
static int dBuffer = 30;
long Time = 0;
int counter = 0;
int stat = 0;
int s = 0;
int Tinit=0;
int compressions = 0;
int compression_counter;
int pos;
int pos_i;
/*******************************************************************************
* FUNCTIONS *
*******************************************************************************/
// The setup routine runs once when you press reset.
void setup() {
// Initialize the PWM and DIR pins as digital outputs.
Serial.begin(115200);
pinMode(pinOut, OUTPUT);
pinMode(pinIn, OUTPUT);
in();
delay(3000);
Tinit = millis();
pos_i=pos;
Serial.print(Tpause);
}
// The loop routine runs over and over again forever.
void loop() {
Time = millis()-Tinit;
/*
if (Time <= Ttrial) {
if (pos >= pos_i - dBuffer) {
}
} else {
*/
counter = Time % frequency + 1;
compression_counter = Time % (4*frequency) + 1;
if (compression_counter <= 3*frequency){
if (counter <= Tmoving) {
out();
stat = 1;
} else if ((counter > Tmoving) && (counter <= Tmoving + 20)) {
noMove();
stat = 2;
} else if ((counter > Tmoving + 20) && (counter <= (frequency - Tpause))){
in();
stat = 0;
} else {
noMove();
stat = 2;
}
} else {
noMove();
stat = 2;
}
//Serial.print(Time);
//Serial.print("___");
//Serial.print(stat);
//Serial.print("___");
Serial.println(stat);
}
//}
void in() {
digitalWrite(pinOut, HIGH);
digitalWrite(pinIn, LOW);
}
void out() {
digitalWrite(pinOut, LOW);
digitalWrite(pinIn, HIGH);
}
void noMove() {
digitalWrite(pinOut, LOW);
digitalWrite(pinIn, LOW);
}
// Include the AccelStepper library:
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(4000);
}
void loop() {
// Set the current position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 200 steps/second until the motor reaches 400 steps (4 revolutions):
while(stepper.currentPosition() != - 5*200)
{
stepper.setSpeed(-1800);
stepper.run();
}
delay(50);
// Reset the position to 0:
stepper.setCurrentPosition(0);
// Run the motor backwards at 600 steps/second until the motor reaches -200 steps (5 revolution):
while(stepper.currentPosition() != 5*200)
{
stepper.setSpeed(1800);
stepper.run();
}
delay(50);
}