Using motor position inputs (via incremental encoder) to control output signal to mosfet driver to drive motor

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++;
}

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code. It's described in How to get the best out of this forum

volatile long encoderValue=0;

setpoint1 nowhere change, so setpoint1/360 is always 0, so while (encoderValue < ( (setpoint1/360)*enc_count_rev) ) will never execute

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.