#define ENCA 2 // YELLOW
#define ENCB 3 // WHITE
int dir;
int dr= 12;
int dl=13;
int doel=500;
volatile int posi = 0; // specify posi as volatile
void setup() {
Serial.begin(9600);
pinMode(ENCA,INPUT);
pinMode(ENCB,INPUT);
pinMode(dr,OUTPUT);
pinMode(dl,OUTPUT);
attachInterrupt(digitalPinToInterrupt(ENCA),readEncoder,RISING);
}
void loop() {
// Read the position in an atomic block to avoid a potential
// misread if the interrupt coincides with this code running
// see: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
int pos = 0;
pos = posi;
dir = doel - pos;
if (dir>0)
{
digitalWrite(dr,HIGH);
Serial.println(rechts)
}
else
{
digitalWrite(dl,HIGH);
Serial.println(links)
}
}
Serial.println(pos);
}
void readEncoder(){
int b = digitalRead(ENCB);
if(b > 0){
posi++;
}
else{
posi--;
}
}