Book "Getting started with Arduino", example 5.2

Hello everybody, new guy here!

First let me say I am very happy to have your acquaintance, and very eager to be helpful when I get a good grip on how it all works, but right now I am very, veeeeery beginner.

So, my girlfriend got me an arduino and the book on the title of the post, and when trying example 5.2 where he asks me to put together 2 of the prior examples (a button and a LED), I couldn´t make it work for the life of me, I´ll show you my best guess and hopefully you can give me some insight :slight_smile:
(I don´t think it has anything to do with the code because I just copied it, but there was no diagram, drawing or anything on how to mount it on the protoboard)

Hello,

Nice to see you here,

The most often when you have a problem the simplest solution works.
Have you checked that the LED is connected correctly, it's + and - side?

With regards,

Mike

Big leg to the right side, right?
I think this might be the only thing I did right

I'm sorry, but I don't have the book you mean, so without more info there isn't much I can do for you.

Thanks anyway, I´ll try to be more specific by tomorrow morning

The short led lead goes to gnd.

  1. Move the wire from pin 9 to gnd. If the led lights, it is wired correctly.
  2. If it does not light, reverse the led and try again.
  3. Use any two opposite corners of the push button contacts, this will always work no matter how the button is oriented. For example, start with the top left contact and call that contact #1. Proceed clockwise and name the top right #2. Bottom right is 3 and bottom left 4. With that numbering scheme, use either pairs 1 & 3 or 2 & 4.

If that doesn't get things working, post your code with code tags (the </> icon) and a photograph of your breadboard.

Welcome to the group.

FYI
All about LEDs

avr_fred:
The short led lead goes to gnd.

  1. Move the wire from pin 9 to gnd. If the led lights, it is wired correctly.
  2. If it does not light, reverse the led and try again.
  3. Use any two opposite corners of the push button contacts, this will always work no matter how the button is oriented. For example, start with the top left contact and call that contact #1. Proceed clockwise and name the top right #2. Bottom right is 3 and bottom left 4. With that numbering scheme, use either pairs 1 & 3 or 2 & 4.

If that doesn't get things working, post your code with code tags (the </> icon) and a photograph of your breadboard.

A warning! This last summer, a customer supplied a kit that contained a bag of red LEDS. From China. Some had the long-short leg polarity reversed!

Paul

Paul_KD7HB:
A warning! This last summer, a customer supplied a kit that contained a bag of red LEDS. From China. Some had the long-short leg polarity reversed!

Paul

That happened to me.
Go by the 'flat side' as the cathode.

If I am not mistaken, the problem lies in the LED connection. I had the same issue yesterday and got crazy over my code until i noticed the mistake in the wiring.

The LED should be connected to the PIN 9 and to GND via the resistor (if this does not solve the issue, try reversing the LED). The way you have it, both sides of the LED are HIGH at the same time so no current might flow, thus no light.

Hey thanks all for coming in

It seems my phone is too powerful and takes heavy pictures, I'll rry my best

So, first the code

Now photo 2

The last photo.
Hey all, thanks a lot dor the welcome and the help, the legends are true, the arduino community really is great

OPs images

Please attach your code, don't take a image of it.

Very very sorry, won´t happen again

const int LED = 9;
const int BOTAO = 7;
int val = 0;
int old_val = 0;
int state = 0;
int brightness = 128;
unsigned long startTime = 0;

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

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

}
}

The push button resistor is wired incorrectly. The resistor should be connect between +5 volts and digital input 7. The switch contact is then connected between digital input 7 and GND.

If ithis does not work, see my earlier post #5 for how to connect the switch.

See S2 for switch wiring.

2017-08-05_20-33-24.jpg
Click on the image for full resolution.

Do you want:
state= 1 - state;
or
state = !state;

Use to format your code.

Edit:

Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Am I any closer?
Resistor between 5V and input 7, switch contact between 7 and GND
Still no reaction when I push the button