Counting shaft pulses for powered rope reeler help!! cant make it work

Hello all,

I have been working on a project that requires code to count the number of turns a drum of rope makes as its pulled out, then when a switch is made by the user to select 'reel in' i need number of pulses which are counted to be minused from the those counted as the rope was pulled out.
when the count is 0 (ie the rope pulled out is equal to that pulled in) the led pin (output pin to go low) shutting off the drum winch in motor and stopping drive.

basically any amount of rope can be pulled off, with the code counting the number of turns of the drum as its pulled out then when the user sets the reel in switch and the motor pulls the rope back in, the code counts down once the number of pulses is the same as the pulses counted out the motor shuts off.

the motor drive is the same as LED pin for code, I have used the serial mointer just for de bugging.
my issue is the code is very jumpy and i have tried varoius ways of trying to get it to work as i want even using the 'goto' command (sorry!!)
The pulses are produced from a proximity switch on the shaft of the drum, this is switching and pulsing cleanly.

My code has become rather messy and i am sorry for my lack of skills and having to trouble you all, No doubt someone will help out and i shall learn!!

Thank you
Radjit

//Constants/// :
const int ProxSensor = 2; // The i/p from shaft prox sensor 1 pulse per rev
const int reelPin = 8; // the pin the reel in enable switich is connected to
const int ledPin = 13; // the pin that the LED is attached to will be motor drive
const int rstPin = 7; // the reset indicator LED pin ready to go

// Variables ///:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int reelin = 0;
int windback = 0;

void setup() {
// initialize the button pin as a input:
pinMode(ProxSensor, INPUT);
pinMode(reelPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(19200);
}

void loop() {
// read the pushbutton input pin:
reelin = digitalRead(reelPin);
if (reelin == HIGH){
goto windback;
}
else {

buttonState = digitalRead(ProxSensor);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println("on");
// Serial.print("n pulsos: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println("off");
}

// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

}

// *reel in minus code between here

windback:
digitalWrite(ledPin, HIGH);
buttonState = digitalRead(ProxSensor);
}
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, deincrement the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter--;
// Serial.println("on");
// Serial.print("n pulsos: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println("off");
}

// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

}

if (buttonPushCounter == 0) {

digitalWrite(rstPin, LOW);
digitalWrite(ledPin, LOW);
delay (1000);
}
}

No matter what type of rope you have, if the drag on the rope going out is different that the load coming in, the amount of rope you compute will be different. Do you have a "level wider" on the drum, so the lay of the rope is the same each time?

Paul

easier to read :wink:

//Constants/// :
const int  ProxSensor = 2;    // The i/p from shaft prox sensor 1 pulse per rev
const int reelPin = 8;      // the pin the reel in enable switich is connected to
const int ledPin = 13;       // the pin that the LED is attached to will be motor drive
const int rstPin = 7;       // the reset indicator LED pin ready to go

// Variables ///:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int reelin = 0;
int windback = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(ProxSensor, INPUT);
  pinMode(reelPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(19200);
}


void loop() {
  // read the pushbutton input pin:
  reelin = digitalRead(reelPin);
  if (reelin == HIGH){
  goto windback;
}
else {

  buttonState = digitalRead(ProxSensor);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      // Serial.println("on");
      // Serial.print("n pulsos:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      // Serial.println("off");
    }
 
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
 
   
  }


// *******reel in minus code between here******

windback:
  digitalWrite(ledPin, HIGH);
  buttonState = digitalRead(ProxSensor);
}
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, deincrement the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter--;
      // Serial.println("on");
      // Serial.print("n pulsos:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      // Serial.println("off");
    }
 
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
 
}
 
  if (buttonPushCounter == 0) {
   
    digitalWrite(rstPin, LOW);
    digitalWrite(ledPin, LOW);
    delay (1000);
  } 
}

The rope lays the same each time so there is no need to allow for a difference.
Basically For example if the code counts 200 pulses (revolutions of drum) out I need the it to count the same 200 Back on and to stop the drive.
My code seems to not count properly, sometimes the serial monitor not showing incremental values.. 1.2.3.4 etc but 1.2.1.2.3.4.3.2.
I can only assume I have brackets or loops wrong so the whole code is looping rather that just the string for counting out until the switch is changed over for the rest in to minus the count.
Or maybe I am looking at this wrongly

The "goto" will kill any logical programming you have.

Paul

I find it difficult to follow your code. From what I see you have three states: winding out, winding in and idle (done winding in). I rearranged your code a bit and pulled the proximity sensor code into it's own function, passing the value to add to the wind count. (I'm assuming buttonPushCounter is winds of rope, not pushes of a button)

It may not be correct but it's neater and may give you some ideas for another direction.

//Constants/// :
const int  ProxSensor = 2;    // The i/p from shaft prox sensor 1 pulse per rev
const int reelPin = 8;      // the pin the reel in enable switich is connected to
const int ledPin = 13;       // the pin that the LED is attached to will be motor drive
const int rstPin = 7;       // the reset indicator LED pin ready to go

// Variables ///:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int reelin = 0;
int windback = 0;

// states
const byte WIND_OUT = 1;
const byte WIND_IN = 2;
const byte IDLE_STATE = 3;

byte currentState = WIND_OUT;   // winding out at power on


void setup() {
  // initialize the button pin as a input:
  pinMode(ProxSensor, INPUT);
  pinMode(reelPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(19200);
}


void loop() {

  switch ( currentState )
  {
    case WIND_OUT:

      // check sensor, add to count
      checkProx(1);

      // read the pushbutton input pin:
      reelin = digitalRead(reelPin);
      if (reelin == HIGH) {
        currentState = WIND_IN;
      }

      break;

    case WIND_IN:

      // check sensor, decrement count
      checkProx(-1);

      // if done winding in, change state
      if ( buttonPushCounter <= 0 ) currentState = IDLE_STATE;

      break;

    case IDLE_STATE:

      digitalWrite(rstPin, LOW);
      digitalWrite(ledPin, LOW);
      delay (1000);

      break;

    default:
      break;
  }

}

void checkProx(int dir)
{
  buttonState = digitalRead(ProxSensor);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter += dir; /* increment or decrement based on direction */
      // Serial.println("on");
      // Serial.print("n pulsos:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      // Serial.println("off");
    }

    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;

  }

}

Do you have pullup or pulldown resistors on the input pins to keep the pins from "floating" and causing false readings when the button is not pressed?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Does your code at least count the pulses?

If not, then forget the code you have at the moment, and write some code JUST to count and store the pulse count.
You need to write your code in stages, and combine the working results at the end.

Tom... :slight_smile:

I presume this line of code is what detects the rotation of the drum

buttonState = digitalRead(ProxSensor);

Why are you confusing us (and yourself) by calling the variable buttonState and elsewhere having a variable called buttonPushCounter to count the rotations?

This is especially confusing because you you also seem to have a genuine push button.

Using meaningful variable names makes programs much easier to develop and debug and makes the code self explanatory so that many fewer comments are needed.

As far as I can see if you replace this

    if (reelin == HIGH){
        goto windback;
    }
    else {

with the following there will be no need for the goto

    if (reelin == HIGH){
        digitalWrite(ledPin, HIGH);
        buttonState = digitalRead(ProxSensor);
    }
    else {

...R