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;
}
}
}
}