speeding/slowing pulsing of led using potentiometer?

Is there any way to simplify the section of code i have commented out? I cannot think of a way to increase the speed of the pulsing led as the val goes up from 0-255 :blush:
sorry if this is basic as I am new, lol. (The adjustable state stuff is simply to click my switch and start/stop the program from running)

void loop() { 
  // set float values 
  float in, out; 
  //start by incorporating on/off switch 
  static bool adjustableState = false; 
  // set value of potentiometer
  int val = analogRead(potPin); 
  // map val
  val = map(val, 0, 1023, 0, 255); 
  constrain (val, 0, 255); 
  delay(5);
  float increment = 0.001; 
  if (digitalRead(switchPin) == HIGH){
    Serial.println("switch Pin is On");
    if (false == adjustableState) {
      adjustableState = true;
    }
    else {
      adjustableState = false;   
    } 
    delay(5);   
  }   
  Serial.println(adjustableState);
  if (adjustableState == true) 
  { 
      /*  if (val = 0) {
    (increment = 0.00001);
    if (val = 50) {
      (increment = 0.0001);
       if (val = 100) {
       (increment = 0.001);
        if (val = 150) {
         (increment = 0.01); 
        if (val =200) {
         (increment = 0.1);
      if (val = 255) {
      (increment = 1.0);
      }  
      }
        } 
     }
    } 
  } */
    Serial.println(val); 
    delay(5);
    for (in = 0; in < 6.283; in = in + increment)
    {
      out = sin(in) *127.5 + 127.5;
      analogWrite(ledPin, out); 
    }
  }
  else { 
    digitalWrite(ledPin, LOW); 
  }
}

A way to simplify   if (val = 0) {?
Sure val = 0;

not exactly your lookup values you used but this is how it could be rewritten

  • minimized the scope for several vars
void loop() 
{ 
  static bool adjustableState = false; 

  if (digitalRead(switchPin) == HIGH)
  {
    Serial.println("switch Pin is On");
    adjustableState = !adjustableState;  // flip the boolean
    delay(5);   
  }   

  Serial.println(adjustableState);
  if (adjustableState == true) 
  { 
    float increment = 1.0;
    int val = analogRead(potPin)/4;  // far faster than map => 0 maps on 0
    int t = 250;  
    while (t > val) 
    {
      t -= 50;     // every step of 50 makes increment 10x smaller
      increment /= 10;
    }
    Serial.println(val); 
    delay(5);

    for (float in = 0.0; in < 6.283; in += increment)  
    {
      int out = sin(in) * 127.5 + 127.5;
      analogWrite(ledPin, out); 
      // might add a delay here too as the fastest loop is very fast
    }
  }
  else 
  { 
    digitalWrite(ledPin, LOW); 
  }
}

robtillaart:
not exactly your lookup values you used but this is how it could be rewritten

  • minimized the scope for several vars
void loop() 

{
  static bool adjustableState = false;

if (digitalRead(switchPin) == HIGH)
  {
¬† ¬† Serial.println(‚Äúswitch Pin is On‚ÄĚ);
    adjustableState = !adjustableState;  // flip the boolean
    delay(5);   
  }

Serial.println(adjustableState);
  if (adjustableState == true)
  {
    float increment = 1.0;
    int val = analogRead(potPin)/4;  // far faster than map => 0 maps on 0
    int t = 250; 
    while (t > val)
    {
      t -= 50;     // every step of 50 makes increment 10x smaller
      increment /= 10;
    }
    Serial.println(val);
    delay(5);

for (float in = 0.0; in < 6.283; in += increment) 
    {
      int out = sin(in) * 127.5 + 127.5;
      analogWrite(ledPin, out);
      // might add a delay here too as the fastest loop is very fast
    }
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}

Thanks a lot, I have not done much incrementing so this is very helpful.

mind you

  if (digitalRead(switchPin) == HIGH)
  {
    Serial.println("switch Pin is On");
    adjustableState = !adjustableState;  // flip the boolean
    delay(5);   
  }

note that adjustableState will flip every iteration of loop() as long as the switchpin == HIGH

you might have meant

  adjustableState = (digitalRead(switchPin) == HIGH);
  if (adjustableState)
  {
    Serial.println("switch Pin is On");
    delay(5);   
  }