source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/10.StarterKit_BasicKit/p02_SpaceshipInterface/p02_SpaceshipInterface.ino @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 1.8 KB
Line 
1/*
2  Arduino Starter Kit example
3 Project 2  - Spaceship Interface
4
5 This sketch is written to accompany Project 2 in the
6 Arduino Starter Kit
7
8 Parts required:
9 1 green LED
10 2 red LEDs
11 pushbutton
12 10 kilohm resistor
13 3 220 ohm resistors
14
15 Created 13 September 2012
16 by Scott Fitzgerald
17
18 http://www.arduino.cc/starterKit
19
20 This example code is part of the public domain
21*/
22
23// Create a global variable to hold the
24// state of the switch. This variable is persistent
25// throughout the program. Whenever you refer to
26// switchState, you’re talking about the number it holds
27int switchstate = 0;
28
29void setup() {
30  // declare the LED pins as outputs
31  pinMode(3, OUTPUT);
32  pinMode(4, OUTPUT);
33  pinMode(5, OUTPUT);
34
35  // declare the switch pin as an input
36  pinMode(2, INPUT);
37}
38
39void loop() {
40
41  // read the value of the switch
42  // digitalRead() checks to see if there is voltage
43  // on the pin or not
44  switchstate = digitalRead(2);
45
46  // if the button is not pressed
47  // turn on the green LED and off the red LEDs
48  if (switchstate == LOW) {
49    digitalWrite(3, HIGH); // turn the green LED on pin 3 on
50    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
51    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
52  }
53  // this else is part of the above if() statement.
54  // if the switch is not LOW (the button is pressed)
55  // turn off the green LED and blink alternatively the red LEDs
56  else {
57    digitalWrite(3, LOW);  // turn the green LED on pin 3 off
58    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
59    digitalWrite(5, HIGH); // turn the red LED on pin 5 on
60    // wait for a quarter second before changing the light
61    delay(250);
62    digitalWrite(4, HIGH); // turn the red LED on pin 4 on
63    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
64    // wait for a quarter second before changing the light
65    delay(250);
66  }
67}
68
Note: See TracBrowser for help on using the repository browser.