Hallo,

Ik ben momenteel bezig met het maken van een stappenmotordriver met een arduino. Ik heb de code deels werkend gekregen. Het probleem onstaat nadat ik de direction pin hoog maak, zet er gewoon 5 v van de arduino op, dan word de volgende pin niet hoog. Dit is mijn code:

/// constants won't change. They're used here to
// set pin numbers:
const int xdirPin = 12; // the number of the pushbutton pin
const int xstepPin = 13;
const int m1 = 2;
const int m2 = 3;
const int m3 = 4;
const int m4 = 5;// the number of the LED pin

// variables will change:
int xstepState = 0;
int xdirState = 0;
int count1 = 1;
int check1 = 0;
// variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(xdirPin, INPUT);
pinMode(xstepPin, INPUT);
}

void loop(){
delay(100);
// read the state of the pushbutton value:
xdirState = digitalRead(xdirPin);
xstepState = digitalRead(xstepPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (xstepState != check1) {
// turn LED on:

if(xstepState == HIGH && xdirState == LOW){
++count1;
check1 = 0;
}
if(xstepState == HIGH && xdirState == HIGH){
--count1;
check1 = 0;
}
if(count1 == 5){
count1 = 1;
}
if(count1 == 0){
count1 = 4;
}
}

check1=xstepState;

switch(count1){

case 1:
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
break;

case 2:
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
break;

case 3:
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
break;

case 4:
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
break;
}
}

Ziet iemand het probleem in mijn code? Ik weet dat er nog andere manieren van aansturen zijn, maar dit is de optie die ik wil gebruiken. Maar wat is het probleem met de direction pin?

Alvast bedankt,

BJ

Wat voor type driver gebruik je?

Stuur tips aan via de arduino.

ik weet niet wat voor effecten het heeft maar

int xstapstate == HIGH
(hoewel alles dat niet 0 is als HIGH gezien wordt)
dat is gewoon niet netjes, zet eerst de helft van je code gewoon eens om naar booleans als je ze zo gebruikt.

en waarom ga je eerst m2, m3 declareren, om vervolgens bij je aansturing gewoon weer 2 en 3 te gebruiken.
daar wordt het allemaal ook niet overzichtelijker van

ps tip is altijd om ff een een serial.print() op te zetten, om wat data naar je pc te sturen om te zien welke stappen je programma maakt