Hi...
I am trying to control a nema 17 stepper motor with TB6560 motor driver and one limit switch. Everything is working fine. but now i want to control the speed also with the rotary encoder KY040 i.e include encoder also.
The code that i was using till now is :-
// defines pins numbers
const int stepPin = 9;
const int dirPin = 10;
const int enPin = 11;
const int homeSwitchPin = 12;
void setup() {
Serial.begin(9600);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(homeSwitchPin , INPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
// Set Dir to Home switch
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
}
void loop() {
int homeSw = digitalRead( homeSwitchPin);
if( homeSw == HIGH && (digitalRead(dirPin) == LOW) ){
motorStep(1);
}
else if( homeSw == LOW && (digitalRead(dirPin) == LOW) ){
digitalWrite(dirPin,HIGH);
delay(2000);
}
if( (digitalRead(dirPin) == HIGH) ){
motorStep(3500);
digitalWrite(dirPin,LOW);
delay(2000);
}
}
void motorStep( int MAX){
for(int x = 0; x < MAX; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
This was my code with 1 limt switch. Now i want to add the code for the encoder also. so please someone provide me the code for encoder with this limit switch code