So I have a motor with a basic incremental motor encoder and I am trying to use an interrupt to increment a encoder variable. I am trying to tell the motor to go from one position to another position with a while loop. I read the encoder and until the motor gets to the right position, I send a signal to the mosfet driver to spin the motor. I am confident my wiring is correct but my program has some kind of error, but I'm not sure where. I have no issues compiling or uploading, but as soon as the arduino starts running the program nothing happens (the motor does not turn on and spin to the setpoint). I think there is a really easy fix for this issue, but I have been looking at this simple code for hours and can't figure it out.
Code:
int enc_A = 3; //define variable for encoder
int sig = 7; // output signal to mosfet driver
int enc_count_rev = 4741; //encoder count per revolution
int setpoint1 = 40; // encoder position setpoint
volatile long encoderValue;
void setup() {
Serial.begin(9600);
pinMode(enc_A,INPUT_PULLUP);
pinMode(sig,OUTPUT);
digitalWrite(sig,LOW); // initialize the mosfet signal off
encoderValue = 0; //initialize encoder position to zero
attachInterrupt(digitalPinToInterrupt(enc_A), updateEncoder, RISING);
}
void loop() {
while (encoderValue < ( (setpoint1/360)*enc_count_rev) ){
Serial.println(encoderValue); reading the encoder value in the serial terminal
digitalWrite(sig,HIGH);
}
}
void updateEncoder(){ //function
//Increment value for each pulse from encoder
encoderValue++;
}