Stepper motor + 2 push button's / help Code

Hi,

Im totaly new in the arduino world, and i can use some help to get started.

Parts:
Arduino UNO
DFrobot Stepper motor shield ( Stepper_Motor_Shield_For_Arduino__SKU_DRI0023_-DFRobot )
Stepper motor 4wire, bipolair.
2 Push buttons. ( 1 CW - 1 CCW )

Im building a adjustable headlight module for my car.

Sorry for my bad english, and i hope someone can help me with the code for this project.

grtz

Have you got any code working, such as reading the rotary encoder and printing something as a result of its output changing ? Or in a different program, moving a stepper motor a defined amount ?

Start with small steps towards what you want to do. Get both of the above programs working separately and you will be well on your way to reaching your goal but if you try to do something more ambitious you will get disheartened.

Incidentally, neither of the links you provided work for me.

links are corrected.

Well yeah i got this one working.

This sample code is for testing the 2 stepper motors
The rotation velocity can be adjusted by the code switch
Microcontroller: Arduino UNO
*/
int M1dirpin = 4;
int M1steppin = 5;
int M2dirpin = 7;
int M2steppin = 6;
void setup()
{
pinMode(M1dirpin,OUTPUT);
pinMode(M1steppin,OUTPUT);
pinMode(M2dirpin,OUTPUT);
pinMode(M2steppin,OUTPUT);
}
void loop()
{
int j;
delayMicroseconds(250);
digitalWrite(M1dirpin,LOW);
digitalWrite(M2dirpin,LOW);
for(j=0;j<=5000;j++){
digitalWrite(M1steppin,LOW);
digitalWrite(M2steppin,LOW);
delayMicroseconds(250);
digitalWrite(M1steppin,HIGH);
digitalWrite(M2steppin,HIGH);
delay(50);
}
}

And for the rotary encoder serial monitor i get this line:
ûþÿþ ºü­õ&âùeèóâ yaÁeè?æeNõ?&- .... ( etc )

And no counting like the sketch on the internet.

And for the rotary encoder serial monitor i get this line:
ûþÿþ ºü­õ&âùeèóâ yaÁeè?æeNõ?&- .... ( etc

Did you set audio rate on the monitor to match the sketch?

AWOL:
Did you set audio rate on the monitor to match the sketch?

Auto-correct "helping" there?

That should be baud rate.

Yeah, smart-ass Android auto-correct.
Thanks.

yeah 1 step closer :stuck_out_tongue:

Set it to 9600 like the sketch.
now i got numbers, jumping around 65450+- and not 1 2 3 4 5 6 7 8 9 10.

Maybe better to start with 2 button's ?

1 Up
1 Down

Maybe better to start with 2 button's ?

Sounds like a good plan.

Someone have a Sketch that i can use to modify?

Im using ( 2 ) momentary push button's

1 for Cw
1 for CCW

i don't know how to do this..
probably very easy for you guys.

grtz

Someone have a Sketch that i can use to modify?

There was one in reply #7.

Your problem breaks down into several parts -reading the state of the switch accurately (which requires that it be wired correctly), doing something when the state of the switch changes (not IS something), and making the steppers move correctly to match the effect of the switch press(es).

Which part(s) are working correctly? Which parts are not? Doing something with n switches is not really more difficult than doing something with one switch.

PaulS:

Someone have a Sketch that i can use to modify?

There was one in reply #7.

Your problem breaks down into several parts -reading the state of the switch accurately (which requires that it be wired correctly), doing something when the state of the switch changes (not IS something), and making the steppers move correctly to match the effect of the switch press(es).

Which part(s) are working correctly? Which parts are not? Doing something with n switches is not really more difficult than doing something with one switch.

The ( 2 ) 10kOhm resistors are on the way, so i can't realy test my code atm.

This is what i have atm. ( changed from a code i found )

// 4 wire bipolar stepper motor driver code for L293D which controlls the direction of the motor with two push buttons for each clockwise and counter clockwise direction. After completing the predefined steps in 'motorstep' waits for the new direction command will come from one of the push buttons

const int CW = 2;
const int CCW = 4;
const int ENA = 8;
const int ENB = 13;
const int wit = 9; 
const int blauw = 10; 
const int paars = 11; 
const int geel = 12;
int valA = 0; // counter clockwise button vallue
int stateA =0; // state of the counter clockwise button
int valB = 0;  // clockwise button value
int stateB = 0; //state of the clockwise button
int pause = 10; //delay between each step for slow rotation
int motorstep = 48; // number of steps when the buttons are once pressed

void setup(){
  pinMode(wit, OUTPUT);
  pinMode(blauw, OUTPUT);
  pinMode(paars, OUTPUT);
  pinMode(geel, OUTPUT);
  pinMode(CW, INPUT);
  pinMode(CCW, INPUT);
}

void loop(){
  valA = digitalRead(CW); //reads the CW button value and writes to 'valA'
  if(valA ==HIGH){
    stateA = 1- stateA;
    delay(1000); // to get rid of button bouncing (arduino reads the buttons really fast!!)
    stateB = 0;
    reverse(motorstep);  
  }
  
  valB = digitalRead(CCW); //reads the CCW button value and writes to 'valB'
  if (valB == HIGH){
    stateB = 1- stateB;
    delay(1000); // to get rid of button bouncing (arduino reads the buttons really fast!!)
    stateA = 0;
    forward(motorstep);    
  }
} // end loop

void reverse(int i){
  // Pin 8 Enable A Pin 13 Enable B on
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  
  while (1)   {
    digitalWrite(wit, 0);
    digitalWrite(blauw, 1);
    digitalWrite(paars, 1);
    digitalWrite(geel, 0);
    delay(pause);  
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(wit, 0);
    digitalWrite(blauw, 1);
    digitalWrite(paars, 0);
    digitalWrite(geel, 1);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(wit, 1);
    digitalWrite(blauw, 0);
    digitalWrite(paars, 0);
    digitalWrite(geel, 1);
    delay(pause);  
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(wit, 1);
    digitalWrite(blauw, 0);
    digitalWrite(paars, 1);
    digitalWrite(geel, 0);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;
  }

  // all outputs to stepper off
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);

}  // end reverse()



void forward(int i){

  // Pin 8 Enable A Pin 13 Enable B on
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);

  while (1){

    digitalWrite(wit, 1);
    digitalWrite(blauw, 0);
    digitalWrite(paars, 1);
    digitalWrite(geel, 0);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break; 

    digitalWrite(wit, 1);
    digitalWrite(blauw, 0);
    digitalWrite(paars, 0);
    digitalWrite(geel, 1);
    delay(pause);  
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(wit, 0);
    digitalWrite(blauw, 1);
    digitalWrite(paars, 0);
    digitalWrite(geel, 1);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(wit, 0);
    digitalWrite(blauw, 1);
    digitalWrite(paars, 1);
    digitalWrite(geel, 0);
    delay(pause);  
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;
  }

  // all outputs to stepper off
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);

}  // end forward()

The ( 2 ) 10kOhm resistors are on the way, so i can't realy test my code atm.

The built in internal pullup resistors have already arrived. You COULD test your code. Connect one leg to ground. Connect the other leg to the digital pin. Turn on the pullup resistor (using pinMode(pinNum, INPUT_PULLUP); or pinMode(pinNum, INPUT); and digitalWrite(pinNum, HIGH);).

This is what i have atm.

It does something. What?
You want it to do something. How does that differ from what it does?