Go Down

Topic: My counting method to control a servo does not loop; not sure how to fix (Read 486 times) previous topic - next topic

UKHeliBob

How could I use a push button to control the counter here?
Do you want to count the number of button presses, change the count using button presses or start counting when the button becomes pressed ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


Preezus

Do you want to count the number of button presses, change the count using button presses or start counting when the button becomes pressed ?
I would like to count the number of button presses.

UKHeliBob

I would like to count the number of button presses.
What do you want to do with the count of button presses and how do you want to change from counting the button presses to blinking the LED ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Preezus

What do you want to do with the count of button presses and how do you want to change from counting the button presses to blinking the LED ?
After a certain amount of presses (5 presses), I want to activate the servo arm 90 degrees. and then after another certain amount of presses (5 more presses) activate the arm back to zero degrees. I want this to keep going until power is disconnected

Eventually, I want to switch out the button for a sensor that will count parts. That is why I think I need to count the parts. It is just easier for me to connect and test the program with a button first, and then add the sensor once I know the program is working as intended.

PaulS

Quote
It is just easier for me to connect and test the program with a button first
So, look at the state change detection example. It is all about counting the number of times the button you sew on is pressed.
The art of getting good answers lies in asking good questions.

Preezus

So, look at the state change detection example. It is all about counting the number of times the button you sew on is pressed.
OK, I have now combined the state change detection example, thanks to Paul, with my previous code.
The servo motor is only activated after every fourth button push (which is great), however I must have an error because the arm is in a constant sweeping motion back and fourth instead of just moving one position. Any ideas how to fix this?

Code: [Select]
#include <Servo.h>;

Servo myservo;

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
const int servoPin = 9;

// Variables will change:
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 pos = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  pinMode(servoPin, OUTPUT);
  myservo.attach(9);
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // 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 went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;



  if (buttonPushCounter % 4 == 0) {

    myservo.write(pos * 90);
    if (pos == 0)
    {
      pos = 1;
    }
    else
    {
      pos = 0;
    }

  }

}

PaulS

Quote
Any ideas how to fix this?
First thing I'd do:
Code: [Select]
  if (buttonPushCounter == 4)
  {
    myservo.write(pos * 90);
    if (pos == 0)
    {
      pos = 1;
    }
    else
    {
      pos = 0;
    }

    buttonPushCounter = 0;
  }


Don't let buttonPushCounter get above 4.

Actually, that is the second thing I'd do. The first thing would be to use INPUT_PULLUP as the mode, and wire the switch with one leg to ground and the other leg to the digital pin. Of course, that requires switching so that LOW means pressed and HIGH means released, but that is easy to do.
The art of getting good answers lies in asking good questions.

AWOL

Code: [Select]
myservo.write(pos * 90);
    if (pos == 0)
    {
      pos = 1;
    }
    else
    {
      pos = 0;
    }

OK, my joke went too far

Code: [Select]
myservo.write(pos);
pos = (pos == 0) ? 90 : 0;

Preezus

LOL... there are plenty of ways to skin a cat, my friend. Thanks to both of you, AWOL & Paul.

Now it's time for me to replace the button with a digital fiber sensor and hopefully not completely destroy any sort of progress we've made.

Go Up