Hello,
I am a beginner using Arduino and electronics. I am aware that the current running through the LCD in the backlight should not be more than 20mA, though when I try to connect backlight pin to the 5V port in the Arduino it does not run. When connecting backlight pin to top rail that has 9V battery running through, I use a resistor, when using a 350 Ohm resistor to top rail, to get to the 20mA, the LCD does show lettering.
though now I would have the 9V Battery running to the bottom rail. Is it okay to do it this way or will it overload the LCD? wo the 9V running to bottom rail, then the lcd has no power and wont run, though when connecting the lcd VCC to the 5v port and ground to ground on ariduno is does not get enough power. (ignore the temp sensor, im using it in place of a hall sensor that i have irl but not on tinkercad.. sensor works properly on real circuit)
I am just trying to get all my components to work together, and not overload the circuit.
Code
#include <LiquidCrystal.h> // Include the LiquidCrystal library for display control
const int fanPin = 9; // Define digital pin 9 for motor PWM control
const int hallPin = 2; // Define digital pin 2 for Hall sensor (Hardware Interrupt Pin)
const int buttonPin = A0; // Define analog pin A0 for the arcade button
LiquidCrystal lcd(11, 12, 6, 3, 4, 5); // Initialize LCD: RS(11), E(12), D4(6), D5(3), D6(4), D7(5) - Ports on Ardiuno Board
volatile int magnetCount = 0; // Initialize volatile integer to store lever arm passes over hall sensor / magnet passes (must be volatile for interrupts)
bool systemActive = false; // Initialize boolean to track system running state - Is it runnning currently
bool lastButtonState = HIGH;// Initialize state variable as HIGH (required for INPUT_PULLUP), low when pressed and high when released
unsigned long lastDebounceTime = 0; // Initialize timer for debouncing the button - Need this for making sure that the button clicked once, only tells program once, and not multiple times
const unsigned long debounceDelay = 50; // Set debounce delay constant to 50 milliseconds - wait 50ms then recheck if button is really clicked or not - need because button may register hundreds of rapid clicks
int motorSpeedPWM = 127; // Define PWM speed constant (127 is roughly 50% power - 255 is full power)
void setup() // Main setup function, runs once at startup
{
lcd.begin(16, 2); // Initialize the 16x2 character LCD display
pinMode(fanPin, OUTPUT); // Set the motor pin as an output
pinMode(hallPin, INPUT_PULLUP); // Set Hall pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP); // Fixed syntax error: pinMode only accepts two arguments, configured with internal pull-up
attachInterrupt(digitalPinToInterrupt(hallPin), countMagnet, FALLING); // Attach interrupt to pin 2 to trigger on falling edge
lcd.print("Press to Start"); // Display initial startup text on LCD
} // End of setup function
void loop() // Main loop, runs repeatedly
{
handleButton(); // Call the function to monitor button presses
if (systemActive) // Check if the system is currently running
{
lcd.setCursor(0, 0); // Set cursor to top-left
lcd.print("Rotations: "); // Display count label
lcd.print(magnetCount); // Display current magnet count value
lcd.print(" "); // Clear remaining characters
lcd.setCursor(0, 1); // Set cursor to bottom row
lcd.print("System Running"); // Display status
}
else // If system is not running
{
lcd.setCursor(0, 0); // Set cursor to top-left
lcd.print("Stopped "); // Display status
lcd.setCursor(0, 1); // Set cursor to bottom row
lcd.print("Rotations: "); // Display count label
lcd.print(magnetCount); // Display current magnet count
lcd.print(" "); // Clear remaining characters
} // End of if-else check
} // End of loop
void countMagnet() // Interrupt Service Routine (ISR) to count pulses
{
if (systemActive) // Only count pulses if the system is running
{
magnetCount++; // Increment the global counter variable
} // End of activation check
} // End of interrupt function
void handleButton() // Function to handle button logic and debouncing
{
int reading = digitalRead(buttonPin); // Read the state of the button on pin A0
if (reading != lastButtonState) // Check if the button state has changed
{
lastDebounceTime = millis(); // Reset the debounce timer
} // End of state change check
if ((millis() - lastDebounceTime) > debounceDelay) // Check if sufficient time has passed for debounce
{
if (reading == LOW) // If reading is LOW, the button is pressed (ACTIVE LOW)
{
if (systemActive) // If the system is currently running
{
systemActive = false; // Set state to false
analogWrite(fanPin, 0); // Turn off the motor
lcd.clear(); // Clear the LCD screen
lcd.print("Restarting."); // Display status
delay(1000); // Wait 1 second
lcd.clear(); // Clear the screen
}
else // If system is currently stopped
{
magnetCount = 0; // Reset magnet count to 0
systemActive = true; // Set state to true
analogWrite(fanPin, motorSpeedPWM); // Turn on the motor
lcd.clear(); // Clear the LCD screen
lcd.print("Starting..."); // Display status
delay(500); // Wait 0.5 seconds
} // End of active state logic
} // End of low read logic
} // End of debounce timing check
lastButtonState = reading; // Update the last button state for the next loop
} // End

