Advertisement
Guest User

ShinyFinder

a guest
Feb 7th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. /* Diyode CodeShield Constants */
  4. #define BUTTON 12
  5. #define SERVO2 9
  6. #define LED 6
  7. #define PIEZO 3
  8. #define SERVO1 5
  9. #define PHOTOCELL 5
  10.  
  11. #define SCREEN_DARK 0
  12. #define SCREEN_BRIGHT 1
  13. #define CHECK_SCRREN_DELAY 40
  14. #define WALKING_BUTTONPRESS_DURATION 600
  15. #define DARK 0
  16. #define BRIGHT 1
  17. #define ERR_WUE_PREMATURE_RETURN 1
  18.  
  19. #define STARTING 1
  20. #define WALKING 2
  21. #define WATCHING 3
  22. #define FOUND 4
  23. Servo servo1;
  24. Servo servo2;
  25.  
  26.  
  27. // Unit A Prefs
  28. int servo1Flat = 84;
  29. int servo1Deflect = 10;
  30. int servo2Flat = 90;
  31. int servo2Deflect = 10;
  32. String name = "A";
  33.  
  34. // Juliet 3DS Prefs
  35.  
  36. int SHINY_LOW1 = 11800;
  37. int SHINY_LOW2 = 13200;
  38. int SHINY_HIGH = 14300;
  39.  
  40.  
  41.  
  42. /*
  43. // Unit B Prefs
  44. int servo1Flat = 88;
  45. int servo1Deflect = 15;
  46. int servo2Flat = 90;
  47. int servo2Deflect = 10;
  48. String name = "B";
  49. */
  50.  
  51. // Dahlia 3DS Prefs
  52. /*
  53. int SHINY_LOW1 = 11000;
  54. int SHINY_LOW2 = 12000;
  55. int SHINY_HIGH = 13500;
  56. */
  57.  
  58. unsigned int mode = 1;
  59.  
  60. unsigned int encounter_count = 0;
  61. unsigned char dark_threshold = 250;
  62. long last_blackout = 0;
  63. //unsigned long start_time;
  64.  
  65. void setup() {
  66.   Serial.begin (115200);
  67.   Serial.print("Start Unit ");
  68.   Serial.println(name);
  69.  
  70.   servo1.attach(SERVO1);
  71.   servo1.write(servo1Flat);
  72.  
  73.   servo2.attach(SERVO2);
  74.   servo2.write(servo2Flat);
  75. }
  76.  
  77. void loop() {
  78.   switch (mode) {
  79.     case STARTING:
  80.       watchButton();
  81.       break;
  82.     case WALKING:
  83.       doWalking();
  84.       break;
  85.     case WATCHING:
  86.       break;
  87.     case FOUND:
  88.       watchButton();
  89.       break;
  90.   }
  91. }
  92.  
  93. void watchButton() {
  94.   if (digitalRead(BUTTON) == HIGH) {
  95.     digitalWrite(LED, LOW);
  96.     mode = WALKING;
  97.   }  
  98. }
  99.  
  100. void doWalking()
  101. {
  102. //    start_time = millis();
  103.     while(1)
  104.     {
  105.         long blackout_duration = walk_until_encounter();
  106.         encounter_count++;
  107.         if(is_shiny(blackout_duration))
  108.         {
  109.          //   unsigned long shiny_time = millis() - start_time;
  110.             Serial.print("Shiny: ");
  111.             Serial.println(encounter_count);
  112.          //   Serial.print("Elapsed time at Shiny: ");
  113.          //   Serial.println(shiny_time);
  114.             encounter_count = 0;
  115.             beep();
  116.             digitalWrite(LED, HIGH);
  117.             mode = FOUND;
  118.             return;
  119.         }
  120.         else
  121.         {
  122.             Serial.print("Encounter count: ");
  123.             Serial.println(encounter_count);            
  124.             tapScreen();
  125.             delay(7000);
  126.         }
  127.     }
  128. }
  129.  
  130. int is_shiny(long blackout_duration)
  131. {
  132.     last_blackout = blackout_duration;
  133.     long shiny_threshold = SHINY_HIGH;
  134.     Serial.print("Blackout Duration: ");
  135.     Serial.println(blackout_duration);
  136.  
  137.     if(is_between(blackout_duration, SHINY_LOW1, SHINY_LOW2) || (blackout_duration > shiny_threshold))
  138.  //   if(blackout_duration > shiny_threshold)
  139.     {
  140.         return 1;
  141.     }
  142.     else
  143.         return 0;
  144. }
  145.  
  146. int is_between(long val, long lower, long upper)
  147. {
  148.     return (val > lower && val < upper);
  149. }
  150.  
  151. void tapScreen() {
  152.   delay(300);
  153.   servo2.write(servo2Flat - servo2Deflect);
  154.   delay(200);
  155.   servo2.write(servo2Flat);
  156. }
  157.  
  158. void beep() {
  159.  analogWrite( PIEZO, 128);
  160.  delay(400);
  161.  digitalWrite(PIEZO, LOW);
  162. }
  163.  
  164.  
  165.  int walk_until_encounter()
  166. {
  167.     unsigned long dark_start = 0;
  168.     unsigned char screen_state = SCREEN_BRIGHT;
  169.      
  170.     while(1)
  171.     {
  172.         delay(CHECK_SCRREN_DELAY);
  173.         if(screen_state == SCREEN_BRIGHT)
  174.         {
  175.             if(millis() % WALKING_BUTTONPRESS_DURATION * 2 < WALKING_BUTTONPRESS_DURATION)
  176.             {
  177.               servo1.write(servo1Flat - servo1Deflect);
  178.             }
  179.             else
  180.             {
  181.               servo1.write(servo1Flat + servo1Deflect);
  182.  
  183.             }
  184.         }
  185.          
  186.         int curr = analogRead(PHOTOCELL);
  187.          
  188.         curr < dark_threshold ? curr = DARK : curr = BRIGHT;
  189.          
  190.         if(screen_state == SCREEN_BRIGHT && curr == DARK)
  191.         {
  192.             screen_state = SCREEN_DARK;
  193.             servo1.write(servo1Flat);
  194.             dark_start = millis();
  195.         }
  196.          
  197.         else if(screen_state == SCREEN_DARK && curr == BRIGHT)
  198.         {
  199.             unsigned long ret = millis() - dark_start;
  200.              return ret;
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement