/* stepper joystick, left and right - variable speed should be but not clear in similation.
Written by R. A. Stephenson for prototype Pan/Tilt Alt/Az small telescope or
binocular set pointer application.
Significant advice, and editorial guidance, supplied by Brian Schmalz
(designer of the Easy Driver® bipolar stepper motor driver board).
Test equipment:
Arduino® Nano, 2 - Easy Driver® bipolar stepper motor driver boards,
analog 2 axis joystick and salvage bipolar stepper motors,
which are both 1.8 degree per full step, being driven at 1/8 micro stepping (Easy Driver default),
which equates to 200 full steps multiplied by 8 = 1600 pulses for one complete revolution of the motor
left right gearing provided by windshield wiper worm and wheel gears (ratio TBD)
up and down gearing provided by paper shredder square cut reduction set (ratio TBD)
project origin; July, 2018, this edit, Version 5.1, began Jan. 2019
last modified: Jan. 23, 2019
*/
//define Arduino pin assignments
//
#define step_pinx 3 // Arduino Digital output pin #2 is step signal pin for X axis L/R
#define dir_pinx 2 // Arduino Digital output pin #3 is direction control pin for X axis L/R
#define x_pin A0 // Arduino analog input from joystick for X axis L/R
#define y_pin A1 // Arduino analog input from joystick for Y axis U/D
#define step_piny 5 // Arduino Digital output pin #5 is step speed select signal pin for Y axis U/D
#define dir_piny 4 // Arduino Digital output pin #4 is direction control pin for Y axis U/D
//
//declare global variable
//
int xaxisState = LOW; //state of x axis stepper pulse set to ON
int yaxisState = LOW; //state of y axis stepper pulse set to ON
//
int XStepDelay;
int YStepDelay;
//
//
const long xhigh = 260; //interval to pulse X axis at slowest speed, change to fit application
const long xlow = 1; //interval to pulse X axis at highest speed, change to fit application
//
const long yhigh = 260; //interval to pulse Y axis at slowest speed, change to fit application
const long ylow = 1; //interval to pulse Y axis at highest speed, change to fit application
//
//
unsigned long previousMillisX = 0; //
unsigned long previousMillisY = 0; //
//
//
//
void setup() {
//
delay(1000); //1 second delay to allow easy drivers to power up
//
pinMode(dir_pinx, OUTPUT);
pinMode(step_pinx, OUTPUT);
pinMode(dir_piny, OUTPUT);
pinMode(step_piny, OUTPUT);
digitalWrite(step_pinx, xaxisState) ; //used in state machine switching for the stepper logic final drive (on or off setting)
digitalWrite(step_piny, yaxisState) ; //used in state machine switching for the stepper logic final drive (on or off setting)
//
}
//
void loop() {
//
unsigned long currentMillisX = millis(); //
unsigned long currentMillisY = millis(); //
//
//
int xValue = analogRead(A0); // x axis variable for joystick inputs controlling r/l PIN DEPENDENT!
int yValue = analogRead(A1); // y axis variable for joystick inputs controlling u/d PIN DEPENDENT!
//
//
if (xValue > 520) {
//if joystick moved RIGHT out of "null zone" to limit jitter
digitalWrite(dir_pinx, HIGH); // motor direction signal sent to Easy Driver X axis DIR pin (ON)
XStepDelay = map(xValue, 520, 1023, xhigh, xlow);
}
else if (xValue < 480) {
//if joystick moved LEFT out of null zone to limit jitter
digitalWrite(dir_pinx, LOW); // motor direction signal sent to Easy Driver X axis DIR pin (OFF)
XStepDelay = map(xValue, 0, 480, xlow, xhigh);
}
else if (yValue > 520) {
// if joystick moved DOWN out of null zone to limit jitter
digitalWrite(dir_piny, HIGH); //motor direction signal to Y axis Easy Driver DIR pin (ON)
YStepDelay = map(yValue, 520, 1023, yhigh, ylow); //
}
else if (yValue < 480) {
//if joystick moved UP out of null zone to limit jitter
digitalWrite(dir_piny, LOW); //motor direction signal to Y axis Easy Driver DIR pin (OFF)
YStepDelay = map(yValue, 0, 480, ylow, yhigh); //
} else {
digitalWrite(dir_piny, LOW); //motor direction signal to Y axis Easy Driver DIR pin (OFF)
digitalWrite(dir_pinx, LOW); // motor direction signal sent to Easy Driver X axis DIR pin (OFF)
// Setting LOW saves a bit of power
//could include signal to set Easy Driver(s) in "sleep" mode for geared drivetrains
}
//_____________
// If X axis (L/R) (Azimuth) motor needs to take a step, give it a step for the correct length of time,
//when that time has expired end the pulse signal
if ((xValue >= 520 || xValue <= 480) && ((unsigned long)(currentMillisX - previousMillisX) >= XStepDelay)) {
//if the joystick X axis values goes outside the null zone calculate
//how long it's been against those joystick values and control step pulse length
xaxisState = !xaxisState; // logic switch between states of step pulse pin on/off
digitalWrite (step_pinx, xaxisState); //control signal for step pin
//if the motor pulse is off, turn it on, and if the pulse is on, turn it off
previousMillisX = currentMillisX; //save the last x channel/axis pulse time on or off
}
else if (xValue <= 520 || xValue >= 480) { //if the joystick input isn't out of the null zone
digitalWrite(step_pinx, LOW); //turn off the step pulse
}
//________________
// If Y axis (U/D) (Altitude) motor needs to take a step, give it a step over the correct length of time,
//after that time has expired end the pulse signal
if ((yValue >= 520 || yValue <= 480) && ((unsigned long)(currentMillisY - previousMillisY) >= YStepDelay)) {
//if the joystick Y axis values goes outside the null zone calculate
//how long it's been against those joystick values and control step pulse length
yaxisState = !yaxisState; // logic switch between states of step pulse pin on/off
digitalWrite (step_piny, yaxisState); //control signal for step pin
//if the motor pulse is off, turn it on, and if the pulse is on, turn it off
previousMillisY = currentMillisY; //save the last y channel/axis pulse time
}
else if (yValue <= 520 || yValue >= 480) { //if the joystick input isn't out of the null zone
digitalWrite(step_piny, LOW); //turn off the step pulse
//
//restart loop
}
}
// // Not move ---- don't know why. stepper variable speed, both direction, not steeling wheel, record, https://forum.arduino.cc/t/stepper-motor-angle-and-speed-control/920340/8
// #include <MobaTools.h>
// #include <SPI.h>
// #include <SD.h>
// // hardware related definitiona
// // Set pins according to your hardware
// // ( button and enable was set to the same pin in your first attempt )
// const int stepPin = 3; //3;
// const int dirPin = 2; //4;
// const int enPin = 10; //2;
// long currentAngle = 0;
// int angle = 0;
// //float stepPerAngle = 1.8;
// int numstep;
// int joyXY;
// int joyLR;
// int joyLRmap;
// int button1 = 2;
// int button1status;
// const byte buttonPins[] = {6};
// const byte joyLRPin = A1;
// const byte joyXYPin = A0;
// const int ledPin = LED_BUILTIN;
// int speedC = 0;
// int speedCC = 0;
// int DIR = 0;
// int step_ = 0;
// int Pause = 0;
// //----------------------------
// const long maxAngle = 180; // this is the max angle in every direction.
// const int maxSpeed = 2500; // max Speed is 2500 steps/sec ( depends what the motor can do )
// const int minSpeed = 10; // lowest Speed
// const int stepsPerRev = 200; // steps per revolution with gear box
// //-----------------------------
// // limits for joystick
// const int joyLimits[] = { 0, 500, 524, 774 };
// //const int joyLimits[] = { 0, 500, 524, 1024 };
// //const int joyLimits[] = { 0, 1024, -360, 360 };
// //------------------------------------------------------------------------------
// int motorSpeed = 500; // Steps/sec
// // create MobaTools objects
// MoToStepper stepper1( stepsPerRev, STEPDIR ); // create a stepper instance
// MoToButtons myButtons( buttonPins, 1, 20, 3000 ); // manage buttons(s), longpress is 3 seconds
// MoToTimebase printTimer; // Timer to print in regular intervals without blocking sketch
// MoToTimebase joyTimer; // Timer to read joystick in regular intervals without blocking sketch
// // limits for joystick
// //const int joyLimits[] = { 245, 500, 524, 774 };
// //const int joyLimits[] = { 0, 500, 524, 1024 };
// // CS pin for SD Card Module
// const int chipSelect = 4;
// void setup() {
// Serial.print("Initializing SD card...");
// // see if the card is present and can be initialized:
// if (!SD.begin(chipSelect)) {
// Serial.println("Card failed, or not present");
// // don't do anything more:
// while (1);
// }
// Serial.println("card initialized.");
// Serial.begin (115200);
// pinMode(button1, INPUT_PULLUP);
// pinMode(ledPin, OUTPUT);
// Serial.begin (115200);
// printTimer.setBasetime( 1000 ); // print values every second
// joyTimer.setBasetime( 100 ); // read joystick 10 times a second
// pinMode(ledPin, OUTPUT);
// // initiate stepper
// stepper1.attach( stepPin, dirPin );
// stepper1.setSpeed(motorSpeed ); // rev/min (if stepsPerRev is set correctly)
// stepper1.setRampLen( stepsPerRev / 2 ); // set ramp length to what the motor needs to not loose steps
// stepper1.attachEnable( enPin, 100, LOW ); // disable motor current in standstill
// }
// void loop() {
// myButtons.processButtons();
// if (joyTimer.tick() ) {
// // read joystick and set stepper speed accordingly
// joyLR = analogRead(joyLRPin);
// currentAngle = map ( joyLR, 0, 1024, -360, 360 ); // starting position as 0 first
// // if the joystic is in the middle ===> sti
// if ( joyLR > joyLimits[1] && joyLR < joyLimits[2] ) // neutral area
// {
// digitalWrite(ledPin, HIGH);
// stepper1.rotate(0 ); // stop stepper if joystick in the middle
// }
// else
// { // move the motor in desired direction
// digitalWrite(ledPin, LOW);
// if ( joyLR >= joyLimits[2] ) {
// // move towards max angle
// motorSpeed = map ( joyLR , joyLimits[2], joyLimits[3] , minSpeed, maxSpeed ); // Values may be adjusted to the joystick
// stepper1.setSpeedSteps( motorSpeed * 10 );
// stepper1.write( maxAngle );
// } else {
// // move towards min angle
// motorSpeed = map ( joyLR , joyLimits[1], joyLimits[0] , minSpeed, maxSpeed ); // Values may be adjusted to the joystick
// stepper1.setSpeedSteps( motorSpeed * 10 );
// stepper1.write( -maxAngle );
// // move the motor to desired position
// currentAngle = map ( joyLR, 0, 1024, -360, 360 ); // starting position as 0 first
// digitalWrite(ledPin, LOW);
// stepper1.write( currentAngle );
// }
// }
// }
// if ( myButtons.longPress(0) ) {
// // button pressed long -> set referencepoint
// Serial.println( "Set referencepoint" );
// stepper1.setZero();
// }
// // if ( printTimer.tick() ) {
// // // debug printing every second
// // Serial.print( "CurrentAngle "); Serial.print(stepper1.read() );
// // Serial.print ( " step "); Serial.print (stepper1.readSteps() );
// // Serial.print ( " DIR ");
// // if (joyLR >= joyLimits[2]) Serial.print ( "right") ;
// // else if (joyLR <= joyLimits[1]) Serial.print( "left");
// // else Serial.print( "stop");
// // Serial.print ( " SpeedCC "); Serial.print (motorSpeed);
// // Serial.print(" joyLR = "); Serial.print (joyLR);
// // Serial.print(" Button= "); Serial.println (myButtons.state(0));
// // }
// // open the file. note that only one file can be open at a time,
// // so you have to close this one before opening another.
// File dataFile = SD.open("servopos.txt", FILE_WRITE);
// // if the file is available, write to it:
// if (dataFile) {
// Serial.print( "CurrentAngle = ");
// dataFile.println(currentAngle);
// dataFile.close();
// // print to the serial port too:
// Serial.println(currentAngle);
// Serial.print( " step = ");
// dataFile.println(step_);
// dataFile.close();
// // print to the serial port too:
// Serial.println(step_);
// // Serial.print( " DIR = ");
// // dataFile.println(DIR);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(DIR);
// // Serial.print( " speedCC = ");
// // dataFile.println(speedCC);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(speedCC);
// // Serial.print( " speedC = ");
// // dataFile.println(speedC);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(speedC);
// // Serial.print (" joyLRmap = ");
// // dataFile.println (joyLRmap);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(joyLRmap);
// // Serial.print (" joyXY = ");
// // dataFile.println (joyXY);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(joyXY);
// // Serial.print(" joyLR = ");
// // dataFile.println (joyLR);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(joyLR);
// // Serial.print(" Button = ");
// // dataFile.println (button1status);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(button1status);
// delay( 20 );
// }
// // if the file isn't open, pop up an error:
// else {
// Serial.println("error opening servopos.txt");
// }
// }
// //Not moving.
// #include <AccelStepper.h> //accelstepper library
// //const int stepPin = 3; //3;
// //const int dirPin = 2; //4;
// //const int enPin = 10; //2;
// AccelStepper stepper (1, 3, 2); // pulses Digital 3 (STEP), direction Digital 2 (CCW)
// // // initialize the stepper library on pins 8 through 11:
// // Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// const byte Analog_X_pin = A1; //x-axis readings
// int Analog_X = 0; //x-axis value
// void setup()
// {
// stepper.setMaxSpeed(18000); //SPEED = Steps / second
// stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
// stepper.setSpeed(18000);
// delay(500);
// }
// void loop() {
// if (stepper.run()) // This works because run() returns true if it has NOT reached the destination
// {
// // Any code put here will slow the stepper down
// // only put code here that has to run while the stepper is running
// }
// else
// {
// // All other code
// // runs when stepper is not running
// }
// }
// // stepper joystick, constant speed, both direction, steeling wheel, record
// #include <MobaTools.h>
// #include <SPI.h>
// #include <SD.h>
// const int stepPin = 3; //3;
// const int dirPin = 2; //4;
// const int enPin = 10; //2;
// long currentAngle = 0;
// int angle = 0;
// //float stepPerAngle = 1.8;
// int numstep;
// int joyXY;
// int joyLR;
// int joyLRmap;
// int button1 = 2;
// int button1status;
// const int ledPin = LED_BUILTIN;
// int speedC = 0;
// int speedCC = 0;
// int DIR = 0;
// int step_ = 0;
// int Pause = 0;
// int motorspeed = 500; // Rev/Min*10
// const int stepsPerRev = 200; // steps per revolution
// MoToStepper stepper1( stepsPerRev, STEPDIR ); // create a stepper instance
// const long maxAngle = 3; // this is the max angle in every direction, e.g. 3 = 360.
// const int maxSpeed = 2500; // max Speed is 2500 steps/sec ( depends what the motor can do )
// const int minSpeed = 10; // lowest Speed
// // limits for joystick
// //const int joyLimits[] = { 245, 500, 524, 774 };
// //const int joyLimits[] = { 0, 500, 524, 1024 };
// MoToTimebase printTimer; // Timer to print in regular intervals without blocking sketch
// MoToTimebase joyTimer; // Timer to read joystick in regular intervals without blocking sketch
// // CS pin for SD Card Module
// const int chipSelect = 4;
// void setup() {
// Serial.print("Initializing SD card...");
// // see if the card is present and can be initialized:
// if (!SD.begin(chipSelect)) {
// Serial.println("Card failed, or not present");
// // don't do anything more:
// while (1);
// }
// Serial.println("card initialized.");
// Serial.begin (115200);
// printTimer.setBasetime( 1000 ); // print values every second
// joyTimer.setBasetime( 100 ); // read joystick 10 times a second
// pinMode(button1, INPUT_PULLUP);
// pinMode(ledPin, OUTPUT);
// stepper1.attach( stepPin, dirPin );
// stepper1.setSpeed(motorspeed ); // rev/min (if stepsPerRev is set correctly)
// stepper1.setRampLen( stepsPerRev / 10); // if 2, then Ramp length is 1/2 revolution
// stepper1.attachEnable( enPin, 100, LOW ); // disable motor current in standstill
// }
// void loop() {
// button1status = digitalRead(button1);
// joyXY = analogRead(A0);
// joyLR = analogRead(A1);
// // currentAngle = map ( joyLR, 0, 1024, -360, 360 ); // starting position as 0 first
// currentAngle = map ( joyLR, 245, 0, 512, 733 ); // starting position as 245 degree first
// // if the joystic is in the middle ===> position motor to the middle
// if ( abs(currentAngle) < 20 ) // maybe adjusted
// {
// digitalWrite(ledPin, HIGH);
// stepper1.write(0 );
// }
// else
// { // move the motor to desired position
// digitalWrite(ledPin, LOW);
// stepper1.write( currentAngle );
// }
// // open the file. note that only one file can be open at a time,
// // so you have to close this one before opening another.
// File dataFile = SD.open("servopos.txt", FILE_WRITE);
// // if the file is available, write to it:
// if (dataFile) {
// Serial.print( "CurrentAngle = ");
// dataFile.println(currentAngle);
// dataFile.close();
// // print to the serial port too:
// Serial.println(currentAngle);
// Serial.print( " step = ");
// dataFile.println(step_);
// dataFile.close();
// // print to the serial port too:
// Serial.println(step_);
// // Serial.print( " DIR = ");
// // dataFile.println(DIR);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(DIR);
// // Serial.print( " speedCC = ");
// // dataFile.println(speedCC);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(speedCC);
// // Serial.print( " speedC = ");
// // dataFile.println(speedC);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(speedC);
// // Serial.print (" joyLRmap = ");
// // dataFile.println (joyLRmap);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(joyLRmap);
// // Serial.print (" joyXY = ");
// // dataFile.println (joyXY);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(joyXY);
// // Serial.print(" joyLR = ");
// // dataFile.println (joyLR);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(joyLR);
// // Serial.print(" Button = ");
// // dataFile.println (button1status);
// // dataFile.close();
// // // print to the serial port too:
// // Serial.println(button1status);
// delay( 20 );
// }
// // if the file isn't open, pop up an error:
// else {
// Serial.println("error opening servopos.txt");
// }
// }
// //USE JOYSTICK and joystick BUTTON TO SET DIFFERENT SPEED & angle & both direction & steering (ramp back) for STEPPER ---- recorder ---- press button few times n work sometime.
// //https://forum.arduino.cc/t/stepper-motor-angle-and-speed-control/920340/16
// #include <MobaTools.h>
// #include <SPI.h>
// #include <SD.h>
// // hardware related definitiona
// // Set pins according to your hardware
// const int stepPin = 3; //3;
// const int dirPin = 2; //4;
// const int enPin = 10; //2;
// const byte buttonPins[] = {6};
// const byte joyLRPin = A1;
// // Integer to hold joystick value
// int val = 0;
// const int ledPin = LED_BUILTIN;
// //----------------------------
// const long maxAngle = 3; // this is the max angle in every direction.
// const int maxSpeed = 2500; // max Speed is 2500 steps/sec ( depends what the motor can do )
// const int minSpeed = 10; // lowest Speed
// const int stepsPerRev = 200 * 10; // steps per revolution with gear box
// //-----------------------------
// // limits for joystick
// //const int joyLimits[] = { 245, 500, 524, 774 };
// const int joyLimits[] = { 0, 500, 524, 1024 };
// //------------------------------------------------------------------------------
// int joyLR;
// byte function = 1; // default is joystick
// int motorSpeed = 10; // Steps/sec.... slower, then more sensitve to joystick
// // create MobaTools objects
// MoToStepper stepper1( stepsPerRev, STEPDIR ); // create a stepper instance
// MoToButtons myButtons( buttonPins, 1, 20, 3000 ); // manage buttons(s), longpress is 3 seconds --- set back to function 1, control by joystick A0
// MoToTimebase printTimer; // Timer to print in regular intervals without blocking sketch
// MoToTimebase joyTimer; // Timer to read joystick in regular intervals without blocking sketch
// // CS pin for SD Card Module
// const int chipSelect = 4;
// void setup() {
// Serial.print("Initializing SD card...");
// // see if the card is present and can be initialized:
// if (!SD.begin(chipSelect)) {
// Serial.println("Card failed, or not present");
// // don't do anything more:
// while (1);
// }
// Serial.println("card initialized.");
// Serial.begin (115200);
// printTimer.setBasetime( 1000 ); // print values every second
// joyTimer.setBasetime( 100 ); // read joystick 10 times a second
// pinMode(ledPin, OUTPUT);
// // initiate stepper
// stepper1.attach( stepPin, dirPin );
// stepper1.setSpeed(motorSpeed ); // rev/min (if stepsPerRev is set correctly)
// stepper1.setRampLen( 10 ); // set ramp length to what the motor needs to not loose steps - back to original position
// stepper1.attachEnable( enPin, 100, LOW ); // disable motor current in standstill
// }
// void loop() {
// myButtons.processButtons();
// // select function mode by button presses
// if ( myButtons.longPress(0) ) {
// // button pressed long -> set referencepoint
// Serial.println( "Set referencepoint" );
// stepper1.setZero();
// function = 1;
// }
// switch ( myButtons.clicked(0) ) {
// case NOCLICK:
// ; // do nothing
// break;
// case DOUBLECLICK:
// Serial.println( "Return to refrencepoint" );
// stepper1.setSpeedSteps( 0 ); // Speed when moving home to 12 noon poition
// stepper1.write(0);
// function = 3;
// break;
// case SINGLECLICK:
// Serial.println( "Set function 2" );
// stepper1.setSpeedSteps( 0 ); // Speed when moving to 6pm position
// stepper1.write(4);
// function = 2;
// break;
// }
// switch ( function ) {
// case 1: // mode 1 - joystick control
// if (joyTimer.tick() ) {
// // read joystick and set stepper speed accordingly
// joyLR = analogRead(joyLRPin);
// // if the joystic is in the middle ===> sti
// if ( joyLR > joyLimits[1] && joyLR < joyLimits[2] ) // neutral area
// {
// digitalWrite(ledPin, HIGH);
// stepper1.rotate(0 ); // stop stepper if joystick in the middle
// }
// else
// { // move the motor in desired direction
// digitalWrite(ledPin, LOW);
// if ( joyLR >= joyLimits[2] ) {
// // move towards max angle
// motorSpeed = map ( joyLR , joyLimits[2], joyLimits[3] , minSpeed, maxSpeed ); // Values may be adjusted to the joystick
// stepper1.setSpeedSteps( motorSpeed * 10 );
// stepper1.write( maxAngle );
// } else {
// // move towards min angle
// motorSpeed = map ( joyLR , joyLimits[1], joyLimits[0] , minSpeed, maxSpeed ); // Values may be adjusted to the joystick
// stepper1.setSpeedSteps( motorSpeed * 10 );
// stepper1.write( -maxAngle );
// }
// }
// }
// break;
// case 2: // mode 2 compass control
// // to be implemented
// break;
// case 3: // return to reference point, switch back to joystick control if reached
// if ( !stepper1.moving() ) function = 1;
// break;
// }
// if ( myButtons.longPress(0) ) {
// // button pressed long -> set referencepoint
// Serial.println( "Set referencepoint" );
// stepper1.setZero();
// }
// // make a string for assembling the data to log:
// String dataString = "";
// // Read pot value and append to the string
// // Map to range of 0-180 for servo
// val = map(analogRead(joyLRPin), 0, 1023, 0, 180);
// dataString += String(val);
// stepper1.write(val);
// delay(15);
// if ( printTimer.tick() ) {
// // debug printing every second
// Serial.print( "CurrentAngle "); Serial.print(stepper1.read() );
// Serial.print ( " step "); Serial.print (stepper1.readSteps() );
// Serial.print ( " DIR ");
// if (joyLR >= joyLimits[2]) Serial.print ( "right") ;
// else if (joyLR <= joyLimits[1]) Serial.print( "left");
// else Serial.print( "stop");
// Serial.print ( " SpeedCC "); Serial.print (motorSpeed);
// Serial.print(" joyLR = "); Serial.print (joyLR);
// Serial.print(" Button= "); Serial.println (myButtons.state(0));
// }
// }
// // Record stepper movement with potentiometer
// // Include libraries
// #include <SPI.h>
// #include <SD.h>
// #include <Servo.h>
// #include <MobaTools.h>
// const byte stepPin = 3;
// const byte dirPin = 2;
// const int stepsPerRev = 500; // Steps per revolution - may need to be adjusted
// MoToStepper stepper1( stepsPerRev, STEPDIR ); // create a stepper instance
// // will I need to define 7 of these?
// // CS pin for SD Card Module
// const int chipSelect = 4;
// // Analog pin for potentiometer
// int analogPin = 0;
// // Integer to hold potentiometer value
// int val = 0;
// // Create a Servo object
// Servo myservo;
// void setup() {
// // Open serial communications and wait for port to open:
// Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
// Serial.print("Initializing SD card...");
// // see if the card is present and can be initialized:
// if (!SD.begin(chipSelect)) {
// Serial.println("Card failed, or not present");
// // don't do anything more:
// while (1);
// }
// Serial.println("card initialized.");
// // Attach servo on pin 9 to the servo object
// myservo.attach(9);
// stepper1.attach( stepPin, dirPin );//how about this? how to deal with this when I have many motors?
// }
// void loop() {
// // make a string for assembling the data to log:
// String dataString = "";
// // Read pot value and append to the string
// // Map to range of 0-180 for servo
// val = map(analogRead(analogPin), 0, 1023, 0, 180);
// dataString += String(val);
// // Write to the servo
// // Delay to allow servo to settle in position
// myservo.write(val);
// delay(15);
// stepper1.write(val);
// delay(15);
// // open the file. note that only one file can be open at a time,
// // so you have to close this one before opening another.
// File dataFile = SD.open("servopos.txt", FILE_WRITE);
// // if the file is available, write to it:
// if (dataFile) {
// dataFile.println(dataString);
// dataFile.close();
// // print to the serial port too:
// Serial.println(dataString);
// }
// // if the file isn't open, pop up an error:
// else {
// Serial.println("error opening servopos.txt");
// }
// }
// // not work, not detect potentiometer nor motor
// //https://arduino.stackexchange.com/questions/43996/how-to-record-steps-a-stepper-motor-takes-and-how-to-rotate-it-backward-and-forw
// #include <Stepper.h>
// // change this to the number of steps on your motor
// #define STEPS 200
// #define NOISE 0.1
// Stepper stepper(STEPS, 8, 9);
// int previous = 0;
// void setup() {
// // set the speed of the motor to 30 RPMs
// stepper.setSpeed(100);
// }
// void loop() {
// // get the sensor value
// float val = analogRead(A0);
// float difference = previous - val;
// if(difference < -NOISE || difference > NOISE)
// {
// stepper.step(val - previous);
// previous = val;
// }
// }
// /*
// not work, not detect potentiometer
// cStepper Motor Control - speed control
// //https://forum.arduino.cc/t/fsr-driven-stepper-motor-and-recording-sensors-data-in-sd-card/642407
// */
// #include <Stepper.h>
// #include <SPI.h>
// #include <SD.h>
// const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// // for your motor
// // initialize the stepper library on pins 8 through 11:
// Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// int stepCount = 0; // number of steps the motor has taken
// int buzzer = 5;
// const int chipSelect = 4;
// void setup() {
// // nothing to do inside the setup
// Serial.begin(9600);
// pinMode(5, OUTPUT);
// pinMode(6, OUTPUT);
// pinMode(7, OUTPUT);
// myStepper.setSpeed(60); // RPM
// Serial.println("Starting Initiation Process");
// tone(buzzer,100,1000);
// myStepper.step(200);
// tone(buzzer,200,1000);
// Serial.println("Initiation process finished");
// sdCard();
// }
// void sdCard()
// {
// Serial.print(F("Initializing SD card..."));
// while (!Serial)
// {
// ; // wait for serial port to connect. Needed for native USB port only
// }
// // see if the card is present and can be initialized:
// if (!SD.begin(chipSelect)) {
// Serial.println(F("Card failed, or not present"));
// // don't do anything more:
// while (1);
// }
// Serial.println(F("card initialized."));
// }
// void motorRoller()
// {
// int rpm ;
// int sensorReading = analogRead(A0);
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// if (sensorReading < 0){
// int sensorReading = analogRead(A0);
// Serial.print("Sensor Reading : ");
// Serial.println(sensorReading);
// Serial.println(" - No pressure");
// }else if(sensorReading < 100){
// //int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// if (motorSpeed > 0){
// myStepper.setSpeed(motorSpeed);
// rpm = stepsPerRevolution * 1;
// // step 1/100 of a revolution:
// //myStepper.step(stepsPerRevolution / 100);
// myStepper.step(rpm);
// }
// }else if(sensorReading < 200){
// //int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// if (motorSpeed > 0){
// myStepper.setSpeed(motorSpeed);
// rpm = stepsPerRevolution * 1.5;
// myStepper.step(rpm);
// digitalWrite(7, HIGH);
// delay(20);
// digitalWrite(7, LOW);
// }
// }else if(sensorReading < 500){
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// int sensorReading = analogRead(A0);
// if (motorSpeed > 0){
// myStepper.setSpeed(motorSpeed);
// rpm = stepsPerRevolution *2;
// // step 1/100 of a revolution:
// //myStepper.step(stepsPerRevolution / 100);
// myStepper.step(rpm);
// digitalWrite(6, HIGH);
// delay(20);
// digitalWrite(6, LOW);
// }
// }else if(sensorReading < 800){
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// if (motorSpeed > 0){
// myStepper.setSpeed(motorSpeed);
// rpm = stepsPerRevolution * 2.5;
// myStepper.step(rpm);
// digitalWrite(5, HIGH);
// delay(20);
// digitalWrite(5, LOW);
// }
// }else if(sensorReading < 900){
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// if (motorSpeed > 0){
// myStepper.setSpeed(motorSpeed);
// rpm = stepsPerRevolution * 3;
// myStepper.step(rpm);
// digitalWrite(6, HIGH);
// delay(20);
// digitalWrite(6, LOW);
// }
// }else if(sensorReading < 1000){
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// if (motorSpeed > 0){
// myStepper.setSpeed(motorSpeed);
// rpm = stepsPerRevolution *3.5;
// myStepper.step(rpm);
// digitalWrite(7, HIGH);
// delay(20);
// digitalWrite(7, LOW);
// }
// }
// else{
// Serial.println("Nothing works - check the code");
// }
// delay(100);
// }
// void fileCapture()
// {
// File myFile;
// myFile = SD.open("logger5.txt", FILE_WRITE);
// int sensorReading = analogRead(A0);
// int rpm;
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// // if the file is available, write to it:
// if (myFile) {
// // String dataString = "";
// // read the sensor value:
// int sensorReading = analogRead(A0);
// if (sensorReading < 0){
// myFile.print("MP");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// }
// else if(sensorReading < 100){
// myFile.print("A");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// }
// else if(sensorReading < 200){
// myFile.print("B");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// }else if(sensorReading <= 500){
// myFile.print("C");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// } else if(sensorReading <= 800){
// myFile.print("D");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// }else if(sensorReading < 900){
// myFile.print("E");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// }else if(sensorReading < 1000){
// myFile.print("F");
// myFile.print(",");
// myFile.print(String(sensorReading));
// myFile.println('\n');
// }
// else{
// Serial.println("All nonsense");
// }
// delay(100);
// }else {
// Serial.println(F("error opening file"));
// }
// myFile.close();
// }
// int rpm;
// void loop() {
// motorRoller();
// fileCapture();
// }
// // button not working, no movement.
// /* Position a stepper according to buttonpresses
// * The stepper is connected by means of a step/dir driver
// * The buttons are connected between pins and GND ( using internal pullups )
// */
// #include <MobaTools.h>
// const int stepRev = 3200; // steps per revolution ( 1/16 microsteps )
// // adjust stepper pins to your needs
// const byte dirPin = 2;
// const byte stepPin = 3;
// const byte enablePin = 10;
// // create stepper object
// MoToStepper myStepper( stepRev, STEPDIR );
// // create button objects
// const byte buttonPins[] = { 6 }; // adjust pins to your needs
// const long stepperPositions[] = { 0, 90, 180, 270, 360 }; // in degrees, must be same number of elements as buttonPins
// const byte buttonCnt = sizeof(buttonPins); // number of buttons/positions
// MoToButtons myButtons( buttonPins, buttonCnt, 20, 500 );
// void setup() {
// myStepper.attach(stepPin, dirPin);
// myStepper.setSpeed( 600 ); // = 60 rpm
// myStepper.setRampLen( 100 ); // 100 steps to achive set speed
// myStepper.attachEnable( enablePin, 100, LOW ); // if you want to switch off power when stepper reached position
// // a homing procedure may be needed here
// }
// void loop() {
// myButtons.processButtons(); // Check buttonstates
// for( byte pos= 0; pos<buttonCnt; pos ++ ) {
// if ( myButtons.pressed(pos) ) {
// // Button was pressed, move stepper to the according position
// myStepper.write( stepperPositions[pos] );
// }
// }
// }
// //stepper move auto left and right, print data - NO Joystick..... Program for acceleration rate control for Stepper Motor with L298N & Arduino
// //Source: https://www.ee-diary.com/2021/08/stepper-motor-acceleration-speed.html
// #include <AccelStepper.h>
// //define pins for stepper motor
// int en = 10;
// int in1 = 9;
// int in2 = 8;
// int in3 = 7;
// int in4 = 6;
// int fullstep = 4; //number of stages in full drive
// int halfstep = 8; //number of stages in half drive
// int stepdrive = fullstep; //select step drive mode
// //define stepper motor with step mode and inputs
// AccelStepper stepper(stepdrive, in1, in2, in3, in4);
// int steps = (stepdrive/4)*200; //number of steps per revolution
// long last = 0;
// int lag = 500; //time (ms) interval for display
// int dir = 1; //direction of rotation
// float rpm, v, oldspeed, a;
// int nsteps;
// void setup(){
// Serial.begin(9600); // define Serial output baud rate
// pinMode(en, OUTPUT);
// digitalWrite(en, HIGH);
// stepper.setMaxSpeed((stepdrive/4)*500); //max speed 500(steps/s) or 1000(steps/s)
// stepper.setAcceleration(800); //acceleration rate(steps/s^2)
// Serial.println("Outputs:");
// Serial.println(" (+ve=clockwise, -ve=counter clockwise)");
// Serial.println("No. of Steps RPM Acceleration(m/s^2) Speed(m/s)");
// Serial.println("-------------------------------------------------------------------\n");
// }
// void loop() {
// stepper.moveTo(dir*steps/2); //move to position ±100 or ±200
// if(stepper.distanceToGo()==0)
// dir = -dir; //change direction of rotation
// if(millis()>last + lag) //lag time elapsed since last print
// {
// v = stepper.speed(); //get motor speed (steps/s)
// nsteps = v*lag/pow(10,3); //No. of steps taken during lag time
// rpm = 60.0*v/steps; //RPM
// a = (v - oldspeed)*1000.0/lag; //Acceleration
// oldspeed = v; //update speed value
// last = millis(); //update last print time
// //Outputs
// //print No. of Steps
// Serial.print(String(nsteps)+"\t\t");
// //print RPM
// Serial.print(String(rpm,2)+"\t\t");
// //print Acceleration
// Serial.print(String(a,2)+"\t\t\t");
// //print Speed
// Serial.println(String(v));
// }
// stepper.run(); //move to new position
// }