*solves* map() in hang up for some seconds the uC/little Stuck Overflow - Why?

Hi guys,

i tried to get some easy programming. I recognised that the easy stuff is sometimes not that easy!

I wanna have two posible conditions for a switch. The first is a absolute movement for a stepper, the other is a relative movement for a stepper.

Absolute movement:
1 Poti is for movement
1 Poti is for speed

Relative movement:
only 1 Poti for speed (next step will be a absolute middle point...but that is the sound of the future :wink: )

So now the problem , in the "Relative movement"-program-part the line with the mapping of the poti valueis causing a delay when the value is around "zero"! BUT i can see in the serial monitor that there is only a problem when its near the minimum value of the poti. I can see that the data in the serial monitor stops for 2-5 seconds and then the program is running on. It looks like a buffer overflow or something else?!?!?!

Has somebody a solution for me??

//Stepper Bibliothek
#include <Stepper.h>
#define STEPS 24  // change this to the number of steps on your motor
Stepper stepper(STEPS, 8, 9, 10, 11); //  create an instance of the stepper class, define Steps, define Mototr Pins


// Eingaenge
int wahlAbsolutPin = 12;  // Wahlschalter Absolut
int wahlRelativPin = 13;  // Wahlschalter Relativ

// Ausgaenge
int enablePin1 = 6;
int enablePin2 = 7;

// Poti Parameter
int sensorSpeed = 0;
int sensorSteps = 0;
int potiMitte = 512;

// Parameter
int prevPotVal = 0;
int motorMove = 0;
int motorSpeed = 0;
int previousmotorSpeed = 0;
const int stepsPerRevolution = 200;




void setup() 
{
 
  Serial.begin(115200);
  pinMode(wahlAbsolutPin,INPUT);  // Wahlschalter Absolut
  pinMode(wahlRelativPin,INPUT);  // Wahlschalter Relativ
  pinMode(enablePin1,OUTPUT);
  pinMode(enablePin2,OUTPUT);
  
}


void loop() 
{
  
  sensorSteps = analogRead(A0);
  sensorSpeed = analogRead(A1);
  
  if (digitalRead(wahlAbsolutPin) == HIGH) {
         
          digitalWrite(enablePin1, HIGH);
          digitalWrite(enablePin2, HIGH);
  
          motorMove = map(sensorSteps, 0, 1023, 24, 0);        // get potiMove and map it 
          motorSpeed = map(sensorSpeed, 0, 1023, 150, 1500);   // get potiSpeed and map it

          stepper.step(motorMove - prevPotVal);
          stepper.setSpeed(motorSpeed);
          
          prevPotVal = motorMove;
      
                                          } 
      
      
  else if (digitalRead(wahlRelativPin) == HIGH) {
          
    
           digitalWrite(enablePin1, HIGH);
           digitalWrite(enablePin2, HIGH);
  
           motorSpeed = map(sensorSpeed, 0, 1023, 10, 1000);   // get potiSpeed and map it
               if (motorSpeed > 0) {
                      stepper.setSpeed(motorSpeed);
                      stepper.step(stepsPerRevolution/100);
                                    }
                                            }
 
   else {
           digitalWrite(enablePin1, LOW);
           digitalWrite(enablePin2, LOW);
         }
       
    Serial.println(motorMove);
    Serial.println(motorSpeed);
   delay(2);  
}

Two to five seconds smells like a reboot. It could be rebooting on a voltage droop when the resistance is low. If you put a Serial.println('Here i am"); in the setup() you'll quickly find out if it's restarting.

-br

By my calculations when you have the relative speed set to a minimum its 10rpm, which with a 24 step motor is 250ms per step - so the call to stepper.step(stepsPerRevolution/100); will take 0.5s to run, perhaps that's it?

Anyway you seem to have two different ideas of how many steps your motor is - 24 as here:

#define STEPS 24  // change this to the number of steps on your motor
Stepper stepper(STEPS, 8, 9, 10, 11); //  create an instance of the stepper class, define Steps, define Mototr Pins

and 200 as here:

const int stepsPerRevolution = 200;

Thanks guys for the fast replay!

@billroy:

I tried it its not a restart!

@MarkT:

so the "10" was only a "debug 10". There is normaly "0". But the time isnt always the same. Thats why i wrote 2-5 sec.When im spinning the poti fast i can see in the "higher" value that its changing; and that fast. when im spinning and eeverytime of the spinning down to zero i got the slomotion thing!

Oh and the difference: When im doing only speed change i can go faster. When im doing movement and speed its not possible. Dont know why but its working :wink:

Maybe i need a procedure that is managing the analogread()?

Update:
When im at f.e. at 100 an spinning the poti fast to 1000 nothing happens and the analogRead() its running on.
When im at 0 an spinning to something else it needs 1-2 sec for an analogRead() value; when im spinning 5 or 10 times fast from 0 to something more it takes more than 1-2 seconds to get the next values whatever which value its at the end..okay except 0 :wink:

Update2:
Maybe its the jittering from the Poti at the minimum. Is there a posbility to smooth this?

I think the stepper functions you're using will block (wait) until the stepper has completed the movement. In the 'relative' case you seem to be setting the speed from the pot reading and then stepping through a fixed angle. Obviously, this will take longer and longer the more you lower the speed, and I couldn't predict what would happen if your speed reduced to zero. It doesn't seem a particularly useful thing to do - is this the behaviour you were trying to produce in 'relative' mode?

motorSpeed = map(sensorSpeed, 0, 1023, 10, 1000);   // get potiSpeed and map it
               if (motorSpeed > 0) {
                      stepper.setSpeed(motorSpeed);
                      stepper.step(stepsPerRevolution/100);
                                    }

motorSpeed will always be above zero, so ....

@robtillaart
like i said in my second post...this was only for debugging!

@PeterH
you got the problem. the library has noc trigger aan is a no non-blocking stepper library. i found a non-blocking library in the web. its an open source library and its called "OpenMoCo AVR Libraries"

SO THANK YOU VERY TO ALL!