Good Day
I am having a problem that when the encoder has reached its step limit (100 “steps” in this case") the motor is still receiving power.
I am serial printing the step count so i can see it increase and go past its setpoint and the motor will keep turning.
The intent is that the motor would stop turning when the step setpoint is reached.
I am not sure why the the if (master_count < 100)command is not being followed.
This is my first time using interrupts.
The push button has no relevance.
Using an ELEGOO UNO R3
Motor and encoder 6V 100:1 155RPM Micro Metal Gearmotor w/ Encoder - RobotShop
Driver Pololu Dual DC Motor Driver 1A, 4.5V-13.5V- TB6612FNG - RobotShop
I hope I have provided enough detail.
Thank You
Mike
[code]
int AI1 = 10;//motor driver
int AI2 = 9;//motor driver
int stdby = //8;motor driver
int PWM = //11;motor driver
#define CHA 2//motor encoder
#define CHB 3//motor encoder
int cycleButt = A3;//momentary cycle button input
int cycleButState;
volatile int master_count = 0; // universal count
volatile byte INTFLAG1 = 0; // interrupt status flag
void setup() {
// put your setup code here, to run once:
pinMode(CHA, INPUT);
pinMode(CHB, INPUT);
pinMode(cycleButt, INPUT_PULLUP);
Serial.begin(57600);
pinMode (AI1, OUTPUT);
pinMode(AI2, OUTPUT);
pinMode(stdby, OUTPUT);
pinMode(PWM, OUTPUT);
Serial.println(master_count);
Serial.println("SETUP");
attachInterrupt(0, flag, RISING);
digitalWrite(stdby, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
cycleButState = digitalRead(cycleButt);
//Serial.println(cycleButState);
if (master_count < 100)
{
motorforward();
}
else { Serial.println("DONE");}
if (cycleButState == LOW) {
Serial.println(cycleButState);
}
if (INTFLAG1) {
Serial.println(master_count);
delay(1);
INTFLAG1 = 0; // clear flag
} // end if
}
void flag() {
INTFLAG1 = 1;
// add 1 to count for CW
if (digitalRead(CHA) && !digitalRead(CHB)) {
master_count++ ;
}
// subtract 1 from count for CCW
if (digitalRead(CHA) && digitalRead(CHB)) {
master_count-- ;
}
}
void motorforward()
{ digitalWrite(AI1, HIGH);
digitalWrite(AI2, LOW);
analogWrite(PWM, 150);
}
void motorreverse()
{ digitalWrite(AI2, HIGH);
digitalWrite(AI1, LOW);
analogWrite(PWM, 150);
}
[/code]