#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const byte LED_PIN = 13;
const byte LED_PIN2 = 12;
const byte INTERRUPT_PIN = 2;
const byte SERVO_PIN = 9;
const int SERVO_INTERVAL = 15; // 15 milliseconds interval for servo
const int SERVO_MIN_ANGLE = 0;
const int SERVO_MAX_ANGLE = 180;
const int SERVO_STEP = 5;
const int LCD_INTERVAL = 1000; // 1000 milliseconds interval for LCD
const int MAX_LCD_INDEX = 3;
volatile byte state = LOW;
int angle = 0;
bool isIncreasing = true;
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillisServo = 0;
unsigned long previousMillisLCD = 0;
int lcdIndex = 1;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), abc, CHANGE);
lcd.init();
lcd.backlight();
myservo.attach(SERVO_PIN);
}
void loop() {
unsigned long currentMillis = millis();
// Servo control
if (currentMillis - previousMillisServo >= SERVO_INTERVAL) {
previousMillisServo = currentMillis;
abc();
}
// LCD update
if (currentMillis - previousMillisLCD >= LCD_INTERVAL) {
previousMillisLCD = currentMillis;
updateLCD();
}
}
void abc() {
if (isIncreasing) {
angle += SERVO_STEP;
if (angle >= SERVO_MAX_ANGLE) {
angle = SERVO_MAX_ANGLE;
isIncreasing = false;
}
} else {
angle -= SERVO_STEP;
if (angle <= SERVO_MIN_ANGLE) {
angle = SERVO_MIN_ANGLE;
isIncreasing = true;
}
}
myservo.write(angle);
}
void updateLCD() {
lcd.clear();
lcd.setCursor(4 * (lcdIndex - 1), 0);
lcd.print(lcdIndex);
lcd.setCursor(4 * (lcdIndex - 1), 1);
lcd.print("hi");
lcdIndex++;
if (lcdIndex > MAX_LCD_INDEX) {
lcdIndex = 1;
}
}