226 lines
9.0 KiB
C++
226 lines
9.0 KiB
C++
#include <SoftwareSerial.h>
|
|
#define RX_PIN 13 // Connected to HC-05 TX pin
|
|
#define TX_PIN 12 // Connected to HC-05 RX pin
|
|
#define LED_PIN 2 // Connected to LED pin
|
|
#include <AccelStepper.h>
|
|
long receivedSteps = 0; //Number of steps
|
|
long receivedSpeed = 0; //Steps / second
|
|
long receivedAcceleration = 0; //Steps / second^2
|
|
char receivedCommand;
|
|
//-------------------------------------------------------------------------------
|
|
int directionMultiplier = 1;
|
|
int directionMultiplier2 = -1;// = 1: positive direction, = -1: negative direction
|
|
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
|
|
AccelStepper stepper(1, 9, 10);// pulses Digital 8 (CLK) direction Digital 9 (CCW),
|
|
AccelStepper stepper2(1, 5, 6);
|
|
SoftwareSerial bluetooth(RX_PIN, TX_PIN); // Create a software serial object
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(38400); // Initialize the serial monitor
|
|
bluetooth.begin(38400); // Initialize the Bluetooth communication
|
|
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
|
|
stepper.setMaxSpeed(10000); //SPEED = Steps / second
|
|
stepper.setAcceleration(800); //ACCELERATION = Steps /(second)^2
|
|
stepper.disableOutputs(); //disable outputs
|
|
stepper2.setMaxSpeed(10000); //SPEED = Steps / second
|
|
stepper2.setAcceleration(800); //ACCELERATION = Steps /(second)^2
|
|
stepper2.disableOutputs(); //disable outputs
|
|
}
|
|
|
|
void loop() {
|
|
|
|
readinfo();
|
|
RunTheMotor();
|
|
}
|
|
|
|
void RunTheMotor() //function for the motor
|
|
{
|
|
if (runallowed == true)
|
|
{
|
|
stepper.enableOutputs(); //enable pins
|
|
stepper.run(); //step the motor (this will step the motor by 1 step at each loop)
|
|
stepper2.enableOutputs(); //enable pins
|
|
stepper2.run(); //step the motor (this will step the motor by 1 step at each loop)
|
|
}
|
|
else //program enters this part if the runallowed is FALSE, we do not do anything
|
|
{
|
|
stepper.disableOutputs(); //disable outputs
|
|
stepper2.disableOutputs(); //disable outputs
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
void GoHome()
|
|
{
|
|
if (stepper.currentPosition() == 0)
|
|
{
|
|
bluetooth.println("We are at the home position.");
|
|
stepper.disableOutputs(); //disable power
|
|
}
|
|
else
|
|
{
|
|
stepper.setMaxSpeed(800); //set speed manually to 400. In this project 400 is 400 step/sec = 1 rev/sec.
|
|
stepper.moveTo(0); //set abolute distance to move
|
|
}
|
|
}
|
|
|
|
|
|
void RotateRelative()
|
|
{
|
|
//We move X steps from the current position of the stepper motor in a given direction.
|
|
//The direction is determined by the multiplier (+1 or -1)
|
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
|
stepper.setMaxSpeed(receivedSpeed); //set speed
|
|
stepper.move(directionMultiplier * receivedSteps); //set relative distance and direction
|
|
}
|
|
|
|
|
|
|
|
void RotateAbsolute()
|
|
{
|
|
//We move to an absolute position.
|
|
//The AccelStepper library keeps track of the position.
|
|
//The direction is determined by the multiplier (+1 or -1)
|
|
//Why do we need negative numbers? - If you drive a threaded rod and the zero position is in the middle of the rod...
|
|
|
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
|
stepper.setMaxSpeed(receivedSpeed); //set speed
|
|
stepper.moveTo(directionMultiplier * receivedSteps); //set relative distance
|
|
}
|
|
void GoHome2()
|
|
{
|
|
if (stepper2.currentPosition() == 0)
|
|
{
|
|
bluetooth.println("We are at the home position.");
|
|
stepper2.disableOutputs(); //disable power
|
|
}
|
|
else
|
|
{
|
|
stepper2.setMaxSpeed(800); //set speed manually to 400. In this project 400 is 400 step/sec = 1 rev/sec.
|
|
stepper2.moveTo(0); //set abolute distance to move
|
|
}
|
|
}
|
|
|
|
|
|
void RotateRelative2()
|
|
{
|
|
//We move X steps from the current position of the stepper motor in a given direction.
|
|
//The direction is determined by the multiplier (+1 or -1)
|
|
|
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
|
stepper2.setMaxSpeed(receivedSpeed); //set speed
|
|
stepper2.move(directionMultiplier2 * receivedSteps); //set relative distance and direction
|
|
}
|
|
|
|
|
|
|
|
void RotateAbsolute2()
|
|
{
|
|
//We move to an absolute position.
|
|
//The AccelStepper library keeps track of the position.
|
|
//The direction is determined by the multiplier (+1 or -1)
|
|
//Why do we need negative numbers? - If you drive a threaded rod and the zero position is in the middle of the rod...
|
|
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
|
|
stepper2.setMaxSpeed(receivedSpeed); //set speed
|
|
stepper2.moveTo(directionMultiplier2 * receivedSteps); //set relative distance
|
|
}
|
|
|
|
|
|
void readinfo()
|
|
{
|
|
if (bluetooth.available() > 0) //if something comes from the computer
|
|
{
|
|
receivedCommand = bluetooth.read(); // pass the value to the receivedCommad variable
|
|
newData = true; //indicate that there is a new data by setting this bool to true
|
|
|
|
if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
|
|
{
|
|
switch (receivedCommand) //we check what is the command
|
|
{
|
|
case 'L':
|
|
digitalWrite(LED_PIN, HIGH);
|
|
break;
|
|
|
|
case 'O':
|
|
digitalWrite(LED_PIN, LOW);
|
|
break;
|
|
|
|
case 'P': //P uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.
|
|
|
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
|
directionMultiplier = 1; //We define the direction
|
|
bluetooth.println("Positive direction."); //print the action
|
|
RotateRelative(); //Run the function
|
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
|
//In theory, this movement should take 5 seconds
|
|
break;
|
|
|
|
case 'N': //N uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.
|
|
|
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
|
directionMultiplier = -1; //We define the direction
|
|
bluetooth.println("Negative direction."); //print action
|
|
RotateRelative(); //Run the function
|
|
//example: N2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed; will rotate in the other direction
|
|
//In theory, this movement should take 5 seconds
|
|
break;
|
|
|
|
case '1': //avancer
|
|
|
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
|
directionMultiplier = -1; //We define the direction
|
|
directionMultiplier2 = 1;
|
|
bluetooth.println("CA AVANCE."); //print the action
|
|
RotateRelative();
|
|
RotateRelative2(); //Run the function
|
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
|
//In theory, this movement should take 5 seconds
|
|
break;
|
|
|
|
case '2': //reculer
|
|
|
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
|
directionMultiplier = 1; //We define the direction
|
|
directionMultiplier2 = -1;
|
|
bluetooth.println("CA RECULE"); //print the action
|
|
RotateRelative();
|
|
RotateRelative2(); //Run the function
|
|
//example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
|
|
//In theory, this movement should take 5 seconds
|
|
break;
|
|
|
|
case '3': //a gauche
|
|
|
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
|
directionMultiplier = 1; //We define the direction
|
|
directionMultiplier2 = 1;
|
|
bluetooth.println("CA TOURNE A GAUCHE"); //print the action
|
|
RotateRelative();
|
|
RotateRelative2(); //Run the function
|
|
break;
|
|
|
|
case '4': //a droite.
|
|
|
|
receivedSteps = bluetooth.parseFloat(); //value for the steps
|
|
receivedSpeed = bluetooth.parseFloat(); //value for the speed
|
|
directionMultiplier = -1; //We define the direction
|
|
directionMultiplier2 = -1;
|
|
bluetooth.println("CA TOURNE A DROITE"); //print the action
|
|
RotateRelative();
|
|
RotateRelative2(); //Run the function
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|