How does Arduino know what 'int state' means here?

const int LED = 9; // the pin for the LED
const int BUTTON = 7; // input pin of the pushbutton

int val = 0; // stores the state of the input pin

int old_val = 0; //stores the previous state of 'val'
int state = 0; //0 = LED off while 1 = LED on

int brightness = 128; //Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() {
pinMode(LED, OUTPUT); //tell Arduino LED is an output
pinMode(BUTTON, INPUT); //and BUTTON is an input
}

void loop() {

val = digitalRead(BUTTON); //read input value and store it

//check if there was a transition
if ((val == HIGH) && (old_val == LOW)) {

state = 1 - state; //change the state from off to on or vice versa

startTime = millis(); //millis() is the Arduino clock, it returns how many ms have passed since the board has been reset

// (this line remembers when the button was last pressed)
delay(10);
}

//check whether the button is being held down
if ((val == HIGH) && (old_val == HIGH)) {

//If the button is held for more than 500ms
if (state == 1 && (millis() - startTime) > 500) {

brightness++; //increment brightness by 1
delay(10); //delay to avoid brightness going too fast

if (brightness > 255) { //255 is the max brightness

brightness = 0; //if we go over 255 let's go back to 0
}
}
}

old_val = val; //val is now old, let's store it

if (state == 1) {
analogWrite(LED, brightness); //turn LED ON at the current brightness level

} else {
analogWrite(LED, 0); //turn LED OFF
}
}

Hi there, I'm a newbie going through the beginner's handbook following this script, which makes a LED light gradually brighten as you hold down a pushbutton. It works perfectly, but I'm struggling to understand how Arduino knows that 'state' means whether the LED is on or off when it was never associated with the LED in the code? Many thanks!

Sure it is:

  if (state == 1) {
    analogWrite(LED, brightness); //turn LED ON at the current brightness level

  } else {
    analogWrite(LED, 0); //turn LED OFF

It doesn't know.
There is nothing special about this variable that links it directly to the LED, except the code.

The LED is turned on and off by these instructions in the code

analogWrite(LED, brightness);
analogWrite(LED, 0);

which will be executed (or not) based on the value of the 'state' variable, which is in turn is set by other instructions in the code based on some specified conditions (which here relate to the button).

So in that sense the comment

int state = 0; //0 = LED off while 1 = LED on

could be seen as misleading if you interpret it literally.

For example, if you changed the 'loop' function to:

void loop {
analogWrite(LED, 255);
analogWrite(LED, 0);
}

The LED would turn on and off (too fast to see), but it would not have any effect on the 'state' variable.

Hope that helped.

bms001:
It doesn't know.
There is nothing special about this variable that links it directly to the LED, except the code.

The LED is turned on and off by these instructions in the code

analogWrite(LED, brightness);

analogWrite(LED, 0);



which will be executed (or not) based on the value of the 'state' variable, which is in turn is set by other instructions in the code based on some specified conditions (which here relate to the button).

So in that sense the comment


int state = 0; //0 = LED off while 1 = LED on



could be seen as misleading if you interpret it literally.

For example, if you changed the 'loop' function to:


void loop {
analogWrite(LED, 255);
analogWrite(LED, 0);
}



The LED would turn on and off (too fast to see), but it would not have any effect on the 'state' variable.

Hope that helped.

Thanks! But I don't see any code that links 'state' to the button or the LED at all. At least with 'val' there is:

val = digitalRead(BUTTON)

But I can't see any equivalent for 'state', unless I'm missing something? Thanks!

Oh I see so 'state' is just an independent number that loops itself from 0 to 1 then back again with this line?

state = 1 - state

Perhaps ‘state’ could be renamed ‘enabled’