Hi !!
Amazing! 
The code you writed works perfect !!
The stepper im using for testing now is only 200steps/revolution so i changed that to get it runnning.
I tried to add an if statment to the button (shortpress) and it works to print out "return to refrencepoint" But im not able to get the stepper to return to Refrencepoint. I tried using the
stepper1.stepsToDo();
but nothing happend,
The thing i want to happen when the button is pressed short is to get the motor return to refrencepoint.
When i get the compass sensor running i will read out an angle, when i press the button twice i want the stepper to move hold the angle set when pushing twice on the button. What function is best to use for this task? If button pressed twice set compass refrence point, if compas direction change the stepper should move the same amount of rotation.
Once again Thank you som much for helping me with this !
Best Regards
Andreas
#include <MobaTools.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 = 6; //3;
const int dirPin = 5; //4;
const int enPin = 7; //2;
const byte buttonPins[] = {2};
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; // steps per revolution with gear box 200 * 50
//-----------------------------
// limits for joystick
const int joyLimits[] = { 245, 500, 524, 774 };
//const int joyLimits[] = { 0, 500, 524, 1024 };
//------------------------------------------------------------------------------
int joyLR;
int joyLRmap;
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();
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 );
}
}
}
if ( myButtons.longPress(0) ) {
// button pressed long -> set referencepoint
Serial.println( "Set referencepoint" );
stepper1.setZero();
}
if ( myButtons.shortPress(0) ) {
// button pressed short -> return stepper to refrencepoint
Serial.println( "Return to refrencepointt" );
stepper1.stepsToDo();
}
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));
}
}