AbstractBeliefs

Untitled

Oct 10th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. /********************************************************
  2. / Learning Objectives:
  3. / Section One (global vars):
  4. /   Variables
  5. /   Types
  6. /   Initialisation
  7. /
  8. / Section Two (setup):
  9. /   setup() in arduino
  10. /   Pin modes
  11. /   Brief intro to functions (ie, you can call them)
  12. /
  13. / Section Three (skipping to loop):
  14. /   loop() in arduino
  15. /   Getting data from functions
  16. /   Conditionals
  17. /   Blinking without delay
  18. /   Sending info to the real world (via an LED+digitalWrite())
  19. /
  20. / Section Four (success):
  21. /   Fill this with your own code and learning material.
  22. /   I'd recommend a brief intro to writing functions, so maybe keep
  23. /   success() simple for now
  24. /
  25. / Section Five (bang):
  26. /   As above. Introduce sound, maybe?
  27. /   Building on simple functions.
  28. /
  29. / Section Six (attachInterrupt from setup, also check())
  30. /   Groups of variables ("arrays")
  31. /   More conditionals
  32. /   Further functions (return et al.)
  33. /
  34. / Section 7 (Recap):
  35. /   The sketch as The Bigger Picture
  36. /
  37. /*******************************************************/
  38.  
  39. // Time-related variables
  40. volatile int period = 1000;   // Period of timer. more mistakes = faster time decrease!
  41. int timer = 30;               // Number of periods to wait. (initially 30)
  42. unsigned long lasttime = 0;   // Last LED change time
  43. unsigned long lasttime2 = 0;  // Last timer change time
  44.  
  45. boolean armed = true;  // Is bomb armed?
  46. boolean led = false;   // Is LED on?
  47.  
  48. void setup(){
  49.   // Set all the switch pins to input
  50.   pinMode(14, INPUT);
  51.   pinMode(15, INPUT);
  52.   pinMode(16, INPUT);
  53.   pinMode(17, INPUT);
  54.  
  55.   // Set the led and buzzer to output
  56.   pinMode(13, OUTPUT);
  57.   pinMode(14, OUTPUT);
  58.  
  59.   // Make check() run when button pressed
  60.   attachInterrupt(0, check, RISING);
  61. }
  62.  
  63. void check(){
  64.   // Store the states the pin needs to be in to defuse
  65.   int states[4] = {HIGH, LOW, HIGH, LOW};
  66.  
  67.   // If the code is wrong, penalise!
  68.   if (digitalRead(14) != states[0]){
  69.     period -= 250;
  70.     return;
  71.   }
  72.   if (digitalRead(15) != states[1]){
  73.     period -= 250;
  74.     return;
  75.   }
  76.   if (digitalRead(16) != states[2]){
  77.     period -= 250;
  78.     return;
  79.   }
  80.   if (digitalRead(17) != states[3]){
  81.     period -= 250;
  82.     return;
  83.   }
  84.  
  85.   // If not, disarm the bomb
  86.   armed = false;
  87. }
  88.  
  89. void bang(){
  90.   // make a bang noise!
  91.   for (;;){ delay(1000); }
  92. }
  93.  
  94. void success(){
  95.   // success!
  96.   for (;;){ delay(1000); }
  97. }
  98.  
  99. void loop(){
  100.   // Get the time
  101.   unsigned long time = millis();
  102.   unsigned long time2 = millis();
  103.  
  104.   // If its time to change the LED...
  105.   if(time - lasttime > (0.5 * period) ) {
  106.     lasttime = time;
  107.    
  108.     // Do so!
  109.     led = !led;
  110.     digitalWrite(13, led);
  111.   }
  112.  
  113.   // Count down one period, if its time to
  114.   if(time2 - lasttime2 > period ) {
  115.     timer--;
  116.    
  117.     // If we are now overdue, ka-boom!
  118.     if ( timer <= 0 ){
  119.       bang();
  120.     }
  121.   }
  122.  
  123.   // If the bomb has been disarmed, success.
  124.   if (armed == false){
  125.     success();
  126.   }
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment