Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************
- / Learning Objectives:
- / Section One (global vars):
- / Variables
- / Types
- / Initialisation
- /
- / Section Two (setup):
- / setup() in arduino
- / Pin modes
- / Brief intro to functions (ie, you can call them)
- /
- / Section Three (skipping to loop):
- / loop() in arduino
- / Getting data from functions
- / Conditionals
- / Blinking without delay
- / Sending info to the real world (via an LED+digitalWrite())
- /
- / Section Four (success):
- / Fill this with your own code and learning material.
- / I'd recommend a brief intro to writing functions, so maybe keep
- / success() simple for now
- /
- / Section Five (bang):
- / As above. Introduce sound, maybe?
- / Building on simple functions.
- /
- / Section Six (attachInterrupt from setup, also check())
- / Groups of variables ("arrays")
- / More conditionals
- / Further functions (return et al.)
- /
- / Section 7 (Recap):
- / The sketch as The Bigger Picture
- /
- /*******************************************************/
- // Time-related variables
- volatile int period = 1000; // Period of timer. more mistakes = faster time decrease!
- int timer = 30; // Number of periods to wait. (initially 30)
- unsigned long lasttime = 0; // Last LED change time
- unsigned long lasttime2 = 0; // Last timer change time
- boolean armed = true; // Is bomb armed?
- boolean led = false; // Is LED on?
- void setup(){
- // Set all the switch pins to input
- pinMode(14, INPUT);
- pinMode(15, INPUT);
- pinMode(16, INPUT);
- pinMode(17, INPUT);
- // Set the led and buzzer to output
- pinMode(13, OUTPUT);
- pinMode(14, OUTPUT);
- // Make check() run when button pressed
- attachInterrupt(0, check, RISING);
- }
- void check(){
- // Store the states the pin needs to be in to defuse
- int states[4] = {HIGH, LOW, HIGH, LOW};
- // If the code is wrong, penalise!
- if (digitalRead(14) != states[0]){
- period -= 250;
- return;
- }
- if (digitalRead(15) != states[1]){
- period -= 250;
- return;
- }
- if (digitalRead(16) != states[2]){
- period -= 250;
- return;
- }
- if (digitalRead(17) != states[3]){
- period -= 250;
- return;
- }
- // If not, disarm the bomb
- armed = false;
- }
- void bang(){
- // make a bang noise!
- for (;;){ delay(1000); }
- }
- void success(){
- // success!
- for (;;){ delay(1000); }
- }
- void loop(){
- // Get the time
- unsigned long time = millis();
- unsigned long time2 = millis();
- // If its time to change the LED...
- if(time - lasttime > (0.5 * period) ) {
- lasttime = time;
- // Do so!
- led = !led;
- digitalWrite(13, led);
- }
- // Count down one period, if its time to
- if(time2 - lasttime2 > period ) {
- timer--;
- // If we are now overdue, ka-boom!
- if ( timer <= 0 ){
- bang();
- }
- }
- // If the bomb has been disarmed, success.
- if (armed == false){
- success();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment