Pages: [1]   Go Down
Author Topic: How to make LED flashing in different states  (Read 250 times)
0 Members and 6 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I'm new to this forum and Arduino. And This is my first post here smiley-razz.
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)
Code:
/*
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


Code:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
delay(100); // Approximately 10% duty cycle @ 1KHz
Nope. Not close.

Have a look at blink without delay
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 56
Posts: 2179
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1143
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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 smiley-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. smiley-wink
Logged

Pages: [1]   Go Up
Jump to: