Problem for a newbie

Hi there wondered if someone could help. Started playing around with an Arduino recently (not got a lot of experience with electronics in general). So far things are going fairly well. but i've gotton stuck on a project out of the MAKE: Getting started book. The basic idea is to use a button to control the brightness of an LED. The problem I'm having is the button is only controlling the LED going on and off. there doesn't seem to be any change in the brightness.

Any suggestions would be greatly appreciated

I have done a rough mock up of the drawing, but the forum wont let me post it just yet

Im using an Arduino duemilanova and a breadboard. the LED goes into PIN 9 and the button PIN 7.


ah its letting me do it now...again sorry about the quality of the pic

#define LED 9
#define BUTTON 7

int val = 0;
int old_val = 0;
int state = 0;

int brightness = 128;
unsigned long startTime = 0;


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

void loop()
{
  val = digitalRead(BUTTON);
  if ((val == HIGH) && (old_val == LOW))
  {
    state = 1 - state;
    startTime = millis();
    delay(10);
  }
   if ((val == HIGH) && (old_val == LOW))
   {
     if (state == 1 && (millis() - startTime) > 500)
     {
       brightness++;
       delay(10);
     if (brightness > 255)
     {
       brightness = 0;
     }
   }
  }

old_val = val;

if (state == 1)
{
  analogWrite(LED, brightness);
}
else
{
  analogWrite(LED, 0);
}
}

thats the sketch im using it may just be that im tired and missing something very obvious…any idea’s? :S

EDIT:: updated the format

You seem to be missing some braces.

That not withstanding change the delay(10) to delay(100) it is likely things are going too fast for you to see.

When you post code highlight it and hit the # button in the reply box.

thanks for the formating tip :slight_smile:
ill go back through and check the code again. Cant find it at the moment :frowning: and there are no compiler problems

PS i tried the delay increase no joy.

Problem solved

   if ((val == HIGH) && (old_val == HIGH)) {

I'd got confused with the previous circuits code. where it checked High against low. For this one it needed High against High

thanks for the help though :slight_smile: