/*
Forum: https://forum.arduino.cc/t/application-of-timer-in-brushed-dc-motor-control-using-arduino/1233664/3
Wokwi: https://wokwi.com/projects/391986735398421505
*/
#include <LiquidCrystal.h>
// Define LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Define motor control pins
const int motorPin1 = 9; // PWM pin for motor speed control
const int motorPin2 = 10; // Direction pin for motor control
const int buttonPin1 = 6; // Button pin for increasing speed
const int buttonPin2 = 7; // Button pin for decreasing speed
int motorSpeed = 0; // Motor speed (0 to 255)
void setup() {
// Set motor pins as output
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Set button pins as input
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Motor Speed: ");
lcd.setCursor(0, 1);
lcd.print("0");
}
void loop() {
// Increase motor speed
if (digitalRead(buttonPin1) == LOW && motorSpeed < 255) {
motorSpeed++;
}
// Decrease motor speed
if (digitalRead(buttonPin2) == LOW && motorSpeed > 0) {
motorSpeed--;
}
// Update motor speed
analogWrite(motorPin1, motorSpeed);
// Display motor speed on LCD
lcd.setCursor(13, 0);
lcd.print(" "); // Clear previous speed
lcd.setCursor(13, 0);
lcd.print(motorSpeed);
delay(100); // Delay for button debouncing
}