Trying to show my light as a percentage

Hi everybody so I have this project I am working on it is a ventilator

so far so good but for example I do not have like a O2 tank so I am going to simulate it with a led and for that idea I have this simple sketch about increasing and decreasing the LED light with 2 buttons

int button1 = 4;
int button2 = 3;
int buttonVal1;
int buttonVal2;
int LEDbright = 0;
int led = 8;
int buzz = 13;


void setup() { // put your setup code here, to run once:
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  pinMode(buzz, OUTPUT);
  Serial.begin(9600);
}

void loop() { // put your main code here, to run repeatedly:
  buttonVal1 = digitalRead(button1);
  buttonVal2 = digitalRead(button2);
  Serial.print("button 1 = ");
  Serial.print(buttonVal1);
  Serial.print(", ");
  Serial.print("button 2 = ");
  Serial.println(buttonVal2);
  delay(250);

  if (buttonVal1 == 0){
    LEDbright = LEDbright + 5;
  }
  if (buttonVal2 == 0){
    LEDbright = LEDbright - 5;
  }
  if (LEDbright > 255){
    LEDbright = 255;
    digitalWrite(buzz, HIGH);
    delay(100);
    digitalWrite(buzz, LOW);
  }
  if (LEDbright < 0){
    LEDbright = 0;
    digitalWrite(buzz, HIGH);
    delay(100);
    digitalWrite(buzz, LOW);
  }


  analogWrite(led, LEDbright);
}

What I want is once I connect my LCD I want that when the LED is completely turned off for it to be equal to 21% and as I gradually increase brightness for it to go up to 100%

so im not sure how to do the math for this to work if anybody has any tips thanks.

The eye's response to brightness is not a simple linear relationship.

You'd be better displaying the percentage on a text display (think: serial monitor), or a bar graph.

Yeah I am going to display the percentage on my display

But for example in my sketch I start at 0 and end in 255 by increasing 5 at a time

All I want is to display those values of 0 to 255 as a percentage from 21% to 100%

Because the brightness of the LED is just for show but the display will show percentage depending on the LED

Have you tried the map function?

Never used map functions and don't know waht they are but just did a quick googled search and I think that will work thanks a lot @TheMemberFormerlyKnownAsAWOL

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.