Go Down

Topic: LED - switching state and changing brigtness (Read 724 times) previous topic - next topic

Oxmaster

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: [Select]
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;
}

CrossRoads

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

Oxmaster


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: [Select]
ledLevel = ledLevel + 51;
if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);


this does PWM functions.

KirAsh4


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.

CrossRoads

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

Oxmaster

Code: [Select]
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.

Henry_Best


Code: [Select]
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.

Oxmaster

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.

Oxmaster

Code: [Select]
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.

Go Up