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
(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)
Move the wire from pin 9 to gnd. If the led lights, it is wired correctly.
If it does not light, reverse the led and try again.
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.
Move the wire from pin 9 to gnd. If the led lights, it is wired correctly.
If it does not light, reverse the led and try again.
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_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.
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.