2 potentiometer 2 stepper motor and 2 switch

hello , i was doing a project and did a bit of copy-paste . i wanted to control 2 stepper motor with 2 different potentiometer and 2 switches for clockwise and counter clockwise. the main job of potentiometer is to slow down and ramp up the speed ,but i guess i did some mistake and the potentiometer aren't functioning..any help would be great !!!!


const int desiredValue = 15;

const int dir = 1;

int Pin1 = 10;//IN1  

int Pin2 = 11;//IN2  

int Pin3 = 12;//IN3  

int Pin4 = 13;//IN4

int Pin5 = 5;//IN1  

int Pin6 = 6;//IN2  

int Pin7 = 7;//IN3  

int Pin8 = 8;//IN4  

int switchCW  =2;

int switchCCW =3;

int potPin1 = A0;

int potPin2 = A1;

int vcc2 = 4;

int vcc3 = 9;

 

int pole1[] ={0,0,0,0, 0,1,1,1, 0};

int pole2[] ={0,0,0,1, 1,1,0,0, 0};

int pole3[] ={0,1,1,1, 0,0,0,0, 0};

int pole4[] ={1,1,0,0, 0,0,0,1, 0};

int pole5[] ={0,0,0,0, 0,1,1,1, 0};

int pole6[] ={0,0,0,1, 1,1,0,0, 0};

int pole7[] ={0,1,1,1, 0,0,0,0, 0};

int pole8[ ] ={1,1,0,0, 0,0,0,1, 0};

int poleStep = 0;

int  dirStatus = 3;

int calcDelayfromTime(int t);

void driveStepper(int c);

int N = calcDelayfromTime(desiredValue);

void setup()

{

 pinMode(Pin1, OUTPUT);

 pinMode(Pin2, OUTPUT);  

 pinMode(Pin3, OUTPUT);  

 pinMode(Pin4, OUTPUT);

 pinMode(vcc2, OUTPUT);

 digitalWrite(vcc2, HIGH);

 pinMode(Pin5, OUTPUT);

 pinMode(Pin6, OUTPUT);  

 pinMode(Pin7, OUTPUT);  

 pinMode(Pin8, OUTPUT);

 pinMode(vcc3, OUTPUT);

 digitalWrite(vcc3, HIGH);

 Serial.begin(9600);

 Serial.println("Robojax Stepper Control");

 Serial.print("Max time per rev set: ");

 Serial.print(desiredValue);

 Serial.print(" loop delay: ");

 Serial.println(N);

 pinMode(switchCW,INPUT_PULLUP);

 pinMode(switchCCW,INPUT_PULLUP);

  delay(4000);

}// setup  

  

 void loop()

{

  if(digitalRead(switchCCW) == LOW)

  {

    dirStatus =1;

  }else if(digitalRead(switchCW) == LOW)

  {

   dirStatus  = 2;  

  }else

  {

    dirStatus =3;

  }

 if(dirStatus ==1){

   poleStep++;

    driveStepper(poleStep);    

 }else if(dirStatus ==2){

   poleStep--;

    driveStepper(poleStep);    

 }else{

  driveStepper(8);  

 }

 if(poleStep>7){

   poleStep=0;

 }

 if(poleStep<0){

   poleStep=7;

 }

 int potValue1 =analogRead(potPin1);

 int speed1 = map(potValue1, 0 , 1023, 1,N);

 

 int potValue2 =analogRead(potPin2);

 int speed2 = map(potValue2, 0 , 1023, 1,N);

 delay(100);

}//loop

int calcDelayfromTime(int t){

  return ((t-5)/4)+1;

  }

void driveStepper(int c)

{

     digitalWrite(Pin1, pole1[c]);  

     digitalWrite(Pin2, pole2[c]);

     digitalWrite(Pin3, pole3[c]);

     digitalWrite(Pin4, pole4[c]);

     digitalWrite(Pin5, pole5[c]);  

     digitalWrite(Pin6, pole6[c]);

     digitalWrite(Pin7, pole7[c]);

     digitalWrite(Pin8, pole8[c]);  

}

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

why
.
.
.
does
.
.
.
.
.
your
.
.
code
.
.
need to have some many empty lines??

const int desiredValue = 15;
const int dir = 1;

int Pin1 = 10;//IN1
int Pin2 = 11;//IN2
int Pin3 = 12;//IN3
int Pin4 = 13;//IN4

int Pin5 = 5;//IN1
int Pin6 = 6;//IN2
int Pin7 = 7;//IN3
int Pin8 = 8;//IN4

int switchCW  = 2;
int switchCCW = 3;

int potPin1 = A0;
int potPin2 = A1;

int vcc2 = 4;
int vcc3 = 9;

