I have been trying to build a more complex program and feel a little overwhelmed trying to define the best way to structure my program.
My project involves using a closed loop stepper motor/driver to move an object to and fro between 2 positions.
I would like to do the following in the implementation:
- using a jog button be able to move the stepper to each end of travel position and then store the positions by button and to be used in the automatic cycling.[/li]
- set the total cycles to be run (full CW and full CCW between above limits.
- enable/disable the stepper motor so that the object could be moved manually without killing power.
- pause/resume the test cycle.
- have control over the acceleration/deceleration of the motion.
- display the total cycles completed, total cycles to be completed, speed/acc parameters of the stepper.
- display error status of the stepper driver.
Do you have any advice on how I should structure the code - I have been piecing it together as I go as you can see....
// #include <SD.h> // To implement ability to store cycle progress and limits to SD card
#include <SpeedyStepper.h>
// #include <Adafruit_GFX.h> // Core graphics library // to implement display for # cycles completed and travel setpoints vs current position
// #include <Adafruit_ST7735.h> // Hardware-specific library
//VAR DEFINITIONS
int CWLimit; //to be used to store the CW extent of travel
int CCWLimit; // to be used to store the CCW extent of travel
int CurrentPos; // current stepper position - will be written to when cycle is paused?
int Steps_per_Rev = 800;
int StepperSpeed = 20;
int OutputLED = 13;
// SIGNAL DEFINITIONS
# define PULPLUS 1 //PUL+ Motor Step Control > BLUE
# define PULMINUS 2 //PUL- Motor Step Control > BLACK COMMON GND
# define DIRPLUS 3 //DIR+ Motor Direction Control > PURPLE
# define DIRMINUS 4 //DIR- Motor Direction Control > BLACK COMMON GND
# define ENAPLUS 5 // Motor Enable Signal (active LOW) > ORANGE
# define ENAMINUS 6 // Motor Enable Signal (active LOW) > BLACK COMMON GND
# define PENDPLUS 7 // travel complete + > YELLOW // Can this be used to report that a full travel is complete instead of using a timer?
# define PENDMINUS 8 // travel complete - > GREY
# define ALMPLUS 9 // FAULT signal > WHITE
# define ALMMINUS 10 //FAULT signal > BROWN
# define SCLK 52 // for Display/SD Card - CLOCK SPEED
# define MOSI 51 // for Display/SD Card
# define TFTCS 11 // for Display/SD Card CHIP Select
# define DC 12 // for Display/SD Card Data/Command Pin
# define RST 14 // for Display/SD Card - can also connect to board reset pin
// CONTROL DEFINITIONS
# define SETCW 15 //Switch to store the CW position?
# define SETCCW 16 //Switch to set the CCW position?
# define enableButton 17 //Button
# define jogCWButton 18 // 3 position momentary switch to jog
# define jogCCWButton 19 // 3 position momentary switch to jog
# define startButton 20 // to start/resume the cycling
# define stopButton 21 // to stop/pause the cycling
int enableState = HIGH;
int enableButtonReading;
int previousEnableState = LOW;
long enableButtonTime = 0;
long enableButtonDebounce = 200;
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
//OUTPUTS
# define OutputLED 13 //LED
// Adafruit_ST7735 tft = Adafruit_ST7735(TFTCS, DC, RST);
float p = 3.1415926;
//STEPPER PARAMETER DEFINITION
SpeedyStepper stepper;
void setup()
{
// DEFINE THE I/O
pinMode (ENAPLUS, OUTPUT); // ENA+
pinMode (ENAMINUS, OUTPUT); // ENA-
pinMode (DIRPLUS, OUTPUT); // DIR+
pinMode (DIRMINUS, OUTPUT); // DIR-
pinMode (PULPLUS, OUTPUT); // PUL+
pinMode (PULMINUS, OUTPUT); // PUL-
pinMode (PENDPLUS, INPUT); // PEND+ Travel Complete
pinMode (PENDMINUS, INPUT); // PENS- Travel Complete
pinMode (ALMPLUS, INPUT); // ALM+
pinMode (ALMMINUS, INPUT); // ALM-
pinMode (SETCW, INPUT_PULLUP); // Sets the CW TRAVEL Limits
pinMode (SETCCW, INPUT_PULLUP); //Sets the CCW Travel Limits
pinMode (enableButton, INPUT_PULLUP); // Enable button state
pinMode (OutputLED, OUTPUT);
//INIT THE I/O
digitalWrite (ENAMINUS, LOW); // ENA- 0V
digitalWrite (ENAPLUS, HIGH); // ENA+ 5V (ENABLED LOW)
digitalWrite (DIRPLUS, LOW); //
digitalWrite (DIRMINUS, LOW); //
digitalWrite (PULPLUS, LOW); //
digitalWrite (PULMINUS, LOW); //
// digitalWrite (SETCW, LOW); //
// digitalWrite (SETCCW, LOW); //
Serial.begin(115200);
// CONNECT STEPPER PINS
stepper.connectToPins(PULPLUS, DIRPLUS);
stepper.setSpeedInStepsPerSecond(100);
}
//DISPLAYS PARAMETERS
void Display_Parameters()
{
Serial.println("Stepper Info:");
Serial.print("CW Limit - ");
Serial.println(CWLimit);
Serial.print("CCW Limit - ");
Serial.println(CCWLimit);
Serial.print("Current Position - ");
Serial.println(CurrentPos);
Serial.print("Steps/Revolution - ");
Serial.println(Steps_per_Rev);
}
void ButtonPress()
{
}
//SETS CW Limit
int RecordCWLimit ()
{
Serial.print(stepper.getCurrentPositionInSteps());
Serial.println("This code will be triggered to read the current position and set as the new value of CW limit");
delay(1000);
}
//SETS CCW Limit
int RecordCCWLimit ()
{
Serial.println("This code will be triggered to read the current position and set as the new value of CCW limit");
}
void loop()
{
// Enable Button Code
enableButtonReading = digitalRead(enableButton);
if (enableButtonReading == LOW && previousEnableState == HIGH && millis() - enableButtonTime > enableButtonDebounce)
{
if ( enableState == LOW)
enableState = HIGH;
else
enableState = LOW;
enableButtonTime = millis();
}
digitalWrite(OutputLED, enableState);
previousEnableState = enableButtonReading;
Serial.println(enableState);
//
// Trying to read the positon of encoder and give feedback
int CW_SET = digitalRead (SETCW);
int CCW_SET = digitalRead (SETCCW);
if (CW_SET == LOW)
{
RecordCWLimit ();
Serial.print("CW Button - ");
Serial.println(CW_SET);
}
else
{
if (CCW_SET == LOW)
{
Serial.print("CCW Button - ");
Serial.println(CCW_SET);
RecordCCWLimit ();
}
else
{
Serial.println("No Buttons Pressed");
Serial.println();
delay(1000);
Display_Parameters();
}
}
//Move forwards 200 steps
stepper.setSpeedInStepsPerSecond(100);
stepper.setAccelerationInStepsPerSecondPerSecond(100);
stepper.moveToPositionInSteps(200);
delay(1000);
// Moves forward another 200 steps
stepper.moveToPositionInSteps(400);
delay(1000);
//Returns to start position
stepper.setSpeedInStepsPerSecond(250);
stepper.setAccelerationInStepsPerSecondPerSecond(800);
stepper.moveToPositionInSteps(0);
delay(2000);
}
