Correct code but doesn't run on the board..:/

I just made a code:
simple one…that it…when u press the button and as long as u hold the button ,the led will be constantly increasing the brightness
when u release the button the led will turn off suddenly
the above was my plan of making a code.I made the code ,no errors,but stilll its not working…!!!
can any one please say the solution for this …

code.txt (684 Bytes)

Hello and welcome,

== is for comparison
= is for assignment

In your code, sometimes you use = for comparison.

but stilll its not working...!!!!!

The code does something. You will get better replies if you say what it ACTUALLY does.

Strange title.

"correct" and "doesn't run" can't both be right.

...R

I Commented a few problems, but there are more.. For example "val" never changes.
You also need to learn to style your code.. It will make is easier to read.. See here. Side note please read the whole page for that link..

const int led = 9;
const int button = 7;
int val = 0;
int brightness = 128;
int state = 0;
unsigned long startTime = 0;
int old_val = 0;
void setup()
{
  pinMode(led,OUTPUT);
  pinMode(button,INPUT);
}
void loop()
{
  //this will not do anything..
  digitalRead(button);

  // Unnecessary brackets.. should be "if(val==HIGH && old_val == LOW)"
  if((val==HIGH)&&(old_val == LOW))   
  {
    state = 1 - state;  //
  startTime = millis();
  delay(10);
}
// Unnecessary brackets.. should be "if(val == HIGH && old_val = HIGH)"
if((val ==HIGH) && (old_val = HIGH))  
{
  // Unnecessary brackets.. should be "if(state == 1 && millis() - startTime > 500)"
  if(state ==1 && (millis() - startTime) >500)  
  {
    brightness++;
    delay(10);
    if (brightness > 250)
    {
      brightness = 0;
    }
  }
}
old_val = val;
if(state = 1) // This sets state to 1 and then returns true, thus will always run
{
  analogWrite(led,brightness);
}
else
{
  analogWrite(led,0);
}}

I completely disagree with you unnecessary brackets comments.

They make the purpose of the programmer clear, unambiguous, forced to keep straight.
They cost nothing to the code, same as white space.

GoForSmoke:
I completely disagree with your unnecessary brackets comments.

Have to agree with this.

Just when you think you know the order of operator precedence, there'll be one that catches you out. An extra set of brackets, to be sure costs nothing.

in the state line:

state = 1 - state;

it decreases the value but to change the led's state it needs to be one:

if(state = 1)
{
  analogWrite(led,brightness);
}

popa

p.s. == is used for comparing
p.p.s in the State (state = 1 - state;) line it make state a negative number

popa6200:
in the State (state = 1 - state;) line it make state a negative number

Actually, assuming state is either 0 or 1, this line will toggle it's value. It's a fairly common variation for state = !state

Yes, I use that a lot:
1-1 = 0
1-0 = 1
1-1 = 0
1-0 = 1

CrossRoads:
Yes, I use that a lot:
1-1 = 0
1-0 = 1
1-1 = 0
1-0 = 1

Twice as much as me, apparently :slight_smile: