Introduction: Tilt Switch Activated LED's
Good Afternoon Here is my very first Arduino Code that i wrote, just completed it now.Fritzing: http://fritzing.org/projects/tilt-sw...d-leds-arduino
Tilt Switch or Button Activated LED's
Turns on and off a light emitting diode(LED) connected to digital pin 13 and 12, when activating the Tilt Switch or a pushbutton attached to pin 2. When the tilt switch gives the Arduino the Off Signal, LED12 Will Switch off and LED13 will turn on.
---How To Connect your switch to Digital pin 2
LED1 to Digital Pin13 and LED2 to Digital Pin12
Connect the switch Via a 100K Resistor to the 5V Pin on the Arduino
Connect Led ground to any ground on the Arduino board
The circuit: * LED attached from pin 13 to ground * LED attached from pin 12 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 (switch/button) from ground
Note: on most Arduinos there is already an LED on the board attached to pin 13.
created 31/12/2013 by Ryner GeekSpot http://www.GeekSpot.co.za modified 30 Aug 2011 by Lulouch GeekSpot
For bugs or help email: sales@linuxos.co.za or info@geekspot.co.za
Tested by Ryner and Eesa 2013-12-31
[CODE]/*
Tilt Switch or Button Activated LED's
Turns on and off a light emitting diode(LED) connected to digital
pin 13 and 12, when activating the Tilt Switch or a pushbutton attached to pin 2.
When the tilt switch gives the Arduino the Off Signal, LED12 Will
Switch off and LED13 will turn on.
The circuit:
* LED attached from pin 13 to ground
* LED attached from pin 12 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 (switch/button) from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 31/12/2013
by Ryner GeekSpot
modified 30 Aug 2011
by Lulouch GeekSpot
For bugs or help email:
sales@linuxos.co.za
or
info@geekspot.co.za
Tested by Ryner and Eesa 2013-12-31
This example code is in the public domain.
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 ledPin12 = 12; // the number of the 1st LED pin
const int ledPin13 = 13; // the number of the 2nd LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LEDs pin as an output:
pinMode(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin12, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin12, LOW);
}
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin13, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin13, LOW);
}
}[/CODE]
Comments