Example 05 from "Getting Started with Arduino"

I’m confusing from the example 05 in the book.
I think the code is correct.
I don’t understand the connection.
Would you tell me what to do?

//example 05; Lighting LED after pusing the button
//?????????????????????

const int LED=13;//LED????????
const int BUTTON=9;//?????????????????

int val = 0;        //??????????????????
int old_val = 0;    //val?????????????
int state=0;        //LED???(0?????1????)

int brightness=128;  //??????????
unsigned long startTime=0;//?????????

void setup(){
  pinMode(LED,OUTPUT);//Arduino?LED??????????
  pinMode(BUTTON,INPUT);//select BUTTON to output
}

void loop(){
 val=digitalRead(BUTTON);  //?????val????????
 
 //??????????
 if((val==HIGH)&&(old_val==LOW)){
   state=1-state;  //on to off, off to on
   startTime=millis();//millis?Arduino???
                     //?????????????????
                     //????????
                     //????????????????
    delay(10);
 }
 //???????????????????
 if((val=HIGH)&&(old_val==HIGH)){
   //500ms??????????
   if(state==1&&(millis()-startTime)>500){
     brightness++;//brightness?1???
     delay(10);    //brightness???????????????
     if(brightness>255){//255????
     brightness=0;      //255?????0???
     }
   }
 }
 
 old_val=val;  //val??????????????
   
   if (state==1){
  
    analogWrite(LED,brightness);//???????LED???
   }else{
       analogWrite(LED,0);      //LED???
   }
}

state = 1 - state; should achieve exactly the same thing because 1 - 0 == 1 and 1 - 1 == 0

HazardsMind:
It should be state = !state

Heh, heh, no the original is actually more correct, state=1-state;

State is an integer. It starts at 0. subtracting from 1 gives 1, doing so again gives zero.

Performing "not" on a zero integer gives -32768, which will never equal 1 so while it does toggle, it cannot be detected with that comparison.

The actual problem is that pin 13 on a UNO cannot be subject to analogWrite(LED,brightness);

analogWrite

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11.

Performing "not" on a zero integer gives -32768,

Nice try, but check the ! operator vs. the ~ operator.

AWOL:

Performing "not" on a zero integer gives -32768,

Nice try, but check the ! operator vs. the ~ operator.

OK, so if you are going to use that (still not marvellous coding), then the comparison should be:

if(state&&(millis()-startTime)>500)

and

if (state)

just in case "true" should ever be other than 1 (even if in C++, it will not be).

And of course, that was not the problem anyway. :smiley:

I’m still in trouble.
Changing as below,

//example 05; Lighting LED after pusing the button
//???

const int LED=13;//LED???
const int BUTTON=9;//???

int val = 0; //???
int old_val = 0; //val???
int state=0; //LED???(0???1???)

int brightness=128; //???
unsigned long startTime=0;//???

void setup(){
pinMode(LED,OUTPUT);//Arduino?LED???
pinMode(BUTTON,INPUT);//select BUTTON to output
}

void loop(){
val=digitalRead(BUTTON); //???val???

//???
if((val==HIGH)&&(old_val==LOW)){
state = 1 - state; //on to off, off to on
startTime=millis();//millis?Arduino???
//???
//???
//???
delay(10);
}
//???
if((val=HIGH)&&(old_val==HIGH)){
//500ms???
if(state==1&&(millis()-startTime)>500){
brightness++;//brightness?1???
delay(10); //brightness???
if(brightness>255){//255???
brightness=0; //255???0???
}
}
}

old_val=val; //val???

if (state==1){

analogWrite(LED,brightness);//???LED???
}else{
analogWrite(LED,0); //LED???
}
}

and becomes as this.

state = 1 - state;

I think I don’t understand the wiring connection.
Would you teach about that, please?

Your still using delay() in your code. Take them out.

Your button is not wired up correctly at all. The green wire should be going to ground GND, not A0.
Your LED is still set to pin 13, and not one of the pins with a ~ next to them, like pins 3,5,6,10 or 11. Pin 9 is already set for your button, so you can keep that.

If anything you should learn how to wire a button and read it first, then add more to the code when your ready.

Not one, not two, but so many things wrong!

You seem to have been confused by our discussion regarding the "state = 1 - state;" matter. This was never a problem in the original code;

The actual problem is as I explained earlier, that pin 13 on a UNO cannot be subject to analogWrite(LED,brightness);

You cannot use "analogWrite()" on a port (pin) that does not support PWM (such pins are marked "~" on the Arduino UNO) - the command will simply be ignored. The compiler will not warn you of this. So either you forego control of the LED brightness and use a "digitalWrite()" call, or you choose another port that does support this, set "LED" to this value in your code and connect the LED to that pin through a 330 ohm resistor.

Now the other problems.

  • You have connected the ground rail on your breadboard to an "analog" pin instead of the ground terminal on the Arduino. That green wire.
  • You have connected the supply rail (red stripe) of your breadboard to 3.3V instead of Vcc.
  • You have connected a LED to another digital pin without a series resistor - 330 ohm would be suitable. Fortunately, you have not actually managed to activate that pin so so far, you have not caused any damage.
  • Your pushbutton is not connected - you appear to have put in a link to connect to Vcc (if the Vcc had been correctly connected) but the pushbutton is two steps to the right of that position. You do need to check which terminals on the pushbutton correspond to the alternate contacts. You should first run a simpler sketch just to check that you can actually turn the LED on by pressing the button.
  • Strictly speaking, the pushbutton is inappropriately connected anyway - proper practice is to have it pulling the input pin to ground, in which case you can use the internal pull-ups of the ATmega chip with pinMode(BUTTON,INPUT_PULLUP) and not need a pull-up resistor. Of course you then have to swap HIGH for LOW in your button logic.
  • Some other problems in the documentation.

You have posted your recent code as a "quote" - you need to go back and choose "modify" on that posting and change the "quote" tags to "code".