CaptainSpaceCat

Automatic Shiny Hunter

Nov 20th, 2017
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. #include "Adafruit_TCS34725.h"
  3.  
  4. #define shinyLED 12
  5. #define switchDetector 13
  6.  
  7. #define pinAX 5
  8. #define pinBY 6
  9. #define pinUL 7
  10. #define pinDR 8
  11. #define pinStart 9
  12.  
  13. const int NUM_MEASUREMENTS = 6;
  14.  
  15. int numLoops = 0;
  16.  
  17. Servo servoAX, servoBY, servoUL, servoDR, servoStart;
  18. bool useAX = false;
  19. bool useBY = false;
  20. bool useUL = false;
  21. bool useDR = false;
  22. bool useStart = false;
  23.  
  24. bool DEBUG = true;
  25.  
  26. int posAX[] = {105, 44, 178}; //A <-> X
  27. int posBY[] = {90, 30, 155};
  28. int posUL[] = {90, 70, 115};
  29. int posDR[] = {90, 65, 105};
  30. int posStart[] = {90, 55};
  31.  
  32. int delayDefault = 250;
  33. int delayScreenTransition = 2500;
  34.  
  35. #define commonAnode true
  36. Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
  37.  
  38. void resetServos() {
  39.   if (useAX) {servoAX.write(posAX[0]);}
  40.   if (useBY) {servoBY.write(posBY[0]);}
  41.   if (useUL) {servoUL.write(posUL[0]);}
  42.   if (useDR) {servoDR.write(posDR[0]);}
  43.   if (useStart) {servoStart.write(posStart[0]);}
  44.  
  45.   delay(delayDefault);
  46. }
  47.  
  48. void setup() {
  49.   if (DEBUG) {
  50.     Serial.begin(9600);
  51.   }
  52.  
  53.   pinMode(switchDetector, INPUT);
  54.   pinMode(shinyLED, OUTPUT);
  55.   digitalWrite(shinyLED, LOW);
  56.  
  57.   if (tcs.begin()) {
  58.     Serial.println("Found sensor");
  59.   } else {
  60.     Serial.println("No TCS34725 found ... check your connections");
  61.     while (1); // halt!
  62.   }
  63.  
  64. //  if (useAX) {servoAX.attach(5);}
  65. //  if (useBY) {servoBY.attach(6);}
  66. //  if (useUL) {servoUL.attach(8);}
  67. //  if (useDR) {servoDR.attach(7);}
  68. //  if (useStart) {servoStart.attach(9);}
  69.   servoAX.attach(5);
  70.   servoBY.attach(6);
  71.   servoUL.attach(7);
  72.   servoDR.attach(8);
  73.   servoStart.attach(9);
  74.   resetServos();
  75.  
  76.   //switchToTester();
  77.  
  78.   waitForStart();
  79.  
  80.   firstLoop();
  81. }
  82.  
  83. void firstLoop() {
  84.   loop();
  85.   detachUnused();
  86. }
  87.  
  88. void loop() {
  89.   numLoops++;
  90.   if (DEBUG) {
  91.     Serial.print("Starting Loop: "); Serial.println(numLoops);Serial.println();
  92.   }
  93.   hyperspace_default();
  94. }
  95.  
  96. //=========================HUNTING LOOPS=========================//
  97. void horde_default() { //repeatedly running horde encounters, no ability on my end
  98.  
  99.   x();
  100.   a();
  101.   delay(1600);
  102.   down();
  103.   a();
  104.   down();
  105.   a();
  106.   a();
  107.   delay(8000);
  108.   float startR, startG, startB;
  109.   measureLight(&startR, &startG, &startB);
  110.   delay(10800); //8500 for less laggy, bit of an issue, 9100 for laggier like vulpix
  111.   //10200 for oras
  112.   float endR, endG, endB;
  113.   measureLight(&endR, &endG, &endB);
  114.   if (!detectTransition(startR, startG, startB, endR, endG, endB)) {
  115.     digitalWrite(shinyLED, HIGH);
  116.     disengageServos();
  117.     while(1); //shiny omg!!!
  118.   }
  119.   left();
  120.   right();
  121.   a();
  122.   delay(8000); //6500 for lower levels, implement run away pokemon, 8500 for laaag
  123. }
  124.  
  125. void latias_default() { //latias run/re-encounter, my pokemon has no ability
  126.   a();
  127.   delay(1500);
  128.   a();
  129.   delay(500);
  130.   float startR, startG, startB;
  131.   measureLight(&startR, &startG, &startB);
  132.   delay(8500);
  133.   float endR, endG, endB;
  134.   measureLight(&endR, &endG, &endB);
  135.   if (!detectTransition(startR, startG, startB, endR, endG, endB)) {
  136.     digitalWrite(shinyLED, HIGH);
  137.     disengageServos();
  138.     while(1); //shiny omg!!!
  139.   }
  140.   left();
  141.   right();
  142.   a();
  143.   delay(6500);
  144.   a();
  145. }
  146.  
  147. void latios_default() { //latias run/re-encounter, my pokemon has no ability
  148.   a();
  149.   delay(1500);
  150.   a();
  151.   delay(500);
  152.   float startR, startG, startB;
  153.   measureLight(&startR, &startG, &startB);
  154.   delay(9000);
  155.   float endR, endG, endB;
  156.   measureLight(&endR, &endG, &endB);
  157.   if (!detectTransition(startR, startG, startB, endR, endG, endB)) {
  158.     digitalWrite(shinyLED, HIGH);
  159.     disengageServos();
  160.     while(1); //shiny omg!!!
  161.   }
  162.   left();
  163.   right();
  164.   a();
  165.   delay(7000);
  166.   a();
  167. }
  168.  
  169. //approx 13.84 hrs per shiny
  170. void hyperspace_default() { //hyperspace hole SR, no ability on my end or on enemy end
  171.   a();
  172.   delay(2250);
  173.   a();
  174.   delay(4250);
  175.   a();
  176.   delay(5000);
  177.   a();
  178.   delay(1000);
  179.   a();
  180.   delay(100);
  181.   a();
  182.   delay(100);
  183.   a();
  184.   delay(2000);
  185.   float startR, startG, startB;
  186.   measureLight(&startR, &startG, &startB);
  187.   delay(9400); //8500 for faster
  188.   float endR, endG, endB;
  189.   measureLight(&endR, &endG, &endB);
  190.   if (!detectTransition(startR, startG, startB, endR, endG, endB)) {
  191.     digitalWrite(shinyLED, HIGH);
  192.     disengageServos();
  193.     while(1); //shiny omg!!!
  194.   }
  195.   softReset();
  196.   delay(12000);
  197. }
  198.  
  199. void hyperspace_ability() { //reshiram hyperspace hole SR, no ability on my end
  200.   a();
  201.   delay(2250);
  202.   a();
  203.   delay(4250);
  204.   a();
  205.   delay(3250);
  206.   a();
  207.   delay(750);
  208.   a();
  209.   delay(50);
  210.   a();
  211.   delay(50);
  212.   a();
  213.   delay(2000);
  214.   float startR, startG, startB;
  215.   measureLight(&startR, &startG, &startB);
  216.   delay(11500);
  217.   float endR, endG, endB;
  218.   measureLight(&endR, &endG, &endB);
  219.   if (!detectTransition(startR, startG, startB, endR, endG, endB)) {
  220.     digitalWrite(shinyLED, HIGH);
  221.     disengageServos();
  222.     while(1); //shiny omg!!!
  223.   }
  224.   softReset();
  225.   delay(10000);
  226. }
  227.  
  228. //=========================UTILITY=========================//
  229. void waitForStart() {
  230.   while (digitalRead(switchDetector) != HIGH) {}
  231. }
  232.  
  233. void writeServo(Servo servo, int pin, int pos, int dTime = 250) {
  234.   servo.attach(pin);
  235.   servo.write(pos);
  236.   delay(dTime);
  237.   servo.detach(pin);
  238. }
  239.  
  240. void switchToTester() {
  241.   waitForStart();
  242.   a();
  243.   delay(2250);
  244.   a();
  245.   delay(4250);
  246.   a();
  247.   delay(4750);
  248.   x();
  249.   delay(1500);
  250.   a();
  251.   delay(1500);
  252.   a();
  253.   up();
  254.   up();
  255.   a();
  256.   right();
  257.   a();
  258.   b();
  259.   delay(2500);
  260.   up();
  261.   a();
  262.   delay(2000);
  263.   a();
  264.   delay(8000);
  265.   softReset();
  266. }
  267.  
  268. bool detectTransition(float r, float g, float b, float _r, float _g, float _b) {
  269.   Serial.println();
  270.   float diffR = abs(r-_r)/((r+_r)/2);
  271.   float diffG = abs(g-_g)/((g+_g)/2);
  272.   float diffB = abs(b-_b)/((b+_b)/2);
  273.   int total = 0;
  274.   if (diffR  >= .1) {total++;}
  275.   if (diffG  >= .1) {total++;}
  276.   if (diffB  >= .1) {total++;}
  277.   return total > 1;
  278. }
  279.  
  280. float measureLight(float *totalR, float *totalG, float *totalB) {
  281.   *totalR = 0;
  282.   *totalG = 0;
  283.   *totalB = 0;
  284.   for (int i = 0; i < NUM_MEASUREMENTS; i++) {
  285.     uint16_t clr, red, green, blue;
  286.     tcs.setInterrupt(false);      // turn on LED
  287.     delay(60);  // takes 50ms to read
  288.     tcs.getRawData(&red, &green, &blue, &clr);
  289.     tcs.setInterrupt(true);  // turn off LED
  290.     float r, g, b;
  291.     uint32_t sum = clr;
  292.     r = red; r /= sum;
  293.     g = green; g /= sum;
  294.     b = blue; b /= sum;
  295.     r *= 256; g *= 256; b *= 256;
  296.  
  297.     *totalR += r;
  298.     *totalG += g;
  299.     *totalB += b;
  300.  
  301.     if (DEBUG) {
  302.       Serial.print("R: "); Serial.print(r); Serial.print(" G: ");Serial.print(g); Serial.print(" B: "); Serial.print(b); Serial.println();
  303.     }
  304.   }
  305.   *totalR /= NUM_MEASUREMENTS;
  306.   *totalG /= NUM_MEASUREMENTS;
  307.   *totalB /= NUM_MEASUREMENTS;
  308.  
  309.   if (*totalR != *totalR) {*totalR = 0;}
  310.   if (*totalG != *totalG) {*totalG = 0;}
  311.   if (*totalB != *totalB) {*totalB = 0;}
  312.  
  313.   if (DEBUG) {
  314.       Serial.print("AvgR: "); Serial.print(*totalR); Serial.print(" AvgG: ");Serial.print(*totalG); Serial.print(" AvgB: "); Serial.print(*totalB); Serial.println();
  315.   }
  316. }
  317.  
  318. void detachUnused() {
  319.   resetServos();
  320.   if (!useAX) {servoAX.detach();}
  321.   if (!useBY) {servoBY.detach();}
  322.   if (!useUL) {servoUL.detach();}
  323.   if (!useDR) {servoDR.detach();}
  324.   if (!useStart) {servoStart.detach();}
  325. }
  326.  
  327. void softReset() {
  328.   useStart = true;
  329.   servoStart.write(posStart[1]);
  330.   delay(delayDefault);
  331.   servoStart.write(posStart[0]);
  332.   delay(delayDefault);
  333. }
  334.  
  335. void disengageServos() {
  336.   resetServos();
  337. //  if (useAX) {servoAX.detach();}
  338. //  if (useBY) {servoBY.detach();}
  339. //  if (useUL) {servoUL.detach();}
  340. //  if (useDR) {servoDR.detach();}
  341. //  if (useStart) {servoStart.detach();}
  342.   servoAX.detach();
  343.   servoBY.detach();
  344.   servoUL.detach();
  345.   servoDR.detach();
  346.   servoStart.detach();
  347.  
  348.   if (DEBUG) {
  349.     Serial.println("Shiny Found!"); Serial.print("Loop: "); Serial.println(numLoops);Serial.println();
  350.   }
  351. }
  352.  
  353. //=========================ABXY=========================//
  354. void b() {
  355.   useBY = true;
  356.   servoBY.write(posBY[2]);
  357.   delay(delayDefault);
  358.   servoBY.write(posBY[0]);
  359.   delay(delayDefault);
  360. }
  361.  
  362. void y() {
  363.   useBY = true;
  364.   servoBY.write(posBY[1]);
  365.   delay(delayDefault);
  366.   servoBY.write(posBY[0]);
  367.   delay(delayDefault);
  368. }
  369.  
  370. void x() {
  371.   useAX = true;
  372.   servoAX.write(posAX[2]);
  373.   delay(delayDefault);
  374.   servoAX.write(posAX[0]);
  375.   delay(delayDefault);
  376. }
  377.  
  378. void a() {
  379.   useAX = true;
  380.   servoAX.write(posAX[1]);
  381.   delay(delayDefault);
  382.   servoAX.write(posAX[0]);
  383.   delay(delayDefault);
  384. }
  385.  
  386. //=========================D PAD=========================//
  387. void left() {
  388.   useUL = true;
  389.   servoUL.write(posUL[2]);
  390.   delay(delayDefault);
  391.   servoUL.write(posUL[0]);
  392.   delay(delayDefault);
  393. }
  394.  
  395. void down() {
  396.   useDR = true;
  397.   servoDR.write(posDR[1]);
  398.   delay(delayDefault);
  399.   servoDR.write(posDR[0]);
  400.   delay(delayDefault);
  401. }
  402.  
  403. void right() {
  404.   useDR = true;
  405.   servoDR.attach();
  406.   servoDR.write(posDR[2]);
  407.   delay(delayDefault);
  408.   servoDR.write(posDR[0]);
  409.   delay(delayDefault);
  410.   servoDR.detach();
  411. }
  412.  
  413. void up() {
  414.   useUL = true;
  415.   servoUL.write(posUL[1]);
  416.   delay(delayDefault);
  417.   servoUL.write(posUL[0]);
  418.   delay(delayDefault);
  419. }
RAW Paste Data Copied