Strange behavior with the Arduino

Hey everyone,
I’ve just started using Arduino (Duemilanove) a few days ago and have been starting to program it just then. I wrote a program to let the arduino make an LED brighten up (PWM) and then hold its brightness. When I press the button again, the LED should dim down and not light up any more. But the problem now is, when I press the LED, what happens is that the LED brightens up as it should, but it wont hold the brightness (so it just brightens PWM, then it becomes unlit). Another interesting issue is that when I hold the button, it brightens and re-brightens again. Any idea on what might be going on? The following is my code:

#define LED 11
#define LED2 12
#define BUTTON 8

void setup()
{
  pinMode(LED , OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(BUTTON , INPUT);
}

void loop(){
  int ledState = 0;
  if(ledState == 0 && readPin(BUTTON) == HIGH){
    ledState = 1;
    brighten(LED);
    ledState = 1;
  }
  if(ledState == 1 && readPin(BUTTON) == HIGH){
    ledState = 0;
    dim(LED);
    ledState = 0;
  }
  if(ledState == 1){
    digitalWrite(LED, 1);
  } else {
    digitalWrite(LED, 0);
  }
}

void brighten(int pin){
  for(int i = 0; i <= 255; i++){
    analogWrite(pin, i);
    delay(10);
  }
}

void dim(int pin){
  for(int i = 255; i >= 0; i++){
    analogWrite(pin, i);
    delay(10);
  }
}

int readPin(int pin){
  if(digitalRead(pin) == HIGH){
    delay(10);
    if(digitalRead(pin) == HIGH){
      digitalWrite(LED2, HIGH);
      return HIGH;
    }
  }
  digitalWrite(LED2, LOW);
  return LOW;
}

LED2 is used for debugging my button. One issue is that, I was hoping to have the 2nd LED light up only when I press the button, and once I let go, it should dim. But what’s happening now is that, once I press it, it’s always bright unless the brightening sequence ends.

I’ve only used 2 LEDs and a tact button, so I dont think I need to post the circuit diagram

How is the switch wired?
Does it have a pull-down?
I think also that you should either declare "ledState" with global scope, or declare it "static".

Hey there

In the subroutine "dim" you have

void dim(int pin){
  for(int i = 255; i >= 0; i[glow]++[/glow]){
    analogWrite(pin, i);
    delay(10);
  }
}

Where I am sure that you meant

void dim(int pin){
  for(int i = 255; i >= 0; i[glow]--[/glow]){
    analogWrite(pin, i);
    delay(10);
  }
}

"Grove" is right about int ledState having static or global place, as it will be remade as a new variable every loop with the value of '0' (leading to digitalwrite(LED,0) in the last "if" statement).

:o
Ok, that I totally did not realize (about the i-- becoming i++), I'll fix it now. And yes, I think making it static/global should fix it.

@Grove: What do you mean by a "pull-down"? Sorry, I dont know too much about electronics.

You don't show how your switch is wired, but let's say it is just wired between your digital pin and +5V, and is a normally-open switch.
With the switch open, the pin is said to be floating, and could sometimes read HIGH and other times LOW, even when the button is not pressed.
A pull-down is a moderately high value resistor (5 to 10k) between the digital pin and ground, which will prevent floating.

Better still, wire your switch between ground and the digital pin, and enable the internal pull-up resistors (see the language reference for "digitalWrite"), and you don't need any external components at all.
Note now however, that with the switch open, the pin will read high, and only go low when you close the switch.

Hey there-

A good place to look is:

It comes with some code and schematic.

To work out the voltage at the pin, the formula:

V(pin) = (R1 / (R1 + R2)) * V

R1 is the resistance of the pull down resistor (usually 4k7, but the example uses 10k) If you imagine the switch being a resistor (R2) with resistance 0 (when closed) and INFINITY (when open); and our voltage accross the series resistors being 5V

V(pin) = (R1 / (R1 + 0)) * (5V) = 5V //for switch closed
V(pin) = (R1 / (R1 + INFINITY)) * (5V) ~ 0V //for switch open

Therefore the voltage at the pin always has a value.

Remember: Any input that you don’t give a voltage level to is an antenna, and any output you do give a voltage to is a short circuit!