/*
Forum: https://forum.arduino.cc/t/application-of-timer-in-brushed-dc-motor-control-using-arduino/1233664/3
Wokwi: https://wokwi.com/projects/391959385017752577
*/
#include <LiquidCrystal.h> // Include LCD library
// Define pins
const int motorPin = 9;
const int potentiometerPin = A0; // Assuming a potentiometer for speed control
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int speed = 100; // Initial speed
unsigned long startTime = 0;
const int timerInterval = 1000; // milliseconds
void setup() {
pinMode(motorPin, OUTPUT);
lcd.begin(16, 2); // Initialize LCD
Serial.begin(9600); // Optional for debugging
}
void loop() {
// Read potentiometer value for speed control
int potValue = analogRead(potentiometerPin);
speed = map(potValue, 0, 1023, 0, 255); // Map potentiometer value to PWM range
unsigned long currentTime = millis();
if (currentTime - startTime >= timerInterval) {
startTime = currentTime;
// Update motor speed
analogWrite(motorPin, speed);
// Display motor speed on LCD
lcd.clear();
lcd.print("Motor Speed: ");
lcd.print(speed);
}
}