// A test for the Discord channel.
// Replaced the Adafruit_Liquidcrystal library by the common library.
// Added a I2C bus scanner.
#include <LiquidCrystal_I2C.h>
// LCD Initialization
//Adafruit_LiquidCrystal lcd(0);
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ---------------- PIN DEFINITIONS ----------------
const int BTN_0 = 5;
const int BTN_30 = 4;
const int BTN_45 = 3;
const int BTN_60 = 2;
// L293D Motor Driver Pins
const int MOTOR_EN = 10;
const int MOTOR_IN1 = 9;
const int MOTOR_IN2 = 8;
// ---------------- VARIABLES ----------------
int targetAngle = 0;
int currentAngle = 0;
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 40;
// ---------------- SETUP ----------------
void setup() {
Serial.begin(9600);
// Added, a I2C bus scan
Wire.begin();
Serial.print("I2C bus scan: ");
for(int address=1; address<128; address++)
{
Wire.beginTransmission(address);
int error = Wire.endTransmission();
if(error == 0)
{
Serial.print("0x");
Serial.print(address,HEX);
Serial.print(", ");
}
}
Serial.println();
// lcd.begin(16, 2); // this is for the Adafruit display.
lcd.clear();
lcd.init();
lcd.backlight();
// Button Inputs
pinMode(BTN_0, INPUT_PULLUP);
pinMode(BTN_30, INPUT_PULLUP);
pinMode(BTN_45, INPUT_PULLUP);
pinMode(BTN_60, INPUT_PULLUP);
// Motor Outputs
pinMode(MOTOR_EN, OUTPUT);
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
stopActuator();
updateDisplay();
Serial.println("System Ready");
}
// ---------------- MAIN LOOP ----------------
void loop() {
checkButtons();
// Smooth actuator movement
if (millis() - lastUpdateTime >= updateInterval) {
lastUpdateTime = millis();
if (currentAngle < targetAngle) {
extendActuator();
currentAngle++;
updateDisplay();
}
else if (currentAngle > targetAngle) {
retractActuator();
currentAngle--;
updateDisplay();
}
else {
stopActuator();
}
}
}
// ---------------- BUTTON HANDLER ----------------
void checkButtons() {
if (digitalRead(BTN_0) == LOW) {
setTarget(0);
}
else if (digitalRead(BTN_30) == LOW) {
setTarget(30);
}
else if (digitalRead(BTN_45) == LOW) {
setTarget(45);
}
else if (digitalRead(BTN_60) == LOW) {
setTarget(60);
}
}
// ---------------- TARGET SETTER ----------------
void setTarget(int angle) {
if (targetAngle != angle) {
targetAngle = angle;
Serial.print("Target set to: ");
Serial.println(targetAngle);
updateDisplay();
delay(200); // debounce
}
}
// ---------------- MOTOR CONTROLS ----------------
void extendActuator() {
digitalWrite(MOTOR_EN, HIGH);
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
}
void retractActuator() {
digitalWrite(MOTOR_EN, HIGH);
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
}
void stopActuator() {
digitalWrite(MOTOR_EN, LOW);
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, LOW);
}
// ---------------- LCD DISPLAY ----------------
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SETTING:");
lcd.print(targetAngle);
lcd.print((char)223); // degree symbol
lcd.setCursor(0, 1);
lcd.print("ACTUAL :");
lcd.print(currentAngle);
lcd.print((char)223);
}