Making a servo arm move after a counter hits 9

Could anyone help me I need to make a system that rotates a servo motor after a counter hits 9 i've made the counter, it just uses a button that can make it go up one number and I just need help making the servo motor rotate after a timer hit's 9, or I could do this by making the arm rotate after 9 button presses could someone tell me how to do this?

Heres the video I used to make the counter: Arduino 7-Segment counter 0-9 with push button up and down - YouTube

Also heres the code for the counter:

const int a = 8; //For displaying segment "a"
const int b = 9; //For displaying segment "b"
const int c = 4; //For displaying segment "c"
const int d = 5; //For displaying segment "d"
const int e = 6; //For displaying segment "e"
const int f = 2; //For displaying segment "f"
const int g = 3; //For displaying segment "g"

bool bPress = false;
const int IncbuttonPin = 10;
const int DecbuttonPin = 11;

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses

int IncbuttonState = 0; // current state of the button
int lastIncbuttonState = 0; // previous state of the button

int DecbuttonState = 0; // current state of the button
int lastDecbuttonState = 0; // previous state of the button

void setup() {
// put your setup code here, to run once:
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G

pinMode( IncbuttonPin , INPUT_PULLUP );
pinMode( DecbuttonPin , INPUT_PULLUP );
Serial.begin(9600);

displayDigit(buttonPushCounter);

}

void loop() {

IncbuttonState = digitalRead(IncbuttonPin);
DecbuttonState = digitalRead(DecbuttonPin);

checkIncButtonPress();
checkDecButtonPress();

if( bPress ){
bPress = false;
turnOff();
displayDigit(buttonPushCounter);
}

/*
for(int i=0;i<10;i++)
{
displayDigit(i);
delay(1000);
turnOff();
}
*/
}

void checkIncButtonPress()
{
// compare the IncbuttonState to its previous state
if (IncbuttonState != lastIncbuttonState) {
// if the state has changed, increment the counter
if (IncbuttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter++;
if( buttonPushCounter > 9) buttonPushCounter =0 ;
Serial.println("on");

} 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
lastIncbuttonState = IncbuttonState;

}
void checkDecButtonPress()
{
// compare the IncbuttonState to its previous state
if (DecbuttonState != lastDecbuttonState) {
// if the state has changed, increment the counter
if (DecbuttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter--;
if( buttonPushCounter < 0) buttonPushCounter =9 ;
Serial.println("on");

} 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
lastDecbuttonState = DecbuttonState;

}

void displayDigit(int digit)
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);

//Conditions for displaying segment b
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);

//Conditions for displaying segment c
if(digit !=2)
digitalWrite(c,HIGH);

//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);

//Conditions for displaying segment e
if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);

//Conditions for displaying segment f
if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);

}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

Maybe put the line in after you increment the button counter:

  buttonPushCounter++;
  if( buttonPushCounter > 9) buttonPushCounter = 0;
  if (buttonPushCounter == 9) servo.write(128);

Ok thanks for the help! But whenever I upload it errors come up such as servo was not declared, do you have any idea why thats happening?!?!?!

You have to put a bit of effort in yourself...like including the <Servo.h> library and then declaring your servo. And you'll need a servo and you'll need to connect it to some power and to a pin and tell the program which pin it's connected to.

Have a look at the Servo examples (Knob or Sweep) in the IDE for how you set things up to use servos.

Steve

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