Encoder alarm. Code a reset every X seconds

G'day I'm new to Arduino and have hit a problem with my first project.

I want my alarm to trigger if Encoder 1 (Enc1) stops while Encoder 2 (Enc2) is rotating.

Currently, if I reset and rotate Enc2, the alarm triggers (as it should). The problem is when Enc1 has been rotating, then stops, there is no alarm.

A possible workaround is to incorporate a reset command into the code. The reset will need to be triggered periodically (ever 2 seconds for instance).

Is this possible and, if so, how?



#include <Encoder.h>

Encoder myEnc1(5, 6);
Encoder myEnc2(3, 4);
//   avoid using pins with LEDs attached
const int IN1 =7;
const int IN2 = 8;
const int ENA = 10;
int pos;
void setup()
 {
  Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
  Serial.println("Basic Encoder Test:");
}

long oldPosition1 =-999,oldPosition2 =-999 ;
 long newPosition1,newPosition2;
void loop() 
{

digitalWrite(ENA,1);
  newPosition1 = myEnc1.read();

if (newPosition1 != oldPosition1) {
    oldPosition1 = newPosition1;
    Serial.println(newPosition1);
 }

  newPosition2 = myEnc2.read();

if (newPosition2 != oldPosition2) {
    oldPosition2 = newPosition2;
    Serial.println(newPosition2);
 }
 
if ((newPosition1!=0)  &&  (newPosition2==0)) 
  {

digitalWrite(IN1,1);
digitalWrite(IN2,0);

}

else if ((newPosition1==0) &&  (newPosition2==0))
{
digitalWrite(IN1,0);
digitalWrite(IN2,0);

  
}
else if ((newPosition1 != 0) && (newPosition2 != 0))
{
digitalWrite(IN1,0);
digitalWrite(IN2,0);

  
}

}

Well written code NEVER needs a reset to continue to operate properly.
There are various tricks to be defensive, only the very worst conditions could end up with a watchdog reset.

Rather than a full reset, perhaps I can reset the enc values to zero every 2 or three seconds?

You need to define "not rotating". Time is an issue, but time is not measured or considered in your code.

One approach is to specify a timeout interval. If there is no change in encoder value within that interval, the encoder is not rotating. Otherwise, it is rotating. Use a state variable to keep track.

See this excellent "blink without delay" tutorial to learn how to time events without blocking other actions.

If that's the case, then maybe they need to find a different choice of words or even a different forum.

Hi @muffincrumb

welcome to the arduino-forum.
well done posting code as a code-section in your first posting.

I'm pretty sure that the values that were printed to the serial monitor are always increasing if the rotery-encoder is turning.

The nature of the encoder-library is:
for each pulse that is detected increment / decrement the value.
It is very unlikely that after starting to rotate that the rotary-encoder will stop exactly at value 0 again.

You should give an overview about your project. You youself say

which means you are (yet) unexperienced. If you give an overview about your project much better suggestions and quite often much easier to realise approaches can be suggested.

best regards Stefan

Thanks for the input everyone. I'll get back to the drawing board with your suggestions.

Ted - the aim of the project is to create a cheap device for detecting overspool on a cable drum/winch ie if a feeder pulley stops but the drum continues rotating, an overspool is being created.

This is the problem exactly Stefan L38

I don't understand what an "overspool" is.

There is a feeder pulley. Is this feeder pulley driven by a motor?
I mean this this way: Does the feeder pulley have its own driving motor ?
= feeder pulley is rotating even if there is no cable?
or
does the cable moving over the feeder-pulley make the feeder-pulley rotate?

Does the cable-drum have its own motor? If the cable-drum has its own motor this would mean the cable is pullled by the rotating cable-drum.
In case the feeder-pulley stops does the feeder-pulley hold the cable in position so the cable can move not even one millimeter and if then the cable-drum continues to rotate the cable wil be tightened more and more on the cable-drum?

And is this called an "overspool"???
I'm not sure if this is a correct english word

Can you upload some pictures how your device is looking like?

Depending on what wheels

  • the feeder-pulley is driven by a motor
    or
  • the cable-drum is driven by a motor
    or
  • both

have a motor and are actively driven
different solutions to detect a stopped cable-drum can be used.

You should answer these questions so that other users become a clear picture how your device is functioning.

best regards Stefan

Only the drum rotation is motorised. The feeder pulley is connected to an encoder that reads length of cable pulled out. This encoder is hardwired to the winch and is irrelevant to the device I'm building because I'd like it to be a simple clip-on gadget. However, the pulley is relevant because it will stop turning during an over-spool (while the drum continues).

An over-spool is a messy situation that happens when the drum rotation overtakes the feed (out) and the cable becomes loosely wound and tangled.

If you've ever been fishing with a multiplier reel you will probably be familiar with this - colloquially named 'birds nest' or 'backlash'.

My concept is - pulley stops while drum is rotating = alarm

That all makes sense..

Just an Uno

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.