Pages: [1]   Go Down
Author Topic: LED - switching state and changing brigtness  (Read 413 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
I'm kind of new in Arduino world.
I want to make program that will change the state of LED HIGH/LOW and if HIGH then 2nd button will change PWM of LED.
I'm watching Jeremy's Blum tutorials on youtube to get familiar with Uno.

this is my not working code.
1st part of my plan is working - switching LED on and off.
Could someone help me?
Code:
int levelPin = 2;
int switchPin = 8;
int ledPin = 11;
int ledLevel = 0;

boolean lastButton = LOW;
boolean lastButtonLevel = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
boolean ledButton = LOW;

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;
}

boolean debounceLevel(boolean last)
{
  boolean current = digitalRead(levelPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(levelPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledOn = !ledOn;
    digitalWrite(ledPin, ledOn);
  }
 
  while (currentButton == HIGH)
  {

/*
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);   
*/
ledButton = debounceLevel(lastButton);
if (ledButton == HIGH)
{
ledLevel = ledLevel + 51;
if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);
}



currentButton = debounce(lastButton);
//if (currentButton == HIGH) lastButton = LOW;
  }
 
  lastButton = currentButton;
}
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 437
Posts: 23669
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, you need to connect the LED to a PWM pin to start.
Assuming pin to resistor to anode, and cathode to Gnd, then:
analogWrite(pwmPin, 255); // full on
analogWrite(pwmPin, 0); // full off

analogWrite(pwmPin, inbetween); // faded amount
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, you need to connect the LED to a PWM pin to start.
Assuming pin to resistor to anode, and cathode to Gnd, then:
analogWrite(pwmPin, 255); // full on
analogWrite(pwmPin, 0); // full off

analogWrite(pwmPin, inbetween); // faded amount

Code:
ledLevel = ledLevel + 51;
if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);

this does PWM functions.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this does PWM functions.

Only if it's connected to a PWM capable pin, otherwise all you will get is either ON of OFF, nothing else.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 437
Posts: 23669
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So what's the problem then? What is the code doing or not doing that you want to change?
Can't tell from the comments in your code.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int ledPin = 11; - PWM one

The code is the only problem,

right now it just switches led on and off,
and I want to make when led is on then I can change it brightness level by pressing another push button,
I don't know how to make it.
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
ledLevel = ledLevel + 51;
if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);

this does PWM functions.
Correct, but over how long a period? Looks to me like your five steps are completed in around a millisecond and so aren't noticeable. Put a delay(500); in there and you might stand a chance of seeing it get brighter.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll write everything step by step to you to understand:

http://i.imgur.com/Un5EPsb.jpg

this is my breadboard.
What I want to do:
1. If you press switch button then LED turns ON or OFF - toggle led not hold to light up
2. if you press level button then LED changes brightness, each push = +51 to value and if 255 - max then turn to 51
3. if you press level button while LED is OFF then nothing happens.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (ledOn == HIGH)
  {
ledButton = debounceLevel(lastButton);
if (ledButton == HIGH && lastButtonLevel == LOW)
{
ledLevel = ledLevel + 51;
}

if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);
lastButtonLevel = ledButton;

currentButton = debounce(lastButton);
if (currentButton == HIGH) ledOn = !ledOn;
  }

I think that it should be like this.
Logged

Pages: [1]   Go Up
Jump to: