#include <PinChangeInterrupt.h>
long actualPointPhd = 0;
long actualPointFeed = 0;
int stepStatusFeed = 0;
byte sensorStatusPhdA = 0;
byte sensorStatusPhdB = 0;
int stepStatusPhd = 0;
byte sensorStatusFeedA = 0;
byte sensorStatusFeedB = 0;
unsigned long timers = 0;
unsigned long timerspast = 0;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(7, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), zmena, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), zmena, CHANGE);
attachPCINT(digitalPinToPCINT(4), zmena2, CHANGE);
attachPCINT(digitalPinToPCINT(7), zmena2, CHANGE);
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() {
sensorStatusFeedA = digitalRead(4);
sensorStatusFeedB = digitalRead(7);
if(sensorStatusFeedA == 0 && sensorStatusFeedB == 0){
if(stepStatusFeed == 3){
actualPointFeed++;
}
if(stepStatusFeed == 1){
actualPointFeed--;
}
stepStatusFeed = 0;
}
if(sensorStatusFeedA == 1 && sensorStatusFeedB == 0){
if(stepStatusFeed == 0){
actualPointFeed++;
}
if(stepStatusFeed == 2){
actualPointFeed--;
}
stepStatusFeed = 1;
}
if(sensorStatusFeedA == 1 && sensorStatusFeedB == 1){
if(stepStatusFeed == 1){
actualPointFeed++;
}
if(stepStatusFeed == 3){
actualPointFeed--;
}
stepStatusFeed = 2;
}
if(sensorStatusFeedA == 0 && sensorStatusFeedB == 1){
if(stepStatusFeed == 2){
actualPointFeed++;
}
if(stepStatusFeed == 0){
actualPointFeed--;
}
stepStatusFeed = 3;
}
}
void loop() {
timers = millis();
if(timers - timerspast >= 100) {
Serial.print(actualPointPhd);
Serial.print(" ");
Serial.println(actualPointFeed);
timerspast = timers;
}
}