Ohm and Potentiometer and Map and is Map a good method

I am using map and a potentiometer to set a parameter that I use to set the speed of a stepper motor.

SpeedM=analogRead(potPin);
speedM=map(speedM,0,1023,minSpeed,maxSpeed);

And it is working. My question what ohm value for the pot would give the best result? Also, is using map the right way to read a pot?

Define "best result'

No, that would be "analogRead"

It’s hard to guess what you’re getting…

You’re using speedM and SpeedM interchangeably - probably not what you’re planning or expecting.

The choice of pot depends on several things, but likely a 10k linear taper would be a good starting point.

none since you just need the percentage of rotation. but a higher value pot (e.g. 10k) will draw less current

Hi,
Can you please post your entire code.
The snippet you have posted has a variable naming error in it, so a look at your code will help us understand your program flow.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

This is the code that has the map in it.

/*Description:
Stepper motor control

  • Circuit:
  • Arduino Component
  •  2  ------->  signal A encoder       
    
  •  3  ------->  signal B encoder
    
  •  4  ------->  left toggle switch 
    
  •  5  ------->  right toggle switch 
    
  •  6  ------->  left push button 
    
  •  7  ------->  right push button
    
  •  8  ------->  direction pin stepper driver
    
  •  9  ------->  step pin stepper driver
    10  ------->  limit switch at left side
    11  ------->  limit switch at right side
    A0  ------->  potentiometer
    

*/

//Libraries
//#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h> //library for the rotary encoder

Encoder myEnc(2, 3); //define pins 2 and 3 for the rotary encoder

//Pins definitions
#define pulPin 9 //step pin
#define dirPin 8 //direction
#define potPin A0 //pot is on arduino A0
#define switchLPin 4 //toggle switch left for method 2
#define switchRPin 5 //toggle switch right for method 2
#define buttonLPin 6 // left push button for method 3
#define buttonRPin 7 // right push button for method 3
#define limitLPin 10 // limit switch at left side
#define limitRPin 11 // limit switch at right side
#define motorInterfaceType 1

//Declare variables
long position = 0; //rotary encoder position
long newPos=0; //new rotary encoder position
uint16_t speedM=0; // motor speed
uint16_t speedC=0; //current speed
bool flagEncoder=0; //flag indicator that encoder has been moved

//Setting parameters
uint16_t maxSpeed=500; //the speed more faster
uint16_t minSpeed=1000; //the speed more slower
uint16_t accel=2; //accleration of the motor
uint16_t stepM=0; //how many steps goes the stepper motor with encoder control
bool leftDir=0; //direction value for left direction
bool rightDir=1; //direction value for right direction

void setup() {
//pins as input
pinMode(switchLPin,INPUT_PULLUP); //set as input with pullup
pinMode(switchRPin,INPUT_PULLUP); //set as input with pullup
pinMode(buttonLPin,INPUT_PULLUP); //set as input with pullup
pinMode(buttonRPin,INPUT_PULLUP); //set as input with pullup
pinMode(limitLPin,INPUT_PULLUP); //set as input with pullup
pinMode(limitRPin,INPUT_PULLUP); //set as input with pullup

//pins as output
pinMode(pulPin,OUTPUT); // set Pin9 as PUL
pinMode(dirPin,OUTPUT); // set Pin8 as DIR

Serial.begin(9600); //initialize the serial transmition at 9600 bauds

speedC=minSpeed;
speedM=minSpeed;

}

void loop() {

//=========Method 1 and 3=========
if(digitalRead(switchLPin)==HIGH && digitalRead(switchRPin)==HIGH){

rotaryEncoder();

readButtons();

}
//=========Method 2===========
else if(digitalRead(switchLPin)==LOW){ //method 2 -->left
digitalWrite(dirPin,leftDir);
speedM=analogRead(potPin);
speedM=map(speedM,0,1023,minSpeed,maxSpeed);

if (speedM != speedC) {
   
    if(speedM>speedC){ 
        speedC+=accel;
        
    }
    else if(speedM<speedC){ 
        speedC-=accel;
        
    }
    
 }

 if(digitalRead(limitLPin)==HIGH){
    digitalWrite(pulPin,HIGH); // Output high
	delayMicroseconds(speedC); // set rotate speed
	digitalWrite(pulPin,LOW); // Output low
	delayMicroseconds(speedC); // set rotate speed
 }
 else{
 	speedC=minSpeed;
 }

}
else if(digitalRead(switchRPin)==LOW){ //method 2 -->right

 digitalWrite(dirPin,rightDir);
 speedM=analogRead(potPin);
 speedM=map(speedM,0,1023,minSpeed,maxSpeed);

  if (speedM != speedC) {
   
    if(speedM>speedC){ 
        speedC+=accel;
        
    }
    else if(speedM<speedC){ 
        speedC-=accel;
        
    }
    
 }

 if(digitalRead(limitRPin)==HIGH){
    digitalWrite(pulPin,HIGH); // Output high
	delayMicroseconds(speedC); // set rotate speed
	digitalWrite(pulPin,LOW); // Output low
	delayMicroseconds(speedC); // set rotate speed
 }
 else{
 	speedC=minSpeed;
 }

}

}

