From 60811af8060d9b27ef122f148f318bcdf30a4384 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 22 Oct 2011 15:08:35 -0700 Subject: Split up the existing code into reusable chunks. --- Puzzle_Box/Puzzle_Box.h | 47 ++++++ Puzzle_Box/Puzzle_Box.pde | 404 +++++++++++++++++++--------------------------- Puzzle_Box/the_eye.h | 82 ++++++++++ 3 files changed, 297 insertions(+), 236 deletions(-) create mode 100644 Puzzle_Box/Puzzle_Box.h create mode 100644 Puzzle_Box/the_eye.h (limited to 'Puzzle_Box') diff --git a/Puzzle_Box/Puzzle_Box.h b/Puzzle_Box/Puzzle_Box.h new file mode 100644 index 0000000..a558803 --- /dev/null +++ b/Puzzle_Box/Puzzle_Box.h @@ -0,0 +1,47 @@ + + + +/* Pin assignments for the version 1.1 shield */ +static const int GPSrx = 4, GPStx = 3; // GPS +static const int LCD_Enable = 6, LCD_RS = 5, LCD_RW = 7; // LCD +static const int LCD_DB4 = 16, LCD_DB5 = 17, LCD_DB6 = 18, LCD_DB7 = 19; +static const int pololu_switch_off = 12; // Pololu switch control +static const int servo_control = 9; // Servo control +static const int LED_pin = 2; // The button LED +static const int BUTTON_PIN = 1; // The pin for the button. + +/* Function definitions */ +void Msg(LiquidCrystal &lcd, const char *top, const char *bottom, unsigned long del); +void drawEye(int location); +void stepEyeAnimation(); +void blinkEye(); +void doMainStage(); +void doButtonStage(); +void doUpdateDistance(); +void doCheckOverrideSerial(); +void doCheckAccess(); +void PowerOff(); + +/* Fixed values should not need changing */ +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; + +/* Eye Animation Steps (12 step program) */ +static const int eyeAnimationSteps[] = { 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2, 1, 0 }; +static const int MAX_EYE_STEPS = 13; + + +/* OLD CONSTANTS */ + +/* These values should be adjusted according to your needs */ +static const int CLOSED_ANGLE = 80; // degrees +static const int OPEN_ANGLE = 180; // degrees +static const float DEST_LATITUDE = 47.512157; +static const float DEST_LONGITUDE = -119.498119; +static const int RADIUS = 5; // meters + + diff --git a/Puzzle_Box/Puzzle_Box.pde b/Puzzle_Box/Puzzle_Box.pde index 481bdbc..bda85f0 100644 --- a/Puzzle_Box/Puzzle_Box.pde +++ b/Puzzle_Box/Puzzle_Box.pde @@ -35,206 +35,23 @@ For supporting libraries and more information see #include #include -void PowerOff(); - -#if 0 // Change this to 1 if you have the version 1.0 shield - -/* Pin assignments for the version 1.0 shield */ -static const int GPSrx = 2, GPStx = 3; -static const int LCD_Enable = 11, LCD_RS = 10, LCD_RW = 7; -static const int LCD_DB4 = 19, LCD_DB5 = 17, LCD_DB6 = 18, LCD_DB7 = 16; -static const int pololu_switch_off = 15; // Pololu switch control -static const int servo_control = 9; -static const int LED_pin = 12; - -#else - -/* Pin assignments for the version 1.1 shield */ -static const int GPSrx = 4, GPStx = 3; // GPS -static const int LCD_Enable = 6, LCD_RS = 5, LCD_RW = 7; // LCD -static const int LCD_DB4 = 16, LCD_DB5 = 17, LCD_DB6 = 18, LCD_DB7 = 19; -static const int pololu_switch_off = 12; // Pololu switch control -static const int servo_control = 9; // Servo control -static const int LED_pin = 2; // The button LED - -#endif - -uint8_t eye1[8] = { - B00000, - B00000, - B00000, - B00000, - B00000, - B00001, - B00011, - B00011, -}; -uint8_t eye2[8] = { - B00000, - B00111, - B01111, - B11000, - B10001, - B00011, - B00111, - B01110, -}; -uint8_t eye3[8] = { - B00000, - B11100, - B11110, - B00011, - B10001, - B11000, - B11100, - B01110, -}; -uint8_t eye4[8] = { - B00000, - B00000, - B00000, - B00000, - B00000, - B10000, - B11000, - B11000, -}; -uint8_t eye5[8] = { - B00011, - B00011, - B00001, - B00000, - B00000, - B00000, - B00000, - B00000, -}; -uint8_t eye6[8] = { - B01110, - B00111, - B00011, - B10001, - B11000, - B01111, - B00111, - B00000, -}; -uint8_t eye7[8] = { - B01110, - B11100, - B11000, - B10001, - B00011, - B11110, - B11100, - B00000, -}; -uint8_t eye8[8] = { - B11000, - B11000, - B10000, - B00000, - B00000, - B00000, - B00000, - B00000, -}; - - -/* These values should be adjusted according to your needs */ -static const int CLOSED_ANGLE = 80; // degrees -static const int OPEN_ANGLE = 180; // degrees -static const float DEST_LATITUDE = 47.512157; -static const float DEST_LONGITUDE = -119.498119; -static const int RADIUS = 5; // meters - -/* Fixed values should not need changing */ -static const int DEF_ATTEMPT_MAX = 50; -static const int EEPROM_OFFSET = 100; - -/* Eye Animation Steps (12 step program) */ -static const int eyeAnimationSteps[] = { 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2, 1, 0 }; -static const int MAX_EYE_STEPS = 13; +#include "Puzzle_Box.h" +#include "the_eye.h" -int currentEyeAnimationStep = 0; -long lastLoopTime = 0; - -/* Loop Stage */ -int loop_stage = 0; - -/* - -While waiting for a button press -1) Blink twice -2) wait 1 second - -*/ - - - - -/* The basic objects needed */ +/* Hardware Objects */ NewSoftSerial nss(GPSrx, GPStx); LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_Enable, LCD_DB4, LCD_DB5, LCD_DB6, LCD_DB7); TinyGPS tinygps; -int attempt_counter; PWMServo servo; +/* Running Information */ +int currentStage = MAIN_STAGE; +int attempt_counter; +int currentEyeAnimationStep = 0; +long lastLoopTime = 0; +float currentDistance = 0; -/* A helper function to display messages of a specified duration */ -void Msg(LiquidCrystal &lcd, const char *top, const char *bottom, unsigned long del) -{ - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print(top); - lcd.setCursor(0, 1); - lcd.print(bottom); - delay(del); -} - -// __|XX|__ -// __|XX|__ -// Location = 0 centers the eye. -void drawEye(int location) -{ - lcd.clear(); - - location = location + 2; - - for (int i = 0; i < 4; i++) { - lcd.setCursor(location + i, 0); - lcd.write(i); - } - - for (int i = 0; i < 4; i++) { - lcd.setCursor(location + i, 1); - lcd.write(i + 4); - } -} - -void stepEyeAnimation() { - //if (currentEyeAnimationStep < 12) { - drawEye(eyeAnimationSteps[currentEyeAnimationStep]); - currentEyeAnimationStep++; - currentEyeAnimationStep = currentEyeAnimationStep % 12; - //} -} - -void blinkEye() { - // Eyes on - digitalWrite(LED_pin, HIGH); - delay(100); - - // Off - lcd.noDisplay(); - digitalWrite(LED_pin, LOW); - delay(100); - - // Back on - lcd.display(); - digitalWrite(LED_pin, HIGH); -} /* The Arduino setup() function */ void setup() @@ -275,6 +92,60 @@ void setup() if (attempt_counter == 0xFF) // brand new EEPROM? attempt_counter = 0; +} + +/* The Arduino loop() function */ +void loop() +{ + + // Check for a stage transition + int buttonState = digitalRead(BUTTON_PIN); + + if (buttonState = HIGH) { + currentStage = BUTTON_STAGE; + } + + // Find our stage + switch (currentStage) { + case MAIN_STAGE: + doMainStage(); + break; + + case BUTTON_STAGE: + doButtonStage(); + break; + + } + + // Find the current distance just to be ready + doUpdateDistance(); + + // Check for override login attempts + doCheckOverrideSerial(); + + /* Turn off after 5 minutes */ + if (millis() >= 300000) + PowerOff(); +} + +/** + * This is what we do while idle... + */ +void doMainStage() { + + + if (millis() - lastLoopTime > 200) { + stepEyeAnimation(); + lastLoopTime = millis(); + } + + +} + +/** + * This is what we do when the button has been pressed. + */ +void doButtonStage() { /* increment it with each run */ ++attempt_counter; @@ -304,17 +175,15 @@ void setup() EEPROM.write(EEPROM_OFFSET, attempt_counter); Msg(lcd, "Seeking", "Signal..", 0); + + doCheckAccess(); } -/* The Arduino loop() function */ -void loop() -{ - - if (millis() - lastLoopTime > 200) { - stepEyeAnimation(); - lastLoopTime = millis(); - } - + +/** + * This function updates the distance, if possible. + */ +void doUpdateDistance() { /* Has a valid NMEA sentence been parsed? */ if (nss.available() && tinygps.encode(nss.read())) { @@ -326,46 +195,55 @@ void loop() if (fix_age != TinyGPS::GPS_INVALID_AGE) { /* Calculate the distance to the destination */ - float distance_meters = TinyGPS::distance_between(lat, lon, DEST_LATITUDE, DEST_LONGITUDE); - - /* Are we close?? */ - if (distance_meters <= RADIUS) - { - Msg(lcd, "Access", "granted!", 2000); - servo.write(OPEN_ANGLE); - } - - /* Nope. Print the distance. */ - else - { - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print("Distance"); - lcd.setCursor(0, 1); - if (distance_meters < 1000) - { - lcd.print((int)distance_meters); - lcd.print(" m."); - } - - else - { - lcd.print((int)(distance_meters / 1000)); - lcd.print(" km."); - } - delay(4000); - Msg(lcd, "Access", "Denied!", 2000); - } - - PowerOff(); + currentDistance = TinyGPS::distance_between(lat, lon, DEST_LATITUDE, DEST_LONGITUDE); } } - /* Turn off after 5 minutes */ - if (millis() >= 300000) - PowerOff(); } +/** + * This function checks for communication traffic from the usb serial port. + */ +void doCheckOverrideSerial() { + +} + +void doCheckAccess() { + /* Are we close?? */ + if (currentDistance <= RADIUS) + { + Msg(lcd, "Access", "granted!", 2000); + servo.write(OPEN_ANGLE); + } + + /* Nope. Print the distance. */ + else + { + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Distance"); + lcd.setCursor(0, 1); + if (currentDistance < 1000) + { + lcd.print((int)currentDistance); + lcd.print(" m."); + } + + else + { + lcd.print((int)(currentDistance / 1000)); + lcd.print(" km."); + } + delay(4000); + Msg(lcd, "Access", "Denied!", 2000); + } + + PowerOff(); +} + + + + /* Called to shut off the system using the Pololu switch */ void PowerOff() { @@ -398,3 +276,57 @@ void PowerOff() exit(1); } +/* A helper function to display messages of a specified duration */ +void Msg(LiquidCrystal &lcd, const char *top, const char *bottom, unsigned long del) +{ + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print(top); + lcd.setCursor(0, 1); + lcd.print(bottom); + delay(del); +} + +// __|XX|__ +// __|XX|__ +// Location = 0 centers the eye. +void drawEye(int location) +{ + lcd.clear(); + + location = location + 2; + + for (int i = 0; i < 4; i++) { + lcd.setCursor(location + i, 0); + lcd.write(i); + } + + for (int i = 0; i < 4; i++) { + lcd.setCursor(location + i, 1); + lcd.write(i + 4); + } +} + +void stepEyeAnimation() { + //if (currentEyeAnimationStep < 12) { + drawEye(eyeAnimationSteps[currentEyeAnimationStep]); + currentEyeAnimationStep++; + currentEyeAnimationStep = currentEyeAnimationStep % 12; + //} +} + +void blinkEye() { + // Eyes on + digitalWrite(LED_pin, HIGH); + delay(100); + + // Off + lcd.noDisplay(); + digitalWrite(LED_pin, LOW); + delay(100); + + // Back on + lcd.display(); + digitalWrite(LED_pin, HIGH); +} + diff --git a/Puzzle_Box/the_eye.h b/Puzzle_Box/the_eye.h new file mode 100644 index 0000000..073a635 --- /dev/null +++ b/Puzzle_Box/the_eye.h @@ -0,0 +1,82 @@ +/* Character definitions for the "Eye" */ + +uint8_t eye1[8] = { + B00000, + B00000, + B00000, + B00000, + B00000, + B00001, + B00011, + B00011, +}; +uint8_t eye2[8] = { + B00000, + B00111, + B01111, + B11000, + B10001, + B00011, + B00111, + B01110, +}; +uint8_t eye3[8] = { + B00000, + B11100, + B11110, + B00011, + B10001, + B11000, + B11100, + B01110, +}; +uint8_t eye4[8] = { + B00000, + B00000, + B00000, + B00000, + B00000, + B10000, + B11000, + B11000, +}; +uint8_t eye5[8] = { + B00011, + B00011, + B00001, + B00000, + B00000, + B00000, + B00000, + B00000, +}; +uint8_t eye6[8] = { + B01110, + B00111, + B00011, + B10001, + B11000, + B01111, + B00111, + B00000, +}; +uint8_t eye7[8] = { + B01110, + B11100, + B11000, + B10001, + B00011, + B11110, + B11100, + B00000, +}; +uint8_t eye8[8] = { + B11000, + B11000, + B10000, + B00000, + B00000, + B00000, + B00000, + B00000, +}; -- cgit v1.2.3