1

I have a problem with stopping motor at same spot when magnet pass.. I tried to add interrupt and to make code as clean as possible but no joy so far. Any suggestions? sw will add later, just ignore..

byte outPin = 12;        // the number of the output pin
byte inPin = 2;          // the number of the input pin

byte hallPin = 3; // hall sensor

byte sw; byte previous; int state = LOW; // the current state of the output pin int reading; // the current reading from the input pin

void setup() { pinMode(inPin, INPUT_PULLUP); pinMode(outPin, OUTPUT);

pinMode(hallPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(hallPin), stop, FALLING); //hall read low when magnet pass }

void loop() { sw=0;

reading = digitalRead(inPin); //read(LOW when press!) if (reading == HIGH) previous=0;

if (previous == 1 && sw == 0) state=LOW; else if (reading == HIGH) state=LOW; else state=HIGH;

digitalWrite(outPin, state); }

void stop() { if (sw == 0 && reading == LOW) { state=LOW; previous=1; } }

Milos
  • 11
  • 1

2 Answers2

3

You can stop a BDC faster if you short-circuit it. This is possible with an H-Bridge and the appropriate components.

It would be easier to use a L293D, which already provides this mode.

If you want to try it with this component, of course you can also build the h-bridge yourself, pay attention to the D on the component. With the D-version the protection diodes are already built in. You can power two motors from 4.5V to 36V with a maximum current of 600mA.

For up to 2A you can use a L298N.

Georg
  • 51
  • 5
-1

The motor can't stop instantly because it has inertia, plus the inertia of whatever stuff the motor is moving.

So if you want it to stop at a set position, you have to slow it down before it reaches the desired position, so it will come to a stop where you want. It's not possible to do that with only one sensor.

A complicated solution would be to use a position encoder, like a rotary encoder, to know the position at all times, so you could use a control algo like PID on the motor. This offers the most features, as you can then set the desired stop position in software, without needing to move the sensor.

If the stop position never moves then a simpler solution is to use multiple sensors. That's often how it's done with elevators: there is a sensor to accurately align the elevator with the floor it has to stop on, but there are also several others slightly before that on the track to tell the controller that it's time to start slowing down, then slow down to a crawl, then stop.

To keep it simple you can use just two sensors: when it goes past the first, slow down the motor to a crawl. When it activates the second sensor, since it is going slow, it will stop quickly and end up where you want.

bobflux
  • 99