Pages: [1]   Go Down
Author Topic: watching pin state change and if stops change turns motor off  (Read 338 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all, any help would be great.

i am using

int pulsePin = 3;   //motor hall effect input

how can i do a small sketech to check this is changing state and when stops changing state turns a motor off ?



Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


so there is some pulsed signal on pin 3?
what is the frequency?

you should go IRQ based as holding state by a polled digitalRead() can miss pulses.

(code not tested)
Code:
#define THRESHOLD 500   // to be configured, now 500 millisec

boolean motorRunning = false;

volatile unsigned long lastTime = 0;

void IRQ()
{
  lastTime = millis();
}

void setup()
{
  attachInterrupt(1, IRQ, RISING);  // 1 = pin 3
}

void loop()
{
  if (millis() - lastTime > THRESHOLD)
  {
    motorRunning == false;
    // motorStop();
  }
  else
  {
    if (motorRunning == false)
    {
      motorRunning = true;
      // MotorStart();
    }
  }
}

Note for safety I propose to stop the motor over and over again if there is no pulse. This is just thinking safety.
Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks fast reply.

my pulse is 6 pulses per mm of travel ( an linerar actuator), about 9 mm travel  per sec
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


so ! 54 pulses per second ... faster than every 25 millisecond. => THRESHOLD = 25

give it a try.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: