#include <PinChangeInterrupt.h>
long actualPointPhd = 0;
byte sensorStatusPhdA = 0;
byte sensorStatusPhdB = 0;
int stepStatusPhd = 0;
long setpoint = 0;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP); // step
pinMode(5, INPUT_PULLUP); // dir
pinMode(10, OUTPUT); // left
pinMode(11, OUTPUT); // right
attachInterrupt(digitalPinToInterrupt(2), zmena, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), zmena, CHANGE);
attachPCINT(digitalPinToPCINT(4), zmena2, FALLING);
Serial.begin(9600);
}
void zmena(){
sensorStatusPhdA = digitalRead(2);
sensorStatusPhdB = digitalRead(3);
if(sensorStatusPhdA == 0 && sensorStatusPhdB == 0){
if(stepStatusPhd == 3){
actualPointPhd++;
}
if(stepStatusPhd == 1){
actualPointPhd--;
}
stepStatusPhd = 0;
}
if(sensorStatusPhdA == 1 && sensorStatusPhdB == 0){
if(stepStatusPhd == 0){
actualPointPhd++;
}
if(stepStatusPhd == 2){
actualPointPhd--;
}
stepStatusPhd = 1;
}
if(sensorStatusPhdA == 1 && sensorStatusPhdB == 1){
if(stepStatusPhd == 1){
actualPointPhd++;
}
if(stepStatusPhd == 3){
actualPointPhd--;
}
stepStatusPhd = 2;
}
if(sensorStatusPhdA == 0 && sensorStatusPhdB == 1){
if(stepStatusPhd == 2){
actualPointPhd++;
}
if(stepStatusPhd == 0){
actualPointPhd--;
}
stepStatusPhd = 3;
}
}
void zmena2() {
if(digitalRead(5) == HIGH) {
setpoint = setpoint + 1L;
}
if(digitalRead(5) == LOW) {
setpoint = setpoint - 1L;
}
}
void loop() {
if(actualPointPhd < setpoint) {
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
}
if(actualPointPhd > setpoint) {
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
if(actualPointPhd == setpoint) {
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
}