int pole1[] = {0, 0, 0, 0, 0, 1, 1, 1, 0};
int pole2[] = {0, 0, 0, 1, 1, 1, 0, 0, 0};
int pole3[] = {0, 1, 1, 1, 0, 0, 0, 0, 0};
int pole4[] = {1, 1, 0, 0, 0, 0, 0, 1, 0};

int pole5[] = {0, 0, 0, 0, 0, 1, 1, 1, 0};
int pole6[] = {0, 0, 0, 1, 1, 1, 0, 0, 0};
int pole7[] = {0, 1, 1, 1, 0, 0, 0, 0, 0};
int pole8[] = {1, 1, 0, 0, 0, 0, 0, 1, 0};

int poleStep = 0;
int  dirStatus = 3;
int calcDelayfromTime(int t);

void driveStepper(int c);

int N = calcDelayfromTime(desiredValue);

void setup() {
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(Pin4, OUTPUT);

  pinMode(vcc2, OUTPUT);
  digitalWrite(vcc2, HIGH);

  pinMode(Pin5, OUTPUT);
  pinMode(Pin6, OUTPUT);
  pinMode(Pin7, OUTPUT);
  pinMode(Pin8, OUTPUT);

  pinMode(vcc3, OUTPUT);
  digitalWrite(vcc3, HIGH);

  Serial.begin(9600);
  Serial.println("Robojax Stepper Control");
  Serial.print("Max time per rev set: ");
  Serial.print(desiredValue);
  Serial.print(" loop delay: ");
  Serial.println(N);

  pinMode(switchCW, INPUT_PULLUP);
  pinMode(switchCCW, INPUT_PULLUP);

  delay(4000);
}// setup


void loop(){

  if (digitalRead(switchCCW) == LOW){
    dirStatus = 1;
  } else if (digitalRead(switchCW) == LOW)  {
    dirStatus  = 2;
  } 
  else{
    dirStatus = 3;
  }

  if (dirStatus == 1) {
    poleStep++;
    driveStepper(poleStep);
  } 
  else if (dirStatus == 2) {
    poleStep--;
    driveStepper(poleStep);
  } 
  else {
    driveStepper(8);
  }

  if (poleStep > 7) {
    poleStep = 0;
  }

  if (poleStep < 0) {
    poleStep = 7;
  }

  int potValue1 = analogRead(potPin1);
  int speed1 = map(potValue1, 0 , 1023, 1, N);

  int potValue2 = analogRead(potPin2);
  int speed2 = map(potValue2, 0 , 1023, 1, N);

  delay(100);

}//loop

int calcDelayfromTime(int t) {
  return ((t - 5) / 4) + 1;
}

void driveStepper(int c){
  digitalWrite(Pin1, pole1[c]);
  digitalWrite(Pin2, pole2[c]);
  digitalWrite(Pin3, pole3[c]);
  digitalWrite(Pin4, pole4[c]);
  
  digitalWrite(Pin5, pole5[c]);
  digitalWrite(Pin6, pole6[c]);
  digitalWrite(Pin7, pole7[c]);
  digitalWrite(Pin8, pole8[c]);
}

What is still missing is a detailed description of what behaviour you watch?

Be the change you want to see in the world
best regards Stefan

Must you work without a stepper library?

Hello Stefan , i want to control speed of motors via potentiometer (different potentiometer for different motor ) and the one of the push button is to turn the stepper motors(both) to clockwise and other push button in counterclockwise direction. Sorry for my insufficient English and also for the spacing XD. HOPE you understood what I meant to say , please do let me know if you didn't understood

Hello groundFungus , It's not compulsory to not to use the library but i don't prefer library since it makes it a bit more confusing and if you want the detailed description of what I want as a output please refer to above reply for Stefan . Sorry for my limited English .

I have understood what you have written.
But there is missing the most important part:

What different behaviour than the wanted do you see if you run your code?
You have to post a detailed description of what your stepper-motors do if you run your last posted code

Be the change you want to see in the world
best regards Stefan

Hi,
Can I suggest you start your code from scratch and first get the stepper rotating, then reversing, then controlling its speed.
Work in stages for ONE stepper, get it working then attempt adding the other.

Cut and pasting is not a way to learn to control your steppers.
Can you please tell us your electronics, programming, arduino, hardware experience?

Can you please post a circuit diagram?
Have you been able to at lease make your steppers move?

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

Here is a project with Arduino UNO, a stepper motor, and 2 potentiometers.

I hope this will help you to build up your application one at a time.

in my code , the potentiometer arent working as intended i.e they should reduce the speed of the motors independently. 1st potentiometer should change the speed of 1st motor and 2nd potentiometer should change the speed of 2nd motor , and one push button should turn the motors (both) clockwise and other push button should turn the motors anticlockwise .

my problem with this code is ..,,its not reading the potentiometer values ..maybe i messed up with map() function .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.