From aa5140ccb2ba0c7ced2f89ef002a96c924caf821 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Wed, 13 Aug 2014 22:04:22 -0700 Subject: Box is complete... I think. --- Puzzle_Box/Puzzle_Box.h | 13 +++--- Puzzle_Box/Puzzle_Box.pde | 100 +++++++++++++++++++++++++++++++--------------- Puzzle_Box/quotes.h | 31 +++++++------- 3 files changed, 86 insertions(+), 58 deletions(-) (limited to 'Puzzle_Box') diff --git a/Puzzle_Box/Puzzle_Box.h b/Puzzle_Box/Puzzle_Box.h index b037048..0a0e3a4 100644 --- a/Puzzle_Box/Puzzle_Box.h +++ b/Puzzle_Box/Puzzle_Box.h @@ -14,14 +14,15 @@ void stepEyeAnimation(); void toggleEye(bool on); void doIdle(); -void displayMessage(); -void doLatLong(); -void doHeading(); -void doEastOf(); +bool displayMessage(int id); +bool doLatLong(); +bool doHeading(); +bool doEastOf(); bool doAttemptCount(); bool doUpdateDistance(); void doCheckOverrideSerial(); +void resetState(); void saveState(); void PowerOff(); float toRandomUnit(int choice, float dist); @@ -34,9 +35,6 @@ static const int DEF_ATTEMPT_MAX = 50; static const int EEPROM_OFFSET = 100; /* Program Stage Constants */ -static const int MAIN_STAGE = 1; -static const int BUTTON_STAGE = 2; - static const char MESSAGE = 3; static const char LATLONG = 4; static const char HEADING = 5; @@ -44,7 +42,6 @@ static const char EASTOF = 6; static const char OPEN = 7; static const byte STATES[] = { - 0, MESSAGE, // Daniel!? Is it you? MESSAGE, // Finally, you found me; Adventure time LATLONG, diff --git a/Puzzle_Box/Puzzle_Box.pde b/Puzzle_Box/Puzzle_Box.pde index a5f78ec..f3fa87d 100644 --- a/Puzzle_Box/Puzzle_Box.pde +++ b/Puzzle_Box/Puzzle_Box.pde @@ -46,7 +46,8 @@ TinyGPS tinygps; PWMServo servo; /* Running Information */ -byte currentStage = MAIN_STAGE; +byte firstRun = true; +byte currentStage = 0; byte attempt_counter; byte currentMessageId; byte currentEyeAnimationStep = 0; @@ -114,43 +115,76 @@ void setup() { /* The Arduino loop() function */ void loop() { - // Check for a stage transition - int buttonState = digitalRead(BUTTON_PIN); + // Uncomment to reset box + #ifdef RESET + resetState(); + PowerOff(); + #endif + + #ifdef OPENBOX + servo.write(OPEN_ANGLE); + PowerOff(); + #endif - if (buttonState == LOW ) { + + if (firstRun && STATES[currentStage] == MESSAGE) { + firstRun = false; + displayMessage(currentMessageId++); currentStage++; + } + // Check for a stage transition + int buttonState = digitalRead(BUTTON_PIN); + + bool progressMade = false; + if (buttonState == LOW) { // Find our stage - switch (currentStage) { + switch (STATES[currentStage]) { case MESSAGE: - displayMessage(); + displayMessage(currentMessageId++); + progressMade = true; break; case LATLONG: - doLatLong(); + progressMade = doLatLong(); break; case HEADING: - doHeading(); + progressMade = doHeading(); break; case EASTOF: - doEastOf(); + progressMade = doEastOf(); break; case OPEN: - servo.write(OPEN_ANGLE); // and open the box + servo.write(OPEN_ANGLE); // open the box break; default: // Dunno break; } - - } else { + } else if (STATES[currentStage] != MESSAGE) { doIdle(); } + if (progressMade) { + currentStage++; + saveState(); + + switch (STATES[currentStage]) { + case MESSAGE: + displayMessage(currentMessageId++); + currentStage++; + break; + + case OPEN: + servo.write(OPEN_ANGLE); // open the box + break; + } + } + // Find the current distance just to be ready doUpdateDistance(); @@ -158,7 +192,7 @@ void loop() { doCheckOverrideSerial(); /* Turn off after 5 minutes */ - if (millis() >= 300000) { + if (millis() >= 120000) { PowerOff(); } } @@ -219,7 +253,7 @@ void doIdle() { /** * This is what we do when the button has been pressed. */ -void doLatLong() { +bool doLatLong() { if (doAttemptCount()) { /* Calculate the distance to the destination */ float currentDistance = TinyGPS::distance_between( @@ -227,8 +261,7 @@ void doLatLong() { if (currentDistance <= RADIUS) { // Here we are! - currentStage++; - saveState(); + return true; } else { // Not there yet. Get a random unit @@ -242,9 +275,10 @@ void doLatLong() { delay(4000); } } + return false; } -void doHeading() { +bool doHeading() { if (doAttemptCount()) { /* Calculate the distance to the destination */ float currentDistance = TinyGPS::distance_between( @@ -252,8 +286,7 @@ void doHeading() { if (currentDistance <= RADIUS) { // Here we are! - currentStage++; - saveState(); + return true; } else { // Not there yet, print a bearing @@ -267,19 +300,20 @@ void doHeading() { delay(4000); } } + return false; } -void doEastOf() { +bool doEastOf() { if (doAttemptCount()) { if (currentLon > EASTOF_LONGITUDE) { // Here we are! - currentStage++; - saveState(); + return true; } else { Msg(lcd, "Campfire", "S'mores!", 2000); } } + return false; } bool doAttemptCount() { @@ -322,6 +356,14 @@ bool doAttemptCount() { return true; } +void resetState() { + /* Reset the attempt counter */ + currentStage = 0; + attempt_counter = 0; + currentMessageId = 0; + saveState(); +} + void saveState() { /* Save the new attempt counter */ EEPROM.write(EEPROM_OFFSET, attempt_counter); @@ -380,11 +422,7 @@ void PowerOff() delay(120000); servo.write(OPEN_ANGLE); // and open the box - /* Reset the attempt counter */ - currentStage = 0; - attempt_counter = 0; - currentMessageId = 0; - saveState(); + resetState(); /* Leave the latch open for 10 seconds */ delay(10000); @@ -503,8 +541,7 @@ char* getUnitLabel(int choice) { } } -void displayMessage() { - int id = currentMessageId++; +bool displayMessage(int id) { int pairs = messages[id].pairs; for (int i = 0; i < pairs; i++) { @@ -513,8 +550,5 @@ void displayMessage() { messages[id].lines[i].line2, 1000); } - - currentStage++; - // Intentionally don't saveState(). - // I want the message to display again after a power cycle. + return true; } diff --git a/Puzzle_Box/quotes.h b/Puzzle_Box/quotes.h index ea29cd2..a87427c 100644 --- a/Puzzle_Box/quotes.h +++ b/Puzzle_Box/quotes.h @@ -10,21 +10,18 @@ typedef struct { static const int msgcount = 12; static const message_t messages[] = { - { 1, (line_pair_t []){ {"That is" , "my eye!" } } }, - { 1, (line_pair_t []){ {"Ouch!" , "" } } }, - { 1, (line_pair_t []){ {"Hey!" , "" } } }, - { 2, (line_pair_t []){ {"Stop!" , "" }, - {"Poking ", "Me!" } } }, - { 2, (line_pair_t []){ {"Great...", "now I" }, - {"can only", "see 2D" } } }, - { 3, (line_pair_t []){ {"Grrrr!" , "" }, - {"That." , "Is." }, - {"My." , "Eye!" } } }, - { 1, (line_pair_t []){ {"Ouch!" , "You Suck"} } }, - { 1, (line_pair_t []){ {"I hate" , "you..." } } }, - { 1, (line_pair_t []){ {"Abuse!" , "Abuse!" } } }, - { 1, (line_pair_t []){ {"Eye !!", "Murderer"} } }, - { 2, (line_pair_t []){ {"Its dark", "over" }, - {"there" , " >>" } } }, - { 1, (line_pair_t []){ {"OH! Ouch", "Pain!" } } } + { 1, (line_pair_t []){ {"Daniel!?", "Is it u?"} } }, + { 4, (line_pair_t []){ {"Finally!", "" }, + {"You", "Found me"}, + {"Let's go", "on an" }, + {"Adven-", "ture" } } }, + { 3, (line_pair_t []){ {"We're", "almost" }, + {"Hey!", "What's" }, + {"Over", "There" } } }, + { 3, (line_pair_t []){ {"That was", "fun!" }, + {"But now", "Im tired"}, + {"Back to", "Camp?" } } }, + { 3, (line_pair_t []){ {"Ah,", "Campfire"}, + {"Oh, hey,", "I got" }, + {"You a", "gift" } } } }; -- cgit v1.2.3