0

I am controlling a stepper motor with a potentiometer and I am trying to map the potentiometer values (0-1023) to the stepper motor steps (200). I only want this stepper motor to turn 1 (one) rotation from 0 on the potentiometer (0 steps on the stepper, yes I have a micro switch to mark 0) all the way to 1023 on the potentiometer (200 steps on the stepper). But I have trouble understanding the map() function.

potValue = map ( potPinValueRead,0,1023,0,200) ;

As per the Arduino example:

map(value, fromLow, fromHigh, toLow, toHigh)

I think I am interpreting the map() function wrong

UPDATE: Even with the map() values the stepper still turns 5 (and a bit (1023/200) ) rotations and not just 1 (one) rotation.

Here is my code:

#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 7, 8, 9, 10);
int previous = 0 ;
int potData = 0 ;
int potPin = A0 ;

int outputA = 7 ;  //Stepper Output
int outputB = 8 ;  //Stepper Output
int outputC = 9 ;  //Stepper Output
int outputD = 10 ; //Stepper Output

int limitSwitch = 2 ; //Limit switch (home switch) is on pin 2

void setup() {
   Serial.begin(9600) ;
   stepper.setSpeed(10);
   pinMode ( outputA, OUTPUT ) ;
   pinMode ( outputB, OUTPUT ) ;
   pinMode ( outputC, OUTPUT ) ;
   pinMode ( outputD, OUTPUT ) ;
   pinMode ( limitSwitch, INPUT ) ;

   //Turn the motor until the limit scwitch is triggered
   while ( digitalRead(limitSwitch) == 0 )
    {
      stepper.step(30);
    }
   //Stop turning when the limit switch has been triggered
   while ( digitalRead(limitSwitch) == 1 )
    {
      stepper.step(0);
    }
   //Turn 5 steps away from the limit switch to un-trigger
   //This is also the "home" position
   stepper.step(-5);
 }

 void loop() {
   int potData = map(analogRead(potPin),0,1024,0,200);

   if (potData>previous)
    {
      stepper.step(5);
    }
   if (potData<previous)
    {
      stepper.step(-5);
    }

  previous = potData;
}
  • I have updated my post and added code – Danny Jan 29 at 7:30
  • 1
    you have misdiagnosed the problem ...... write a sketch that moves 200 steps, waits for a second, then repeats .... no potentiometer code, no limit switch code ... do you get one revolution? ..... you have to get 1 rev per 200 steps before you do anything else – jsotola Jan 29 at 7:38
  • I did as you suggested and I get 1 revolution for 200 steps – Danny Jan 29 at 8:03
  • now you are certain that the hardware works correctly ..... that means that the problem is inside your program ...... look at your loop() code ..... how many steps will the motor move if you change the potentiometer by one "position"? – jsotola Jan 29 at 19:37
  • @jsotola : it turns out the Arduino I was using was defective. I bought 20 of them and it turns out 4 of them had the same issues (the supplier got it from you know where). I returned all for genuine Arduino's. Anyway, everything is working fine now. Thank you for your help. – Danny Jan 30 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.