Light Not Fading
A bit of a silly mistake we
constantly made, when you
switch from simple on off to
fading remember to move the
LED wire from pin 13 to pin 9
21
CIRC-07
Code
(no need to type everything in just)
File > Examples > Digital > Button
(example from the great arduino.cc site check it out for other great ideas)
/* Button
* Turns on and off a light emitting diode(LED) connected to
* digital pin 13, when pressing a pushbutton attached to pin 7. The circuit:
* LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to // set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT); }
Not Working?
(3 things to try)
More, More, More:
More details, where to buy more parts, where to ask more questions.
http://tinyurl.com/c64tmt
Making it Better
Light Not Turning On
The pushbutton is square and
because of this it is easy to put
it in the wrong way. Give it a 90
degree twist and see if it starts
working.
On button off button:
Fading up and down:
The initial example may be a little underwhelming (ie. I
Lets use the buttons to control an analog signal. To do this you
don't really need an Arduino to do this), lets make it a
will need to change the wire connecting the LED from pin 13 to
little more complicated. One button will turn the LED on
pin 9, also change this in code.
int ledPin = 13; ----> int ledPin = 9;
the other will turn the LED off. Change the code to.
Next change the loop() code to read.
int ledPin = 13; // choose the pin for the LED
int inputPin1 = 3; // button 1
int value = 0;
int inputPin2 = 2; // button 2
void loop(){
if (digitalRead(inputPin1) == LOW) { value--; }
void setup() {
else if (digitalRead(inputPin2) == LOW) { value++; }
pinMode(ledPin, OUTPUT); // declare LED as output
value = constrain(value, 0, 255);
pinMode(inputPin1, INPUT); // make button 1 an input
analogWrite(ledPin, value);
pinMode(inputPin2, INPUT); // make button 2 an input
delay(10);
}
}
void loop(){
Changing Fade Speed:
if (digitalRead(inputPin1) == LOW) {
digitalWrite(ledPin, LOW); // turn LED OFF
If you would like the LED to fade faster or slower, there is only
} else if (digitalRead(inputPin2) == LOW) {
digitalWrite(ledPin, HIGH); // turn LED ON
one line of code that needs changing;
}
delay(10); ----> delay(new #);
}
To fade faster make the number smaller, slower requires a
Upload the program to your board, and start toggling the
larger number.
LED on and off.
Underwhelmed?
No worries these circuits are all
super stripped down to make
playing with the components
easy, but once you throw them
together the sky is the limit.
20
CIRC-07
What We’re Doing:
.:Button Pressing:.
.:Pushbuttons:.
Up to this point we have focused entirely on outputs, time to
get our Arduino to listen, watch and feel. We'll start with a
simple pushbutton. Wiring up the pushbutton is simple. There is
one component, the pull up resistor, that might seem out of place.
This is included because an Arduino doesn't sense the same way we do (ie button pressed,
button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or
LOW. The button is set up to pull the Arduino's pin LOW when it is pressed, however, when the
button is unpressed the voltage of the pin will float (causing occasional errors). To get the
Arduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up
resistor.
(note: the first example program uses only one of the two buttons)
The Circuit:
Wire
Pushbutton
x2
560 Ohm Resistor
Green-Blue-Brown
x1
2 Pin Header
x4
CIRC-07
Breadboard sheet
x1
Parts:
Red LED
x1
10k Ohm Resistor
Brown-Black-Orange
x2
Schematic:
Arduino
pin 13
LED
resistor
(560ohm)
gnd
(ground) (-)
pin 2
pushbutton
pin 3
+5 volts
Arduino
resistor
(10k ohm)
.:download:.
breadboard layout sheet
http://tinyurl.com/dzmh8w
.:view:.
assembling video
http://tinyurl.com/dnln6g
The Internet