How to make LED flashing in different states

Hi guys, I'm new to this forum and Arduino. And This is my first post here :P.
I have watched the video by Jeremy Blum on youtube to teach me how to use buttons and PMW on Arduino.
But I want to add new features to it.
the original code below written by Jeremy Blum will change the brightness of LED by pressing the buttons each time.( total is 5)

/*
Arduino Tutorials
Episode 3
Switch4 Program (pwm)
Written by: Jeremy Blum
*/

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledLevel = ledLevel + 51;
  }
  lastButton = currentButton;
  
  if (ledLevel > 255) ledLevel = 0;
  analogWrite(ledPin, ledLevel);
}

If I want give a duty cycle to the LED when it's lighted,like the code below

void setup()
{
  pinMode(11, OUTPUT);
}

void loop()
{
  digitalWrite(11, HIGH);
  delay(100); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(11, LOW);
  delay(900);
}

how am I supposed to add those two code together?
For example, if I pressed the button first time and the LED will be on and flashing in a frequency and if I press again it will be brighter and flashing in the same frequency.
I'm really new to the C programming and arduino but willing to learn everything.
Thanks for any advice and help.

delay(100); // Approximately 10% duty cycle @ 1KHz

Nope. Not close.

Have a look at blink without delay

I'm really new to the C programming and arduino but willing to learn everything.

OK, go thru all the examples that come with the IDE.
When you understand them, progress to your own projects.

void setup()

{
  pinMode(11, OUTPUT);
}

void loop()
{
  digitalWrite(11, HIGH);
  delay(100); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(11, LOW);
  delay(900);
}

Will work - in the sense you have a constant semi-bright LED - if you replace the delay() with delayMicroseconds().

So next step is to use two int variables, lowRatio and highRatio, giving them the values 100 and 900.
Then you put in the button code that - say - puts the values 200 and 800 respectivly. This should change your brightness.

Having put these delays into your main loop code, your loop() is limited to run once a millisecond. That is OK for such a short demo program. The challenge is to replace the two delays with one construct using two "timervalues" based on the "blink without delay" example sketch. That way your loop can run unhindred at near Mhz speed - which may be important for other tasks your bigger programs will need.

NB: You do not need to do ALL example sketches :wink: only the ones that contain relevant peripherals for your projects. But then if you want to learn EVERYTHING about C++ then you're going to need to read all examples and the source of the libraries and core. :wink:

First of all i might not be one to talk about teaching but i started with jeremy bulum or whatever his name is, and honestly his code in 2014 is outdated he taped the videos in 2011, and in 3 years im sure alot changed. I say stick to the examples on this website. Jeremy left me with questions , and much confusion. now dont get me wrong im not saying hes a bad teacher im just saying he's not the right teacher to teach beginners, I say you should know a bit about the arduino before looking at his videos t :sweat_smile:.....????

videos in 2011, and in 3 years im sure alot changed

Very much no.
It's flashing an LEE for god sake. That has been the same since micro controllers first did it.
Not everything has to be done just yesterday to be relevant. If you think 3 years is a long time you must be very young.

Msquare:

void setup()

{
  pinMode(11, OUTPUT);
}

void loop()
{
  digitalWrite(11, HIGH);
  delay(100); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(11, LOW);
  delay(900);
}

Will work - in the sense you have a constant semi-bright LED - if you replace the delay() with delayMicroseconds().

So next step is to use two int variables, lowRatio and highRatio, giving them the values 100 and 900.
Then you put in the button code that - say - puts the values 200 and 800 respectivly. This should change your brightness.

Having put these delays into your main loop code, your loop() is limited to run once a millisecond. That is OK for such a short demo program. The challenge is to replace the two delays with one construct using two “timervalues” based on the “blink without delay” example sketch. That way your loop can run unhindred at near Mhz speed - which may be important for other tasks your bigger programs will need.

NB: You do not need to do ALL example sketches :wink: only the ones that contain relevant peripherals for your projects. But then if you want to learn EVERYTHING about C++ then you’re going to need to read all examples and the source of the libraries and core. :wink:

Thank you a lot! Sorry for the late reply. Just finish my other jobs and back to work on this. :slight_smile:

Astrocode317:
First of all i might not be one to talk about teaching but i started with jeremy bulum or whatever his name is, and honestly his code in 2014 is outdated he taped the videos in 2011, and in 3 years im sure alot changed. I say stick to the examples on this website. Jeremy left me with questions , and much confusion. now dont get me wrong im not saying hes a bad teacher im just saying he's not the right teacher to teach beginners, I say you should know a bit about the arduino before looking at his videos t :sweat_smile:.....????

Ya, I understand your point. Jeremy is great, but sometimes just jump too fast for me as a beginner. Do you have any recommendation video or book for me?

Jeremy is great, but sometimes just jump too fast for me as a beginner

There's always the pause key. . .

AWOL:
There's always the pause key. . .

XD Touche AWOL

amebapu - have a look at Nick Gammon's tutorial here Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs