2
\$\begingroup\$

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): Mosfet Schem

And using the VCNT2020 Sensor in the schematic here: VCNT2020 Schematic

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

EDIT: My current schematic - schematic

\$\endgroup\$
  • \$\begingroup\$ Anybody want to 'tap' Olin for a 'cartoon' statement? \$\endgroup\$ – laptop2d Jun 18 at 18:28
  • \$\begingroup\$ @laptop2d It's breadboard wiring diagrams that are cartoons. Other than that we've already have Olin's statement here. \$\endgroup\$ – Nick Alexeev Jun 20 at 1:36
  • \$\begingroup\$ @NickAlexeev Thanks, forgot about that \$\endgroup\$ – laptop2d Jun 20 at 3:07
  • \$\begingroup\$ My schematic is terrible, I didn’t use labels, the mosfets are different symbols which really annoyes me and the 5v regulator is attached to the mosfet circuits for some reason. I would fix this but I haven’t had that much time recently and I just plugged everything together in kicad. \$\endgroup\$ – Vosem Media Jun 20 at 6:28
1
\$\begingroup\$

Also, I would like you to take a look at my unfinished code and ask if you could suggest anything.

The not-equal comparison will not work well with a real analog signal because of noise

gear10us = analogRead(A1);
delayMicroseconds(10)
if (gear10us != analogRead(A1)) {
  // ...
}

Suppose you stop your motor, and your sensor is staring at the same thing. The signal from the IR sensor will be constant. But there also is going to be some noise riding on the signal. If you make two ADC readings you will get two integer numbers. These numbers will almost always be not-equal because of noise.
So, gear10us != analogRead(A1) could be always true, depending on the ADC resolution and the amount of noise.

Add an allowance for noise

gear10us = analogRead(A1);
delayMicroseconds(10)
gearNow = analogRead(A1);
if ((gearNow < gear10us - noiseAllowance) || (gear10us + noiseAllowance < gearNow)) {
  // ...
}

instead of simple not-equal comparison.

The while loop will never exit; it will loop infinitely

while(gearCounter < 15) {
  digitalWrite(5, HIGH);
}

You monitor the counter in the while loop. The counter would have to change while the loop is looping. But there's no way for that counter to change during the while loop, so the program will be stuck in the while loop.

Check the counter one time during the pass through the loop() function. Check for the dear (one time), then check the counter (one time without a while loop).

\$\endgroup\$
  • \$\begingroup\$ I edited your code into my question and will get the correct noiseAllowance when I hook the sensor to a nano and test it. \$\endgroup\$ – Vosem Media Jun 20 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.