Hi,
I am trying to make a motor protector using arduino nano.
It’s a 24vDC - reduction geared motor, which is fitted with a "Sensor Disc" (image attached) on one end of its shaft and the other end is connected to the load through a fly wheel.
The rotation of the disc is sensed by this Sensor (image attached) and fed to arduino nano.
The program is expected to do is:
-
The 'fwdPin' is set to HIGH to run the motor in forward direction.
-
If the motor is over loaded and got stuck (the "Sensor Disc" stops rotating, resulting in no change in 'SensePinState'), then wait for 500 milli seconds and cut the power supply to the motor by setting 'fwdPin' LOW.
-
Wait for 1000 milli seconds (to give time to the fly wheel to come to rest) then reverse the motor direction by setting the 'revPin' HIGH.
(The polarity of the supply to the motor is reversed using a relay, to reverse the motor running direction)
- If the motor is rotating continuously, then the 'revPin' is let to stay HIGH and the 'Toggle count' is reset to zero,
else, wait for 500 milli seconds and then cut the power supply to the motor by setting 'revPin' LOW.
-
This toggling process is repeated indefinitely until the 'Toggle count' remains zero.
-
If the motor gets stuck after 1st toggle (toggle count is incremented to 1), the motor is reversed,
If the motor gets stuck after 2nd toggle (toggle count is incremented to 2), the motor is reversed,
If the motor gets stuck after 3rd toggle (toggle count is incremented to 3), the motor is stopped.
Problem in the program:
- If I set the 'sensetimediff' to 500 milli seconds and the 'ontimediff' to 1000 milli seconds,
the 'sensetimediff' overrules the 'ontimediff' and the 'ontimediff' is alo becomes 500 milli seconds, which can be seen in the serial monitor.
Kindly help me.
const int SensePin = 2;
const int fwdPin = 3;
const int revPin = 4;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long diff = 0;
unsigned long prev_ON_millis = 0;
unsigned long curr_ON_millis = 0;
unsigned long ON_diff = 0;
int toggleCount = 0;
const int sensetimediff = 500;
const int ontimediff = 1000;
void setup()
{
Serial.begin(9600);
pinMode(fwdPin, OUTPUT);
digitalWrite(fwdPin, HIGH);
pinMode(revPin, OUTPUT);
digitalWrite(revPin, LOW);
pinMode(SensePin, INPUT_PULLUP);
}
void loop()
{
static boolean last_SensePinState = 0;
boolean SensePinState = digitalRead(SensePin);
currentMillis = millis();
diff = currentMillis - previousMillis;
if (SensePinState != last_SensePinState && diff < sensetimediff)
{
Serial.print("TC=");
Serial.print(toggleCount);
Serial.print(" ");
Serial.print("DIFF=");
Serial.print(diff);
Serial.print(" ");
Serial.print("ON-DIFF=");
Serial.print(ON_diff);
Serial.println(" RUNNING ");
previousMillis = currentMillis;
prev_ON_millis = curr_ON_millis;
toggleCount = 0;
}
else if (SensePinState == last_SensePinState && diff > sensetimediff)
{
switch (toggleCount)
{
case 0:
Serial.print("TC=");
Serial.print(toggleCount);
Serial.print(" ");
Serial.print("DIFF=");
Serial.print(diff);
Serial.print(" ");
Serial.print("ON-DIFF=");
Serial.print(ON_diff);
Serial.println(" RUNNING ");
digitalWrite(fwdPin, LOW);
curr_ON_millis = millis();
ON_diff = curr_ON_millis - prev_ON_millis;
if (ON_diff >= ontimediff)
{
digitalWrite(revPin, HIGH);
}
previousMillis = currentMillis;
prev_ON_millis = curr_ON_millis;
toggleCount++;
break;
case 1:
Serial.print("TC=");
Serial.print(toggleCount);
Serial.print(" ");
Serial.print("DIFF=");
Serial.print(diff);
Serial.print(" ");
Serial.print("ON-DIFF=");
Serial.print(ON_diff);
Serial.println(" RUNNING ");
digitalWrite(revPin, LOW);
curr_ON_millis = millis();
ON_diff = curr_ON_millis - prev_ON_millis;
if (ON_diff >= ontimediff)
{
digitalWrite(fwdPin, HIGH);
}
previousMillis = currentMillis;
prev_ON_millis = curr_ON_millis;
toggleCount++;
break;
case 2:
Serial.print("TC=");
Serial.print(toggleCount);
Serial.print(" ");
Serial.print("DIFF=");
Serial.print(diff);
Serial.print(" ");
Serial.print("ON-DIFF=");
Serial.print(ON_diff);
Serial.println(" RUNNING ");
digitalWrite(fwdPin, LOW);
curr_ON_millis = millis();
ON_diff = curr_ON_millis - prev_ON_millis;
if (ON_diff >= ontimediff)
{
digitalWrite(revPin, HIGH);
}
previousMillis = currentMillis;
prev_ON_millis = curr_ON_millis;
toggleCount++;
break;
case 3:
Serial.print("TC=");
Serial.print(toggleCount);
Serial.print(" ");
Serial.print("DIFF=");
Serial.print(diff);
Serial.print(" ");
Serial.print("ON-DIFF=");
Serial.print(ON_diff);
Serial.println(" RUNNING ");
digitalWrite(revPin, LOW);
previousMillis = currentMillis;
prev_ON_millis = curr_ON_millis;
toggleCount++;
break;
}
}
if (toggleCount > 3)
{
Serial.print("TC=");
Serial.print(toggleCount);
Serial.print(" ");
Serial.print("DIFF=");
Serial.print(diff);
Serial.print(" ");
Serial.print("ON-DIFF=");
Serial.print(ON_diff);
Serial.println(" STOPPED ");
digitalWrite(fwdPin, LOW);
digitalWrite(revPin, LOW);
}
last_SensePinState = SensePinState;
}