// CONTROL MULTIPLE STEPPER OR SERVO ---- NOT TURNING WITH EITHER DELINK RESET OR ENABLE PIN....
// https://forum.arduino.cc/t/stepper-motor-angle-and-speed-control/920340/16
// #include <MobaTools.h>
// // hardware related definitiona
// // Set pins according to your hardware
// const int stepPin = 2; //3;
// const int dirPin = 3; //4;
// const int enPin = 10; //2;
// const byte buttonPins[] = {6};
// const byte joyLRPin = A1;
// const int ledPin = LED_BUILTIN;
// //----------------------------
// const long maxAngle = 400; // 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 * 50; // 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 = 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
// void setup() {
// 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( 100 ); // 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();
// // 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( 2000 ); // Speed when moving home
// stepper1.write(0);
// function = 3;
// break;
// case SINGLECLICK:
// Serial.println( "Set function 2" );
// 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();
// }
// 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));
// }
// }
// // stepper motor - auto move, left fast, right slow, NO ENPIN PLEASE
// const int dirPin = 2;
// const int stepPin = 3;
// const int stepsPerRevolution = 200;
// void setup()
// {
// // Declare pins as Outputs
// pinMode(stepPin, OUTPUT);
// pinMode(dirPin, OUTPUT);
// }
// void loop()
// {
// // Set motor direction clockwise
// digitalWrite(dirPin, HIGH);
// // Spin motor slowly
// for(int x = 0; x < stepsPerRevolution; x++)
// {
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(2000);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(2000);
// }
// delay(1000); // Wait a second
// // Set motor direction counterclockwise
// digitalWrite(dirPin, LOW);
// // Spin motor quickly
// for(int x = 0; x < stepsPerRevolution; x++)
// {
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(1000);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(1000);
// }
// delay(1000); // Wait a second
// }
// // Stepper Motor X --- joystick left n right
// const int stepPin = 3; //X.STEP
// const int dirPin = 2; // X.DIR
// // joystick
// int vrx = A0;
// int vry = A1;
// int vrx_data = 0;
// int vry_data = 0;
// int x = 0;
// int SMSpeed = 500; // Stepper Motor Speed
// void setup() {
// // Sets the two pins as Outputs
// Serial.begin(9600);
// pinMode(stepPin,OUTPUT);
// pinMode(dirPin,OUTPUT);
// pinMode(vrx , INPUT);
// pinMode(vry, INPUT);
// }
// void loop()
// {
// joystick();
// }
// void joystick()
// {
// vrx_data = analogRead(vrx);
// Serial.print("Vrx:");
// Serial.println(vrx_data);
// // to stop the stepper motor
// if ( (vrx_data > 490) && (vrx_data < 510) )
// {
// ;
// }
// if ( vrx_data > 700 )
// {
// digitalWrite(dirPin,HIGH);
// x = x + 1;
// digitalWrite(stepPin,HIGH);
// delayMicroseconds(SMSpeed);
// digitalWrite(stepPin,LOW);
// delayMicroseconds(SMSpeed);
// }
// if ( vrx_data < 300 )
// {
// digitalWrite(dirPin,LOW);
// x = x - 1;
// digitalWrite(stepPin,HIGH);
// delayMicroseconds(SMSpeed);
// digitalWrite(stepPin,LOW);
// delayMicroseconds(SMSpeed);
// }
// }
// // Control a Stepper Motor - steering wheel effect - a Joystick and return back
// // https://www.brainy-bits.com/post/control-a-stepper-motor-using-a-joystick-and-an-arduino
// #define step_pin 3 // Pin 3 connected to Steps pin on EasyDriver
// #define dir_pin 2 // Pin 2 connected to Direction pin
// #define MS1 5 // Pin 5 connected to MS1 pin
// #define MS2 4 // Pin 4 connected to MS2 pin
// #define SLEEP 7 // Pin 7 connected to SLEEP pin
// #define X_pin A0 // Pin A0 connected to joystick x axis
// int direction; // Variable to set Rotation (CW-CCW) of the motor
// int steps = 1025; // Assumes the belt clip is in the Middle
// void setup() {
// pinMode(MS1, OUTPUT);
// pinMode(MS2, OUTPUT);
// pinMode(dir_pin, OUTPUT);
// pinMode(step_pin, OUTPUT);
// pinMode(SLEEP, OUTPUT);
// digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
// delay(5); // Wait for EasyDriver wake up
// /* Configure type of Steps on EasyDriver:
// // MS1 MS2
// //
// // LOW LOW = Full Step //
// // HIGH LOW = Half Step //
// // LOW HIGH = A quarter of Step //
// // HIGH HIGH = An eighth of Step //
// */
// digitalWrite(MS1, LOW); // Configures to Full Steps
// digitalWrite(MS2, LOW); // Configures to Full Steps
// }
// void loop() {
// while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
// if (steps > 0) {
// digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps--;
// }
// }
// while (analogRead(X_pin) > 100 && analogRead(X_pin) <= 400) {
// if (steps < 512) {
// digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps++;
// }
// if (steps > 512) {
// digitalWrite(dir_pin, HIGH);
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps--;
// }
// }
// while (analogRead(X_pin) > 401 && analogRead(X_pin) <= 600) {
// if (steps < 1025) {
// digitalWrite(dir_pin, LOW);
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps++;
// }
// if (steps > 1025) {
// digitalWrite(dir_pin, HIGH);
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps--;
// }
// }
// while (analogRead(X_pin) > 601 && analogRead(X_pin) <= 900) {
// if (steps < 1535) {
// digitalWrite(dir_pin, LOW);
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps++;
// }
// if (steps > 1535) {
// digitalWrite(dir_pin, HIGH);
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps--;
// }
// }
// while (analogRead(X_pin) > 900 && analogRead(X_pin) <= 1024) {
// if (steps < 2050) {
// digitalWrite(dir_pin, LOW);
// digitalWrite(step_pin, HIGH);
// delay(1);
// digitalWrite(step_pin, LOW);
// delay(1);
// steps++;
// }
// }
// }
// // Stepper motor speed control https://www.bing.com/videos/riverview/relatedvideo?&q=paul+arduino-tutorial-35-understanding-how-to-use-a-stepper-motor%2f&qpvt=paul+arduino-tutorial-35-understanding-how-to-use-a-stepper-motor%2f&mid=9BEA35C33B8D65F11E749BEA35C33B8D65F11E74&&FORM=VRDGAR
// #include <Stepper.h>
// int stepsPerRevolution=2048; // higher, faster
// int motSpeed=10; // higher, faster
// int dt=50; // no different if 50 or 1000
// int buttonPin=2; // check video for actual connection of hardware
// int motDir=1; // check video for actual connection of hardware
// int buttonValNew;
// int buttonValOld=1;
// Stepper myStepper(stepsPerRevolution, 8,10,9,11);
// void setup() {
// // put your setup code here, to run once:
// Serial.begin(9600);
// myStepper.setSpeed(motSpeed);
// pinMode(buttonPin,INPUT);
// digitalWrite(buttonPin,HIGH);
// }
// void loop() {
// buttonValNew=digitalRead(buttonPin);
// if (buttonValOld==1 && buttonValNew==0){
// motDir=motDir*(-1);
// }
// myStepper.step(motDir*1);
// buttonValOld=buttonValNew;
// }
// drone flight controller with MPU6050 https://electronoobs.com/eng_robotica_tut5_3.php
// but can't download code??
// // control 2 stepper motors, constant speed, with joystick
// // Pin numbers
// const int X_pin = A0; // analog pin connected to X output ; A0
// const int Y_pin = A1; // analog pin connected to Y output ; A1
// // Handling X-axis joystick defects
// float Xmin = 0; // full left, should be 0
// float ZeroX = 510; // middle point, should be 512
// float Xmax = 1023; // full right, should be 1024
// float OffsetX = 100; // Handling middle mechanical play at middle point
// // Handling Y-axis joystick defects
// float Ymin = 0; // full bottom, should be 0
// float ZeroY = 490; // middle point, should be 512
// float Ymax = 1023; // full top, should be 1024
// float OffsetY = 100; // Handling middle mechanical play at middle point
// // X axis values
// float VitX = 0; // 0 = Stop ; 1 = Full Speed
// float DirX = 0; // 0 = Stop ; 1 = Clockwise ("Right") ; -1 = CounterClockwise ("Left")
// float X = 0; // from 0 to 1024, X joystick position
// // Y axis values
// float VitY = 0; // 0 = Stop ; 1 = Full Speed
// float DirY = 0; // 0 = Stop ; 1 = Clockwise ; -1 = CounterClockwise
// float Y = 0; // from 0 to 1024, Y joystick position
// // Motors control parameters
// const float DelayXmax = 7000; // Min speed for X axis
// const float DelayXmin = 500; // Max speed for X axis
// const float DelayYmax = 7000; // Min speed for Y axis
// const float DelayYmin = 500; // Max speed for Y axis
// // Stepper motor X speed variable
// float SMspeedX = (DelayXmax+DelayXmin)/2;
// // Stepper motor Y speed variable
// float SMspeedY = (DelayYmax+DelayYmin)/2;
// // Motors display management
// // Let's assume VminX = VminY = 0, these variable wont be used in the sketch
// float VmaxX = 100; // arbitrary
// float VmaxY = 100; // arbitrary
// // Motors' outputs
// const int stepPinX = 3; //X.STEP output
// const int dirPinX = 2; // X.DIR output
// // Motors output
// const int stepPinY = 3; //Y.STEP output
// const int dirPinY = 2; // Y.DIR output
// //######################################################
// void setup() {
// // Pins to read from Joystick
// pinMode(X_pin, INPUT);
// pinMode(Y_pin, INPUT);
// // Pins to digitalWrite for Motor X
// pinMode(stepPinX,OUTPUT);
// pinMode(dirPinX,OUTPUT);
// // Pins to digitalWrite for Motor Y
// pinMode(stepPinY,OUTPUT);
// pinMode(dirPinY,OUTPUT);
// //Monitoring and debugging
// Serial.begin(115200);
// }
// //######################################################
// void loop() {
// //---------------------------------------------------
// // Reading X value
// X = analogRead(X_pin);
// // Calc X Dir
// if (X < ZeroX-OffsetX) {DirX = -1;}
// else if (X > ZeroX+OffsetX) {DirX = 1;}
// else {DirX = 0;}
// // Calc X Speed to debug purposes only
// if (X < ZeroX-OffsetX) {VitX = (VmaxX/(Xmin-ZeroX+OffsetX)*X+VmaxX/((Xmin/(OffsetX-ZeroX))+1));}
// else if (X > ZeroX+OffsetX) {VitX = (VmaxX/(Xmax-ZeroX-OffsetX))*X+(VmaxX/(1-(Xmax/(ZeroX+OffsetX))));}
// else {VitX = 0;}
// // Move X StepperMotor
// if (DirX == 1) {
// StepRightX();
// //Serial.println(SMspeedX);
// }
// else if (DirX == -1) {
// StepLeftX();
// //Serial.println(SMspeedX);
// }
// else {}
// //---------------------------------------------------
// // Reading Y value
// Y = analogRead(Y_pin);
// // Calc Y Dir
// if (Y < ZeroY-OffsetY) {DirY = -1;}
// else if (Y > ZeroY+OffsetY) {DirY = 1;}
// else {DirY = 0;}
// // Calc Y Speed to debug purposes only
// if (Y < ZeroY-OffsetY) {VitY = (VmaxY/(Ymin-ZeroY+OffsetY)*Y+VmaxY/((Ymin/(OffsetY-ZeroY))+1));}
// else if (Y > ZeroY+OffsetY) {VitY = (VmaxY/(Ymax-ZeroY-OffsetY))*Y+(VmaxY/(1-(Ymax/(ZeroY+OffsetY))));}
// else {VitY = 0;}
// // Move Y StepperMotor
// if (DirY == 1) {
// StepRightY();
// //Serial.println(SMspeedY);
// }
// else if (DirY == -1) {
// StepLeftY();
// //Serial.println(SMspeedY);
// }
// else {}
// //---------------------------------------------------
// //delay(1); //Used to try to smooth movements
// return;
// // Display X
// Serial.print("X measurement:");
// Serial.print(X);
// Serial.println();
// Serial.print("VitX = ");
// Serial.print(VitX);
// Serial.println();
// Serial.print("DirX = ");
// Serial.print(DirX);
// Serial.println();
// Serial.print("X delay : ");
// Serial.print(SMspeedX);
// Serial.println();
// Serial.println();
// // Display Y
// Serial.print("Y measurement:");
// Serial.print(Y);
// Serial.println();
// Serial.print("VitY = ");
// Serial.print(VitY);
// Serial.println();
// Serial.print("DirY = ");
// Serial.print(DirY);
// Serial.println();
// Serial.print("Y delay : ");
// Serial.print(SMspeedY);
// Serial.println();
// Serial.println();
// // Delay to make Serial readable
// //delay(500);
// }
// //######################################################
// void StepRightX(){
// // Calc X micro-delay to drive stepper motor X clockwise at a given speed
// SMspeedX = ((DelayXmin-DelayXmax)/(Xmax-ZeroX-OffsetX))*X+((Xmax*DelayXmax-DelayXmin*(ZeroX+OffsetX))/(Xmax-ZeroX-OffsetX));
// //Stepping motor
// digitalWrite(dirPinX,LOW);
// digitalWrite(stepPinX,HIGH);
// delayMicroseconds(SMspeedX);
// digitalWrite(stepPinX,LOW);
// delayMicroseconds(SMspeedX);
// }
// void StepLeftX(){
// // Calc X micro-delay to drive stepper motor X counter clockwise at a given speed
// SMspeedX = ((DelayXmax-DelayXmin)/(ZeroX-OffsetX-Xmin))*X+((ZeroX-OffsetX)*DelayXmin-Xmin*DelayXmax)/(ZeroX-OffsetX-Xmin);
// digitalWrite(dirPinX,HIGH);
// digitalWrite(stepPinX,HIGH);
// delayMicroseconds(SMspeedX);
// digitalWrite(stepPinX,LOW);
// delayMicroseconds(SMspeedX);
// }
// //-----------------------------------------
// void StepRightY(){
// // Calc Y micro-delay to drive stepper motor Y clockwise at a given speed
// SMspeedY = ((DelayYmin-DelayYmax)/(Ymax-ZeroY-OffsetY))*Y+((Ymax*DelayYmax-DelayYmin*(ZeroY+OffsetY))/(Ymax-ZeroY-OffsetY));
// //Stepping motor
// digitalWrite(dirPinY,LOW);
// digitalWrite(stepPinY,HIGH);
// delayMicroseconds(SMspeedY);
// digitalWrite(stepPinY,LOW);
// delayMicroseconds(SMspeedY);
// }
// void StepLeftY(){
// // Calc Y micro-delay to drive stepper motor Y counter clockwise at a given speed
// SMspeedY = ((DelayYmax-DelayYmin)/(ZeroY-OffsetY-Ymin))*Y+((ZeroY-OffsetY)*DelayYmin-Ymin*DelayYmax)/(ZeroY-OffsetY-Ymin);
// digitalWrite(dirPinY,HIGH);
// digitalWrite(stepPinY,HIGH);
// delayMicroseconds(SMspeedY);
// digitalWrite(stepPinY,LOW);
// delayMicroseconds(SMspeedY);
// }
// // Stepper Motor X - with joystick left or right constant speed
// // https://www.electroniclinic.com/arduino-cnc-shield-v3-0-and-a4988-hybrid-stepper-motor-driver-joystick/
// //https://howtomechatronics.com/tutorials/arduino/how-to-control-stepper-motor-with-a4988-driver-and-arduino/
// const int stepPin = 3; //X.STEP
// const int dirPin = 2; // X.DIR
// // joystick
// int vrx = A0;
// int vry = A1;
// int vrx_data = 0;
// int vry_data = 0;
// int x = 0;
// int SMSpeed = 3000; // Stepper Motor Speed
// void setup() {
// // Sets the two pins as Outputs
// Serial.begin(9600);
// pinMode(stepPin,OUTPUT);
// pinMode(dirPin,OUTPUT);
// pinMode(vrx , INPUT);
// pinMode(vry, INPUT);
// }
// void loop()
// {
// joystick();
// }
// void joystick()
// {
// vrx_data = analogRead(vrx);
// Serial.print("Vrx:");
// Serial.println(vrx_data);
// // to stop the stepper motor
// if ( (vrx_data > 490) && (vrx_data < 510) )
// {
// ;
// }
// if ( vrx_data > 700 )
// {
// digitalWrite(dirPin,HIGH);
// x = x + 1;
// digitalWrite(stepPin,HIGH);
// delayMicroseconds(SMSpeed);
// digitalWrite(stepPin,LOW);
// delayMicroseconds(SMSpeed);
// }
// if ( vrx_data < 300 )
// {
// digitalWrite(dirPin,LOW);
// x = x - 1;
// digitalWrite(stepPin,HIGH);
// delayMicroseconds(SMSpeed);
// digitalWrite(stepPin,LOW);
// delayMicroseconds(SMSpeed);
// }
// }
// //a4988 basic left and right turn - connect reset to sleep to turn it on.
// // https://how2electronics.com/control-stepper-motor-with-a4988-driver-arduino/
// const int dirPin = 2;
// const int stepPin = 3;
// const int stepsPerRevolution = 200;
// void setup()
// {
// // Declare pins as Outputs
// pinMode(stepPin, OUTPUT);
// pinMode(dirPin, OUTPUT);
// }
// void loop()
// {
// // Set motor direction clockwise
// digitalWrite(dirPin, HIGH);
// // Spin motor slowly
// for(int x = 0; x < stepsPerRevolution; x++)
// {
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(2000);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(2000);
// }
// delay(1000); // Wait a second
// // Set motor direction counterclockwise
// digitalWrite(dirPin, LOW);
// // Spin motor quickly
// for(int x = 0; x < stepsPerRevolution; x++)
// {
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(1000);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(1000);
// }
// delay(1000); // Wait a second
// }
// #include <Stepper.h>
// const int stepsPerRevolution = 100; // 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
// void setup() {
// // nothing to do inside the setup
// }
// void loop() {
// // read the sensor value:
// int sensorReading = analogRead(A0);
// // map it to a range from 0 to 100:
// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// // set the motor speed:
// if (motorSpeed > 0) {
// myStepper.setSpeed(motorSpeed);
// // step 1/100 of a revolution:
// myStepper.step(stepsPerRevolution / 100);
// }
// }