LED Blinking Speed using Potentiometer Arduino Uno

Hello, I am trying to control the blink speed of an arduino uno using a potentiometer. I am basing this off of a previous topic (Using Potentiometer to Control LED Blink Speed - Exhibition - Arduino Forum). Here is the code that I have been using thus far, ignore the LCD screen stuff, it works.

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
int potpin = A2;  
int ledpin = 13;
int val;    
int lastChange;// the time of changing pin state

void setup()
{
  pinMode(ledpin, OUTPUT);
  val = map(analogRead(potpin), 0, 1023, 500, 5); 
  
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  Serial.begin(9600);
  lcd.print("RPM = ");
  lcd.setCursor(0,1);
  lcd.print("Temp (F) = ");
  lcd.setCursor(20,0);
  lcd.print("Bearing Life = ");
}

void loop()
{
 lastChange = millis();
 digitalWrite(ledpin, HIGH);
 while(lastChange + val <= millis())  // loop while there's time for it :)
 {
   val = map(analogRead(potpin), 0, 1023, 500, 5);  // read the value every time
   Serial.println(val);
 }

 // the same here:
 digitalWrite(ledpin, LOW);
 lastChange = millis();
 while(lastChange + val <= millis())
 {
   val = map(analogRead(potpin), 0, 1023, 500, 5); 
   Serial.println(val); 
 }
}

The potentiometer is not changing the speed and I'm not sure what the issue is. I am having trouble including some images of the way it is connected, due to file size restrictions, so please let me know if those would be helpful. Thank you.

Serial.println is blocking as long as buffer is full. Remove it and see if it helps.

EDIT: as written it will blink much much faster than you can see. It will be more like dimming the LED.

EDIT2: after rereading it is not so bad: the LED should blink from 1Hz to 100Hz. Since blinking can be seen to ~40Hz (IIRC) you should be able to see the blinking when it is slow. What values you see on Serial.println? What the LED does?

predzZzZzZ:

int val;    

int lastChange;// the time of changing pin state

These variables should be 'unsigned long' to hold a millis() value
and use:

while(millis() - lastChange < val) {

Also... I presume you are seeing 'val' change in the serial monitor ?

Yours,
TonyWilk