Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- /* Diyode CodeShield Constants */
- #define BUTTON 12
- #define SERVO2 9
- #define LED 6
- #define PIEZO 3
- #define SERVO1 5
- #define PHOTOCELL 5
- #define SCREEN_DARK 0
- #define SCREEN_BRIGHT 1
- #define CHECK_SCRREN_DELAY 40
- #define WALKING_BUTTONPRESS_DURATION 600
- #define DARK 0
- #define BRIGHT 1
- #define ERR_WUE_PREMATURE_RETURN 1
- #define STARTING 1
- #define WALKING 2
- #define WATCHING 3
- #define FOUND 4
- Servo servo1;
- Servo servo2;
- // Unit A Prefs
- int servo1Flat = 84;
- int servo1Deflect = 10;
- int servo2Flat = 90;
- int servo2Deflect = 10;
- String name = "A";
- // Juliet 3DS Prefs
- int SHINY_LOW1 = 11800;
- int SHINY_LOW2 = 13200;
- int SHINY_HIGH = 14300;
- /*
- // Unit B Prefs
- int servo1Flat = 88;
- int servo1Deflect = 15;
- int servo2Flat = 90;
- int servo2Deflect = 10;
- String name = "B";
- */
- // Dahlia 3DS Prefs
- /*
- int SHINY_LOW1 = 11000;
- int SHINY_LOW2 = 12000;
- int SHINY_HIGH = 13500;
- */
- unsigned int mode = 1;
- unsigned int encounter_count = 0;
- unsigned char dark_threshold = 250;
- long last_blackout = 0;
- //unsigned long start_time;
- void setup() {
- Serial.begin (115200);
- Serial.print("Start Unit ");
- Serial.println(name);
- servo1.attach(SERVO1);
- servo1.write(servo1Flat);
- servo2.attach(SERVO2);
- servo2.write(servo2Flat);
- }
- void loop() {
- switch (mode) {
- case STARTING:
- watchButton();
- break;
- case WALKING:
- doWalking();
- break;
- case WATCHING:
- break;
- case FOUND:
- watchButton();
- break;
- }
- }
- void watchButton() {
- if (digitalRead(BUTTON) == HIGH) {
- digitalWrite(LED, LOW);
- mode = WALKING;
- }
- }
- void doWalking()
- {
- // start_time = millis();
- while(1)
- {
- long blackout_duration = walk_until_encounter();
- encounter_count++;
- if(is_shiny(blackout_duration))
- {
- // unsigned long shiny_time = millis() - start_time;
- Serial.print("Shiny: ");
- Serial.println(encounter_count);
- // Serial.print("Elapsed time at Shiny: ");
- // Serial.println(shiny_time);
- encounter_count = 0;
- beep();
- digitalWrite(LED, HIGH);
- mode = FOUND;
- return;
- }
- else
- {
- Serial.print("Encounter count: ");
- Serial.println(encounter_count);
- tapScreen();
- delay(7000);
- }
- }
- }
- int is_shiny(long blackout_duration)
- {
- last_blackout = blackout_duration;
- long shiny_threshold = SHINY_HIGH;
- Serial.print("Blackout Duration: ");
- Serial.println(blackout_duration);
- if(is_between(blackout_duration, SHINY_LOW1, SHINY_LOW2) || (blackout_duration > shiny_threshold))
- // if(blackout_duration > shiny_threshold)
- {
- return 1;
- }
- else
- return 0;
- }
- int is_between(long val, long lower, long upper)
- {
- return (val > lower && val < upper);
- }
- void tapScreen() {
- delay(300);
- servo2.write(servo2Flat - servo2Deflect);
- delay(200);
- servo2.write(servo2Flat);
- }
- void beep() {
- analogWrite( PIEZO, 128);
- delay(400);
- digitalWrite(PIEZO, LOW);
- }
- int walk_until_encounter()
- {
- unsigned long dark_start = 0;
- unsigned char screen_state = SCREEN_BRIGHT;
- while(1)
- {
- delay(CHECK_SCRREN_DELAY);
- if(screen_state == SCREEN_BRIGHT)
- {
- if(millis() % WALKING_BUTTONPRESS_DURATION * 2 < WALKING_BUTTONPRESS_DURATION)
- {
- servo1.write(servo1Flat - servo1Deflect);
- }
- else
- {
- servo1.write(servo1Flat + servo1Deflect);
- }
- }
- int curr = analogRead(PHOTOCELL);
- curr < dark_threshold ? curr = DARK : curr = BRIGHT;
- if(screen_state == SCREEN_BRIGHT && curr == DARK)
- {
- screen_state = SCREEN_DARK;
- servo1.write(servo1Flat);
- dark_start = millis();
- }
- else if(screen_state == SCREEN_DARK && curr == BRIGHT)
- {
- unsigned long ret = millis() - dark_start;
- return ret;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement