I expected my liquid crystal display to have a range of 30 to 350. When I turn the 5K potentiometer the LCD display's range is 0 to 1023. I thought my code would scale the range down, but it does not. Code is below
#include<LiquidCrystal.h> // Library for the Liquid Crystal Display
LiquidCrystal lcd(12,11,5,4,3,2); // Digital pins for the Liquid Crystal Display
const int duration = 50; // Duration of LED staying on during bpm
int potPin = 0;
int potValue; //Potentiometer pin
unsigned int beats_per_minute = 60; // Variables to track tempo and time delay
unsigned int MS_per_beat = 0;
void setup()
{
lcd.begin(16,2); // Begin LCD
lcd.clear(); // Clear LCD screen
unsigned int milliseconds_per_minute = 1000 * 60; //Calculate MS_per_beat based on tempo in bpm
MS_per_beat = milliseconds_per_minute/beats_per_minute;
}
void loop()
{
int value = analogRead(potPin); //Check the status of the potentiometer
if (value != potValue) //Recalculate the tempo if the value has changed.
{
beats_per_minute = map(potValue, 0, 1023, 30, 350); // Map the value to a reasonable metronome range of 30 to 350 bpm
unsigned int milliseconds_per_minute = 1000 * 60; //Recalculate the delay time
MS_per_beat = milliseconds_per_minute/beats_per_minute; //Recalculate the delay time
potValue = value; //Update potvalue
}
lcd.setCursor(0,0);
lcd.print("Beat Per Minute");
lcd.setCursor(0,1);
lcd.print(value); //Output to the LCD
delay(MS_per_beat); //Delay loop for specified amount of time
lcd.clear(); //Clear LCD screen
}