Stroboscope controlled by Arduino Uno and Encoder

Hello, I am trying to make a variable strobe light to change how fast the LED blinks using an encoder. I am new to coding, so if you guys could help me out I would greatly appreciate it. I am not sure how to actually make the encoder change the pulses, I was able to get the encoder working, but I believe how it is programmed is now my new issue. I have included my code for reference:

unsigned long timerStart,
              timerEnd = 12048UL; // interval between blinks, uS
uint16_t strobe = 1000; // length of strobe pulse, 1 mS
byte led = 13; // onboard led

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

Encoder myEnc(14,15);

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);

void setup() {
  pinMode(led,OUTPUT); //make led pin OUTPUT
  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 = ");
}

long oldPosition  = -999;

void loop() {
  
  
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
    
  if(micros() - timerStart < strobe){
    digitalWrite(led,HIGH); //turn led on
    lcd.setCursor(6,0);
    lcd.print(strobe);
  }else digitalWrite(led,LOW); // turn led off
  
  if(micros() - timerStart >= timerEnd)
    timerStart += timerEnd; // restart timer  
}

I am not sure how to actually make the encoder change the pulses,

Which variable sets the rate of the blinking ? Hint : it starts with a value of 1000

What does your code do currently when the encoder value changes ? Could it change the value of the variable that sets the rate of blinking at the same time ?

UKHeliBob:
Which variable sets the rate of the blinking ? Hint : it starts with a value of 1000

What does your code do currently when the encoder value changes ? Could it change the value of the variable that sets the rate of blinking at the same time ?

So before I put the "}" below the encoder portion of the code, the light would be off and only blink on when I rotated the encoder. The variable "newPosition" is supposed to be the variable to read the encoder position and "strobe" will change the blinking, however, I have no clue how to actually do this with the encoder. Currently, the light is just on, and nothing is printed on the serial monitor, which is what I thought my code was set to do.

What do you see if you print newPosition immediately after

  long newPosition = myEnc.read();

Does it ever change ?

UKHeliBob:
What do you see if you print newPosition immediately after

  long newPosition = myEnc.read();

Does it ever change ?

Sorry, I fixed this, the serial monitor now reads the "newPosition", I needed to change it to "9600 baud". I'm not sure how to go about getting the encoder to change the pulses though.

What values do you get for newPosition when you move the encoder ?

clockwise I get a negative value in increments of one, and with counterclockwise I get the same but with positive values.

predzZzZzZ:
clockwise I get a negative value in increments of one, and with counterclockwise I get the same but with positive values.

Sounds good.

Before reading the value again save newPosition in a variable named oldPosition. I believe you are doing something like that already. When you have read newPosition, if it has got larger then increase the value of the timing variable and vice versa, and the frequency will change. If it does not change enough for what you want then increase or decrease the timing variable by a larger amount.

Update: I have gotten the LED to blink and change flashing speeds using the encoder, but I am having some more issues:

  1. The flashing speed decreases when turning the encoder counter-clockwise (which I guess is a good thing if the speed increases going clockwise)

  2. The LED starts out at max speed (completely on) where it should start out at "off"

  3. I'm having trouble with telling the exact speed at what it is blinking, eventually, I want to turn it into flashes per minute, but I'm not sure how to tell that with millis()

Here is the most up to date code I am using, any help would be appreciated.

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

Encoder myEnc(14,15);

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);

unsigned long startMillis; 
unsigned long currentMillis;

const byte ledPin = 13;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time

  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("RPM = ");
  lcd.setCursor(0,1);
  lcd.print("Temp (F) = ");
  lcd.setCursor(20,0);
  lcd.print("Bearing Life = ");
}

long oldPeriod = -999;
long newPeriod = 0;

void loop()
{
  unsigned long period = myEnc.read();
  if (period != oldPeriod){
    oldPeriod = period;
    newPeriod = oldPeriod/4;
    lcd.setCursor(6,0);
    lcd.print(newPeriod);
    lcd.print("  ");
  }
  currentMillis = millis();
  if (currentMillis - startMillis >= period)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startMillis = currentMillis;
  }
}
  1. The LED starts out at max speed

That's a silly statement, since an LED doesn't have a speed. It is on or off.

Using three variables, all with period in the name, that contain values that differ by a factor of 4, to keep track of the current and previous values is one too many.

   unsigned long currInterval = myEnc.read() / 4;

   if(currInterval != prevInterval)
   {

I really do not see the benefit of comparing the current interval to the previous interval. I can't imagine that it is NECESSARY to keep the LED on or off until the LED has completed the interval that it was turned on, or off, for, when the interval changes.

If you wish to toggle the state of the LED pin at a "flashes per minute" rate, you need to decide what the encoder reading means, and calculate the on/off time based on that meaning.

Then, independent of reading the encoder/calculating an interval, you toggle, or not, the LED pin.