My plan is to start a motor, then stop the motor when the gear teeth gets to 15 (out of 16) I will be able to tell at what position the gear is by placing an IR reflective sensor under the teeth. The sensor is a VCNT2020. I am controlling the motor with three MOSFETs in the schematic here (Leaving out M1):
And using the VCNT2020 Sensor in the schematic here:
And finally using an ATtiny85 as the microcontroller. I was originally going to use an arduino, but there is not that much IO so I thought it wouldn't be worth it.
My question is, will the motor be able to stop in time, should I begin to stop it on an earlier gear or will I need to do some kind of braking where I use a fourth FET to short the motor terminals. Also, I would like you to take a look at my unfinished code and ask if you could suggest anything. The reading of the sensor pin and the digital writing to the Mosfet need to be very fast otherwise the motor could do another few rotations and the gear go another tooth, going past the 15th one. I don't mind if it is exactly 15, but it needs to be 16 or below, and 13 or above.
Here is the motor, I couldn't find the datasheet but it runs on a maximum of 30 amps and 12.6v 3s lipo. The maximum rpm is 34,000
Code:
int gearcounter = 0;
int noiseAllowance = 10 // Ill change this when I 'calibrate' the design on a breadboard.
void setup() {
Serial.begin(9600);
pinMode(A1, INPUT); // Ir sensor pin
pinMode(5, OUTPUT); // Mosfet gate pin
}
void loop() {
gear10us = analogRead(A1);
delayMicroseconds(10)
gearNow = analogRead(A1);
if ((gearNow < gear10us - noiseAllowance) || (gear10us + noiseAllowance < gearNow)) {
gearcounter += 1;
}
}
if (gearcounter == 16) {//Maximum gear is 16, I just want to stop it one before the maximum
gearcounter = 1;
}
while(gearcounter < 15){
digitalWrite(5, HIGH);
}
}
Thanks