Go Down

Topic: LED - switching state and changing brigtness (Read 722 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.

Go Up