// // NOT work, servo can move, not stepper ---- Record servo movement
// // Include libraries
// #include <SPI.h>
// #include <SD.h>
// #include <Servo.h>
// #include <MobaTools.h>
// // 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;
// const int stepPin = 3;
// const int dirPin = 2;
// int customDelay,customDelayMapped; // Defines variables
// const int stepsPerRev = 500; // Steps per revolution - may need to be adjusted
// // MoToStepper stepper1( stepsPerRev, STEPDIR ); // create a stepper instance
// MoToStepper stepper1( stepPin, dirPin ); // create a stepper instance
// 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?
// pinMode(stepPin,OUTPUT);
// pinMode(dirPin,OUTPUT);
// digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
// }
// 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);
// // 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");
// }
// customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(customDelayMapped);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(customDelayMapped);
// }
// // Function for reading the Potentiometer
// int speedUp() {
// int customDelay = analogRead(A0); // Reads the potentiometer
// int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
// return newCustom;
// }
// control stepper speed with potentiometer knob
// Defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}
/*
Servo position recorder
servo-record.ino
Records servo movements on SD card
Displays results on Serial Monitor
DroneBot Workshop 2019
https://dronebotworkshop.com/sd-card-arduino/
*/
//playback replay servo movement
//Include libraries
// #include <SPI.h>
// #include <SD.h>
// #include <Servo.h>
// // CS pin for SD Card Module
// const int chipSelect = 4;
// // String to hold one line of text
// String buffer;
// // 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);
// // 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");
// // If the file is available, read it
// if (dataFile) {
// while (dataFile.available()) {
// // Write one line to buffer
// buffer = dataFile.readStringUntil('\n');
// // Print to serial monitor
// Serial.println(buffer);
// // Convert string to integer and position servo
// myservo.write(buffer.toInt());
// delay(15);
// }
// dataFile.close();
// }
// // if the file isn't open, pop up an error:
// else {
// Serial.println("error opening servopos.txt");
// }
// }
// void loop() {
// }
// // Record servo movement
// // Include libraries
// #include <SPI.h>
// #include <SD.h>
// #include <Servo.h>
// // 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);
// }
// 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);
// // 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");
// }
// }
/*
Connect potentiometer to SD card datalogger, first time
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// #include <SPI.h>
// #include <SD.h>
// const int chipSelect = 4;
// 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.");
// }
// void loop() {
// // make a string for assembling the data to log:
// String dataString = "";
// // read three sensors and append to the string:
// for (int analogPin = 0; analogPin < 3; analogPin++) {
// int sensor = analogRead(analogPin);
// dataString += String(sensor);
// if (analogPin < 2) {
// dataString += ",";
// }
// }
// // 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("datalog.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 datalog.txt");
// }
// }
// /*
// Loading SD card read/write - first time
// This example shows how to read and write data to and from an SD card file
// The circuit:
// * DO – Master In/Slave Out (or MISO)
// Vss – Ground.
// CLK – Clock.
// Vcc – Supply voltage.
// DI – Master Out/Slave In (MOSI)
// CD# pin is the pin for card detect.
// * SD card attached to SPI bus as follows:
// ** MOSI - pin 11
// ** MISO - pin 12
// ** CLK - pin 13
// ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
// created Nov 2010
// by David A. Mellis
// modified 9 Apr 2012
// by Tom Igoe
// This example code is in the public domain.
// */
// #include <SPI.h>
// #include <SD.h>
// File myFile;
// 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...");
// if (!SD.begin(4)) {
// Serial.println("initialization failed!");
// while (1);
// }
// Serial.println("initialization done.");
// // open the file. note that only one file can be open at a time,
// // so you have to close this one before opening another.
// myFile = SD.open("test.txt", FILE_WRITE);
// // if the file opened okay, write to it:
// if (myFile) {
// Serial.print("Writing to test.txt...");
// myFile.println("testing 1, 2, 3.");
// // close the file:
// myFile.close();
// Serial.println("done.");
// } else {
// // if the file didn't open, print an error:
// Serial.println("error opening test.txt");
// }
// // re-open the file for reading:
// myFile = SD.open("test.txt");
// if (myFile) {
// Serial.println("test.txt:");
// // read from the file until there's nothing else in it:
// while (myFile.available()) {
// Serial.write(myFile.read());
// }
// // close the file:
// myFile.close();
// } else {
// // if the file didn't open, print an error:
// Serial.println("error opening test.txt");
// }
// }
// void loop() {
// // nothing happens after setup
// }
// /*
// serial config:
// Board: Arduiono Pro / Pro Mini
// Port: tty.usbseriala400eMNr
// Programmer: USBtinyISP
// */
// // Definitionen
// #include <Servo.h> // servo treiber
// Servo servo_0;
// Servo servo_1;
// Servo servo_2;
// Servo servo_3;
// int sensorPin0 = A0; // Schulter
// int sensorPin1 = A1; // Handfind
// int sensorPin2 = A2; // Ellbogen
// int sensorPin3 = A3; // Zange
// int count0, arrayStep, arrayMax, countverz, Taster, stepsMax, steps, time = 1000, del = 1000, temp;
// //arraystep = memory what pos in the array
// //arrayMax = max steps we safed to array
// //countverz = seems to be something to calculate the delay between complete moves
// //Taster = Button
// //stepsMax = longest way a servo have to travel
// //steps = single steps for a move between stored positions
// unsigned int verz = 0;
// long previousMillis1 = 0;
// long previousMillis2 = 0;
// long previousMillis3 = 0;
// long previousMillis4 = 0;
// long previousMicros = 0;
// unsigned long currentMillis = millis();
// unsigned long currentMicros = micros();
// // arrays
// int Delay[7] = {0,0,1,1,1,1,1}; // array to map gripper pot to delay in seconds
// int SensVal[4]; // sensor value
// float dif[4], ist[4], sol[4], dir[4]; // difference between stored position and momentary position
// int joint0[180];// array for servo(s)
// int joint1[180];
// int joint2[180];
// int joint3[180];
// int top = 179; // we should not write over the end from a array
// // status
// boolean playmode = false, Step = false;
// void setup()
// {
// pinMode(4, INPUT); // sets the digital pin 4 as input
// pinMode(6, INPUT);
// pinMode(13, OUTPUT); // sets the digital pin 13 as outtput
// digitalWrite(13, HIGH); // sets the LED on
// servo_0.attach(3); // attaches the servo
// servo_1.attach(10);
// servo_2.attach(5);
// servo_3.attach(11);
// Serial.begin(115200); // Baudrate have to be same on the IDE
// Serial.println("mini robot ready...");
// //delay(1000);
// digitalWrite(13, LOW);
// }
// void loop() // here we go!
// {
// currentMillis = millis(); // all is about timing
// currentMicros = micros();
// // read the button
// Button();
// if(!playmode) // manualy modus
// {
// if(currentMillis - previousMillis1 > 25) // 25miliseconds until next manual mode update
// {
// if (arrayStep < top)
// {
// previousMillis1 = currentMillis; //reset
// readPot(); // get the value from potentiometers
// mapping(); // map to milliseconds for servos
// move_servo(); // setz newservo position
// //record();
// } // end counter < max
// } // end step check
// } // ende manualy move
// else if(playmode) // play
// {
// if (Step) // next step read from array
// {
// digitalWrite(13, HIGH); //LED
// if (arrayStep < arrayMax) // we not reach the end from stored data
// {
// arrayStep += 1; // next array pos
// Read(); // from the arrays
// calculate(); // find biggest travel distance and calculate the other 3 servos (the have to do smaler steps to be finished at same time!)
// Step = 0;
// digitalWrite(13, LOW);
// }
// else // array read finished > start over
// {
// arrayStep = 0; //
// calc_pause(); // delay between moves read from potentiometer
// countverz = 0; // used for the delay
// while(countverz < verz) // verz = time getting from calc_pause();
// { // here we do loop and wait until next start over
// countverz += 1;
// calc_pause();
// digitalWrite(13, HIGH); delay(25);
// digitalWrite(13, LOW); delay(975);
// }
// }
// //Serial.println(arrayStep);
// }
// else // do the servos!
// {
// if (currentMicros - previousMicros > time) // here we do a single micro step
// { //
// previousMicros = currentMicros;
// play_servo();
// }
// }
// }// ende playmode
// // ---------------------------------------------------------------------------------Hardware pause switch PIN 6
// while (digitalRead(4) == false)
// {
// digitalWrite(13, HIGH); delay(500);
// digitalWrite(13, LOW); delay(500);
// }
// // ---------------------------------------------------------------------------------- Textout serial
// // serial ausgabe 1 sek
// /*if(currentMillis - previousMillis2 > 5000)
// {
// previousMillis2 = currentMillis;
// /*count0 = 0;
// while(count0 < 4)
// {
// int val = SensVal[count0];
// // val = map(val, 142, 888, 0, 180);
// Serial.println(val);
// //Serial.println("test");
// count0 += 1;
// }
// Serial.println(playmode);
// Serial.println(arrayStep);
// Serial.println(arrayMax);
// Serial.println(" ");
// }*/
// }
// // ---------------------------------------------------------------------------------------- sub routinen
// void calc_pause() // read pot and map to usable delay time after a complete move is done
// {
// readPot();
// temp = SensVal[3];
// if (temp < 0) temp = 0;
// temp = map(temp, 0, 680, 0 ,5);
// verz = Delay[temp]; // verz = delay in second
// Serial.print(temp);
// Serial.print(" ");
// Serial.print(verz);
// Serial.print(" ");
// Serial.println(countverz);
// }
// void readPot() // read analog inputs and add some offsets (mechanical corrections)
// {
// SensVal[0] = analogRead(sensorPin0); //SensVal[0] += -10; // rotate
// SensVal[1] = analogRead(sensorPin1); //SensVal[1] += 280; // Shoulder
// SensVal[2] = analogRead(sensorPin2); //SensVal[2] += -50; // hand
// SensVal[3] = analogRead(sensorPin3); // SensVal[3] += 0;// gripper
// Serial.print(SensVal[2]);Serial.print(" "); // CHECK
// }
// void mapping() // we need microsecond for the servos instead potentiometer values
// {
// ist[0] = map(SensVal[0], 150, 900, 600, 2400);// drehen
// ist[1] = map(SensVal[1], 1000, 100, 550, 2400);// Schulter
// ist[2] = map(SensVal[2], 120, 860, 400, 2500);// Hand
// ist[3] = map(SensVal[3], 1023, 0, 500, 2500);// Zange
// Serial.println(ist[2]); // CHECK
// }
// void record()
// {
// joint0[arrayStep] = ist[0]; // write positions in servo array
// joint1[arrayStep] = ist[1];
// joint2[arrayStep] = ist[2];
// joint3[arrayStep] = ist[3];
// }
// void Read()
// {
// sol[0] = joint0[arrayStep]; // read from the array
// sol[1] = joint1[arrayStep];
// sol[2] = joint2[arrayStep];
// sol[3] = joint3[arrayStep];
// }
// void move_servo()
// {
// servo_0.writeMicroseconds(ist[3]); // send milissecond values to servos
// servo_1.writeMicroseconds(ist[2]);
// servo_2.writeMicroseconds(ist[0]);
// servo_3.writeMicroseconds(ist[1]);
// }
// // ------------------------------------------------------------ single steps calculating
// void calculate()
// {
// // travel distance for each servo
// dif[0] = abs(ist[0]-sol[0]);
// dif[1] = abs(ist[1]-sol[1]);
// dif[2] = abs(ist[2]-sol[2]);
// dif[3] = abs(ist[3]-sol[3]);
// // biggest travel way from all 4 servos
// stepsMax = max(dif[0],dif[1]);
// stepsMax = max(stepsMax,dif[2]);
// stepsMax = max(stepsMax,dif[3]);
// // stepsMax is the biggest distance a servo have to do beween momentary position and new pos read from the array
// //Serial.println(stepsMax);
// if (stepsMax < 500) // del(ay) between a single step is bigger is move is smaler. just looks cool
// del = 1200;
// else
// del = 600;
// // calculating single (micro) step for each servo
// // need that to do move all servos in a loop (stepsMax times done) with different values.
// // This makes all servos have done the traveling distance at same time
// if (sol[0] < ist[0]) dir[0] = 0-dif[0]/stepsMax; else dir[0] = dif[0]/stepsMax;
// if (sol[1] < ist[1]) dir[1] = 0-dif[1]/stepsMax; else dir[1] = dif[1]/stepsMax;
// if (sol[2] < ist[2]) dir[2] = 0-dif[2]/stepsMax; else dir[2] = dif[2]/stepsMax;
// if (sol[3] < ist[3]) dir[3] = 0-dif[3]/stepsMax; else dir[3] = dif[3]/stepsMax;
// //Serial.println(dir4);
// }
// void play_servo()
// {
// steps += 1;
// if (steps < stepsMax) // sure we not reach the end from a move
// {
// //time = del*5;// anfahr rampe
// if(steps == 20) time = del*4; // ramp up
// else if(steps == 40) time = del*3; // time is the delay in microsecns we wait in the mainloop until
// else if(steps == 80) time = del*2; // a micro step will be done
// else if(steps == 100) time = del-1; // cannot explain here is not del*1
// if(steps == stepsMax-200) time = del*2; // stop ramp down (200 microsteps before end time will be increased
// else if(steps == stepsMax-80) time = del*3;
// else if(steps == stepsMax-40) time = del*4;
// else if(steps == stepsMax-20) time = del*5;
// ist[0] += dir[0]; // set new pos
// ist[1] += dir[1];
// ist[2] += dir[2];
// ist[3] += dir[3];
// servo_0.writeMicroseconds(ist[3]); // Zange //anschlüsse gemappt!
// servo_1.writeMicroseconds(ist[2]); // Hand
// servo_2.writeMicroseconds(ist[0]); // Schulter
// servo_3.writeMicroseconds(ist[1]); // Ellbogen
// }
// else
// {
// Step = 1; // next step aus array lesen
// steps = 0; // servo zwischenschritte
// }
// }
// void data_out() // just to write the recorded data to serial
// {
// int i = 0;
// while(i < arrayMax)
// {
// digitalWrite(13, HIGH);
// i += 1;
// Serial.print(joint0[i]); Serial.print(", ");
// }
// Serial.println("Joint0");
// i = 0;
// while(i < arrayMax)
// {
// digitalWrite(13, HIGH);
// i += 1;
// Serial.print(joint1[i]); Serial.print(", ");
// }
// Serial.println("Joint1");
// i = 0;
// while(i < arrayMax)
// {
// digitalWrite(13, HIGH);
// i += 1;
// Serial.print(joint2[i]); Serial.print(", ");
// }
// Serial.println("Joint2");
// i = 0;
// while(i < arrayMax)
// {
// digitalWrite(13, HIGH);
// i += 1;
// Serial.print(joint3[i]); Serial.print(", ");
// }
// Serial.println("Joint3");
// }
// void Button() // check buttons for single and doubleclick
// {
// if (digitalRead(6) == false)
// {
// delay(1);
// if (digitalRead(6) == true) // taster losgelassen
// {
// if (Taster == 0)
// {
// Taster = 1;
// previousMillis3 = currentMillis;
// //Serial.print("Status Record "); Serial.println(Taster);
// }
// else if ((Taster == 1) && (currentMillis - previousMillis3 < 250))
// {
// Taster = 2;
// //Serial.println(Taster);
// }
// /*else if ((Taster == 2) && (currentMillis - previousMillis3 < 500))
// {
// Taster = 3;
// Serial.println(Taster);
// }*/
// }
// }
// if ((Taster == 1) && (currentMillis - previousMillis3 > 1000)) // write to array
// {
// arrayStep += 1;
// arrayMax = arrayStep;
// record();
// Taster = 0;
// playmode = false;
// Serial.print("Record Step: "); Serial.println(arrayStep);
// digitalWrite(13, HIGH);
// delay(100);
// digitalWrite(13, LOW);
// }
// else if (Taster == 2)
// {
// arrayStep = 0;
// playmode = true;
// Taster = 0;
// Step = 1;
// Serial.println("playmode ");
// data_out();
// delay(250);
// digitalWrite(13, LOW);
// }
// /*if (Taster == 3)
// {
// // ++ arrayStep
// // playmode = 1;
// Taster = 0;
// Serial.println("Clear ");
// }*/
// if (currentMillis - previousMillis3 > 2000) // button Status clear
// {
// Taster = 0;
// //Serial.println("restart ");
// }
// }
// /* auto move stepper, one direction, one speed. ====== minimumStepper =======================================
// https://forum.arduino.cc/t/moba-tools-library-controlling-mutliple-stepper-motors-and-a4988/1053718/13
// Bare minimum to get a stepper with step/dir driver turning
// */
// #include <MobaTools.h>
// // Stepper connections - Please adapt to your own needs.
// const byte stepPin = 3;
// //i assume I would need to do something like this const byte stepping[7]= {5, 7, 8, ...};
// const byte dirPin = 2;
// //i would use an array and define all the pins for 7 motors
// //or can I actually connect all dir pins from the drivers to a single pin in Arduino?
// //Note that I won't need to rotate two motors in different directions simultinusly
// 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?
// void setup() {
// stepper1.attach( stepPin, dirPin );//how about this? how to deal with this when I have many motors?
// stepper1.setSpeed( 300 );
// //i want the same speed for all of them, so how to apply this to all of them
// stepper1.setRampLen( stepsPerRev / 2);
// // Ramp length is 1/2 revolution, same thing, how to apply this to all?
// stepper1.rotate(1);
// // start turning, 1=vorward, -1=backwards, same thing.. how to apply this to all?
// }
// //USE JOYSTICK and joystick BUTTON TO SET DIFFERENT SPEED & angle for STEPPER ---- 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 = 3; //3;
// const int dirPin = 2; //4;
// const int enPin = 10; //2;
// const byte buttonPins[] = {6};
// const byte joyLRPin = A1;
// 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 * 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 = 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
// 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 to 12 noon poition
// stepper1.write(0);
// function = 3;
// break;
// case SINGLECLICK:
// Serial.println( "Set function 2" );
// stepper1.setSpeedSteps( 5000 ); // 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();
// }
// 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));
// }
// }