void rotaryEncoder(){ //read the rotary encoder

newPos = myEnc.read();

if (newPos != position) {

if(newPos>position){ 
    
    digitalWrite(dirPin,leftDir);
   
}
else if(newPos<position ){ 
    
    digitalWrite(dirPin,rightDir);
   
}

stepM+=10;

position = newPos;

// Serial.println(stepM);

}

if(digitalRead(dirPin)==leftDir){ //move the motor to left side
if(stepM>0){

	   if(digitalRead(limitLPin)==HIGH){
	        
	        digitalWrite(pulPin,HIGH); // Output high
			delayMicroseconds(speedC); // set rotate speed
			digitalWrite(pulPin,LOW); // Output low
			delayMicroseconds(speedC); // set rotate speed

			if(speedC>maxSpeed) speedC=speedC-accel; 
			stepM=stepM-1;
	      	Serial.println(speedC);	
	    }
	    else{
	    	speedC=minSpeed;
	    	speedM=minSpeed;
	    	flagEncoder=0;
	    }

}
else{
speedC=minSpeed;
speedM=minSpeed;
flagEncoder=0;
}
}

else if(digitalRead(dirPin)==rightDir){ //move the motor to right side
if(stepM>0){

	   if(digitalRead(limitRPin)==HIGH){
	        digitalWrite(pulPin,HIGH); // Output high
			delayMicroseconds(speedC); // set rotate speed
			digitalWrite(pulPin,LOW); // Output low
			delayMicroseconds(speedC); // set rotate speed

			if(speedC>maxSpeed) speedC=speedC-accel; 
			stepM=stepM-1;
			Serial.println(speedC);	
	    }
	    else{
	    	speedC=minSpeed;
	    	speedM=minSpeed;
	    	flagEncoder=0;
	    }
 }
 else{
	speedC=minSpeed;
	speedM=minSpeed;
	flagEncoder=0;
}

}

}

void readButtons(){ //read the buttons

if(digitalRead(buttonLPin)==LOW){ //if left button has been pressed
digitalWrite(dirPin,leftDir);
speedC=minSpeed;

while(digitalRead(buttonLPin)==LOW){
    
    speedM=map(speedM,0,1023,minSpeed,maxSpeed);

 if(digitalRead(limitLPin)==HIGH){
    digitalWrite(pulPin,HIGH); // Output high
	delayMicroseconds(speedC); // set rotate speed
	digitalWrite(pulPin,LOW); // Output low
	delayMicroseconds(speedC); // set rotate speed

	if(speedC>speedM) speedC-=accel; 
 }
}

}

if(digitalRead(buttonRPin)==LOW){ //if right button has been pressed
digitalWrite(dirPin,rightDir);
speedC=minSpeed;

while(digitalRead(buttonRPin)==LOW){
    
    speedM=map(speedM,0,1023,minSpeed,maxSpeed);

 if(digitalRead(limitRPin)==HIGH){
    digitalWrite(pulPin,HIGH); // Output high
	delayMicroseconds(speedC); // set rotate speed
	digitalWrite(pulPin,LOW); // Output low
	delayMicroseconds(speedC); // set rotate speed

	if(speedC>speedM) speedC-=accel; 
 }
}

}
}

Hi,
Sorry but this your third thread, please use code tags.
To add code please click this link;

At this stage your code is just about unreadable.

Can I make a suggestion;
At the top of the void loop(), you read ALL your inputs and assign a variable to each input, then use that variable exclusively through your code.
In other words take a snapshot of your inputs and work on those, then take another snapshot etc etc.
You will find writing variable names easier then digital Read and analogRead repeatedly through your code.
Variable names like limitLState, limitRState, etc
Post your code in a NEW post please.

Thanks.. Tom... :smiley: :+1: :coffee: :australia: