I am a noob. Just figuring out arduino code. ( I have some very rusty c++ experience). I need to drive a motor, and count/display the total revs. Ultimately I need a android app (IOT I think?) That uses a single stop/start button, and direction button, but that is Phase II.
Ultimately I would like to exit the loop once the motor has moved a fixed number of revs, but right now I do not know what that count is. The Z pulse input is for troubleshooting, till I know the rev #. Ultimately I will need help reading the z pulse count, (With an if / then?) and then stopping motion. I have the motor code running OK now. Could more optimization.
The (brushless) motor outputs a once per rev "Z" index pulse. So this is NOT a quadrature encoder. I have found several "encoder" examples on the forum. I hesitate to upload my code yet, as it is very cluttered with commented out attempts. I also found the Jmotor library that supports single pulse encoders, but cannot get it to compile for my R4 UNO Wifi Arduino.
Attached is a real simple example for reading a quad encoder. Can someone PLEASE explain where the ISR is, (I "think" it is the attachInterupt) and where to insert the rotor value code? I do not care how slow it runs, so not sure I need to only output the value every 10 seconds. The motor only turns for about 2 seconds.
I have made a number of attempts, and I clearly do not know what I am doing. Included is the last post on the thread. I also do not understand how to "format the post" where it makes it more readable & highlights the code in gray...
__
// encoder pulse read
byte A = 2; // One quadrature pin
byte B = 3; // the other quadrature pin
volatile int Rotor = 0;
void setup() {
// set DIO pins
pinMode(A, INPUT);
pinMode(B, INPUT);
// Attach interrupt to pin A
attachInterrupt(0, UpdateRotation, FALLING);
// Use serial port to keep user informed of rotation
Serial.begin(57600);
}
void loop() {
}
void UpdateRotation() {
// This is the subroutine that runs every time pin 2
// switches from high to low.
if (digitalRead(B)) {
Rotor++;
} else {
Rotor--;
}
Serial.println(Rotor, DEC);
}
No. Exactly the opposite. The code to read the encoder state and update Rotor goes in the InteruptServiceRoutine. The code to print out the value of Rotor goes in loop(). The value should not be printed on every pass through loop, obviously, but every 10 seconds or so would be OK.
__