Skip to content

Instantly share code, notes, and snippets.

@Xatpy
Last active November 20, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xatpy/90d4940673494490e1899edc4c757851 to your computer and use it in GitHub Desktop.
Save Xatpy/90d4940673494490e1899edc4c757851 to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
#include <Servo.h> //From Library
Servo servoMain;
const int buttonPin = 2;
const int ledPin = 13;
const int debugPin = 12;
int counterPresses = 0;
int isEnabled = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
const int rs = 11, en = 10, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
isEnabled = false;
pinMode(ledPin, OUTPUT);
pinMode(debugPin, OUTPUT);
pinMode(buttonPin, INPUT);
servoMain.attach(9); // servo on digital pin 9
servoMain.write(0);
lcd.begin(16, 2);
lcd.print("FIfarmFUT - ");
}
void loop() {
int reading = digitalRead(buttonPin);
digitalWrite(ledPin, isEnabled);
lastButtonState = reading;
if (isEnabled) {
counterPresses++;
digitalWrite(debugPin, HIGH);
servoMain.write(75);
delay(150);
servoMain.write(0);
delay(500);
digitalWrite(debugPin, LOW);
}
printLCD();
delayDetectButtons(500);
}
void printLCD() {
lcd.setCursor(13, 0);
lcd.print(isEnabled ? "On " : "Off");
lcd.setCursor(0, 1);
lcd.print("#A: ");
lcd.setCursor(4, 1);
lcd.print(counterPresses);
}
void delayDetectButtons(unsigned long duration) {
unsigned long start = millis();
while (millis() - start <= duration) {
checkButtons();
}
}
void checkButtons() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
isEnabled = !isEnabled;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment