Hier ein Beispiel, wie man mit einem Handrad ein Schrittmotor steuern kann.
Aufbau

Sketch
int A = 8; // Pin
int B = 13; // Pin
int valB = 0; // Wert vom Pin
int valA = 0; // Wert vom Pin
boolean lockA = false;
boolean lockB = false;
int count = 0;
int dir = 0; // Drehrichtung
unsigned long oneStep = 0; // ein Schritt
void setup() {
Serial.begin(9600);
pinMode(B, INPUT_PULLUP);
pinMode(A, INPUT_PULLUP);
pinMode(6, OUTPUT); // Enable
pinMode(5, OUTPUT); // Puls
pinMode(4, OUTPUT); // Direction
digitalWrite(6, LOW);
Serial.println("Bereit...");
}
void loop() {
forward();
backward();
setStep();
}
void forward(){
valA = digitalRead(A); // read the input pin
if(valA == HIGH) {
lockA = true;
if(dir == 0) {
// Serial.println("dir = 1");
dir = 1;
}
} else {
lockA = false;
}
}
void backward(){
valB = digitalRead(B); // read the input pin
if(valB == HIGH) {
lockB = true;
if(dir == 0) {
dir = 2;
// Serial.println("dir = 2");
}
} else {
lockB = false;
}
}
void setStep(){
if(lockA && lockB) {
if (oneStep == 0) {
if(dir == 1) {
// Serial.print("HIGH A ");
Serial.println(++count);
stepper(1, "forward");
}
if(dir == 2) {
// Serial.println("HIGH B");
Serial.println(--count);
stepper(1, "backward");
}
}
oneStep = 1;
}
if (!lockA && !lockB) {
oneStep = 0;
dir = 0;
}
}
void stepper(int steps, String drehRichtung) {
if(drehRichtung.equals("forward")) {
digitalWrite(4, HIGH);
}
if(drehRichtung.equals("backward")) {
digitalWrite(4, LOW);
}
for(int i = 0; i < steps; i++) {
digitalWrite(5,HIGH);
delayMicroseconds(500);
digitalWrite(5,LOW);
delayMicroseconds(500);
}
}Code-Sprache: JavaScript (javascript)

















