To add the servo to the code that you posted.
Include the servo library
#include <Servo.h>
Create a servo object
Servo servo;
In setup(), attach the servo.
servo.attach(4); // or any pin that you want
in the displayNumber() function add:
servo.write(buttonPushCounter);
I connected up 2 buttons and a servo to my Uno and modified your code as above. It works. Additionally I had to use a different LDC I2C library because I don't have the one that you used. Just uncomment the LiquidCrystal library stuff and comment or remove the hd44780 library stuff. Better yet, install the hd44780 library because, in my opinion, it is a better library.
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <Servo.h>
//LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
hd44780_I2Cexp lcd;
Servo servo;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
int counter = 0;
// this constant won't change:
const int Up_buttonPin = 2; // the pin that the pushbutton is attached to
const int Down_buttonPin = 3;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
int down_buttonState = 0; // current state of the up button
int down_lastButtonState = 0; // previous state of the up button
bool bPress = false;
byte LT[8] =
{
B00111,
B01111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte UB[8] =
{
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte RT[8] =
{
B11100,
B11110,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte LL[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B01111,
B00111
};
byte LB[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
};
byte LR[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11110,
B11100
};
byte MB[8] =
{
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte block[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void setup()
{
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
servo.attach(4);
//lcd.init(); // initialize the lcd
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.createChar(0, LT);
lcd.createChar(1, UB);
lcd.createChar(2, RT);
lcd.createChar(3, LL);
lcd.createChar(4, LB);
lcd.createChar(5, LR);
lcd.createChar(6, MB);
lcd.createChar(7, block);
// Print a message to the LCD.
lcd.backlight();
lcd.clear();
displayNumber();
}
void printNumber(int val)
{
int col = 5;
if ( val >= 10)
{
printDigits(val / 10, col);
printDigits(val % 10, col + 4);
}
else
{
printDigits(val, col);
}
}
void loop()
{
checkUp();
checkDown();
if ( bPress)
{
bPress = false;
displayNumber();
}
}
void displayNumber()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vol:");
printNumber( buttonPushCounter);
servo.write(buttonPushCounter);
}
void custom0(int x)
{
lcd.setCursor(x, 0);
lcd.write(0);
lcd.write(1);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void custom1(int x)
{
lcd.setCursor(x, 0);
lcd.write(1);
lcd.write(2);
lcd.print(" ");
lcd.setCursor(x, 1);
lcd.write(4);
lcd.write(7);
lcd.write(4);
}
void custom2(int x)
{
lcd.setCursor(x, 0);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(4);
}
void custom3(int x)
{
lcd.setCursor(x, 0);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(4);
lcd.write(4);
lcd.write(5);
}
void custom4(int x)
{
lcd.setCursor(x, 0);
lcd.write(3);
lcd.write(4);
lcd.write(7);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.print(" ");
lcd.write(7);
}
void custom5(int x)
{
lcd.setCursor(x, 0);
lcd.write(3);
lcd.write(6);
lcd.write(6);
lcd.setCursor(x, 1);
lcd.write(4);
lcd.write(4);
lcd.write(5);
}
void custom6(int x)
{
lcd.setCursor(x, 0);
lcd.write(0);
lcd.write(6);
lcd.write(6);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void custom7(int x)
{
lcd.setCursor(x, 0);
lcd.write(1);
lcd.write(1);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.print(" ");
lcd.write(7);
}
void custom8(int x)
{
lcd.setCursor(x, 0);
lcd.write(0);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void custom9(int x)
{
lcd.setCursor(x, 0);
lcd.write(0);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.print(" ");
lcd.write(7);
}
void printDigits(int digits, int x)
{
// utility function for digital clock display: prints preceding colon and leading 0
switch (digits)
{
case 0:
custom0(x);
break;
case 1:
custom1(x);
break;
case 2:
custom2(x);
break;
case 3:
custom3(x);
break;
case 4:
custom4(x);
break;
case 5:
custom5(x);
break;
case 6:
custom6(x);
break;
case 7:
custom7(x);
break;
case 8:
custom8(x);
break;
case 9:
custom9(x);
break;
}
}
void checkUp()
{
up_buttonState = digitalRead(Up_buttonPin);
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState)
{
// if the state has changed, increment the counter
if (up_buttonState == LOW)
{
bPress = true;
// 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
up_lastButtonState = up_buttonState;
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState)
{
// if the state has changed, increment the counter
if (down_buttonState == LOW)
{
bPress = true;
// 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
down_lastButtonState = down_buttonState;
}
I raced KT100 (Walbro weed eater carb) a while back and from my experience I think that 1 degree adjustment may be a bit fine. If I remember right I did about 1/8-1/4 turn lean for the long straight and back 1/8-1/4 turn rich for the infield (by hand, of course).