ThatSnail

Untitled

Apr 7th, 2013
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  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://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
  27. int switchstate = 0;
  28.  
  29. void 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.  
  39. void 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. // blink 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. // the code below will run
  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. }
Advertisement
Add Comment
Please, Sign In to add comment