markruff

Arduino Robot to play Google Dinosaur Game

Apr 3rd, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo pusherServo;
  4.  
  5. const int backgroundPin = A0;
  6. const int obstaclePinLow = A1;
  7. const int obstaclePinHigh = A2;
  8. const int outputPin = 2;
  9.  
  10. const int runAngle=0;
  11. const int jumpAngle=20;  
  12.  
  13. // light sensor cutoff between light and dark
  14. int cutoffBackground;
  15. int cutoffLowLightBG;
  16. int cutoffHighLightBG;
  17. int cutoffLowDarkBG = 350; // does not need calibration
  18. int cutoffHighDarkBG = 350; // does not need calibration
  19.  
  20. int threshold = 30;
  21.  
  22. bool backgroundLight = false;
  23.  
  24. void setup() {
  25.   Serial.begin(9600);
  26.  
  27.   pusherServo.attach(9);
  28.  
  29.   pinMode(outputPin, OUTPUT);
  30.   digitalWrite(outputPin, LOW);
  31.  
  32.   // set threshold for light being on on sensor
  33.   // hold up to the monitor to get brightness calibrated
  34.  
  35.   Serial.print("Calibrate please\n");
  36.   Serial.print("Hold device so all sensors on bright part of screen\n");
  37.   delay(300);
  38.   Serial.print("Calibrating");
  39.   delay(1000);
  40.   Serial.print(".");
  41.   delay(1000);
  42.   Serial.print(".");
  43.   delay(1000);
  44.   Serial.print(".\n");
  45.  
  46.   // background cut off for light vs dark
  47.   cutoffBackground = analogRead(backgroundPin) - threshold;
  48.  
  49.   // obstacle cut off vs light screen
  50.   cutoffLowLightBG = analogRead(obstaclePinLow) - threshold;
  51.   cutoffHighLightBG = analogRead(obstaclePinHigh) - threshold;
  52.  
  53.   // obstacle cut off vs dark screen, manually set as this is easier to detect
  54.   // these declared earlier
  55.  
  56.   Serial.print("Calibration Complete\n\n");
  57. }
  58.  
  59. void loop() {
  60.   bool needJump = false;
  61.   digitalWrite(outputPin, LOW);
  62.  
  63.   bool lowLight = false;
  64.   bool highLight = false;
  65.    
  66.   int backgroundVal = analogRead(backgroundPin);
  67.   int lowVal = analogRead(obstaclePinLow);
  68.   int highVal = analogRead(obstaclePinHigh);
  69.  
  70.   // first deal with light background case
  71.   if (backgroundVal > cutoffBackground) {
  72.     if (backgroundLight == false){
  73.       delay(500);
  74.       backgroundLight = true;
  75.     }
  76.     if (lowVal > cutoffLowLightBG) lowLight = true;
  77.     if (highVal > cutoffHighLightBG) highLight = true;
  78.  
  79.     // if either obstacle areas appear dark we have to jump
  80.     if (!lowLight || !highLight) {
  81.       needJump = true;
  82.     }
  83.   } else { // now deal with dark background case
  84.     if (backgroundLight == true){
  85.       delay(500);
  86.       backgroundLight = false;
  87.     }
  88.     if (lowVal > cutoffLowDarkBG) lowLight = true;
  89.     if (highVal > cutoffHighDarkBG) highLight = true;
  90.  
  91.     // if either obstacle areas appear light we have to jump
  92.     if (lowLight || highLight) {
  93.       needJump = true;
  94.     }
  95.    
  96.   }
  97.     if (needJump) {
  98.     digitalWrite(outputPin, HIGH);
  99.     delay(10);
  100.     pusherServo.write(jumpAngle);
  101.     delay(200);
  102.     pusherServo.write(runAngle);
  103.     delay(25);
  104.  
  105.   }
  106. }
Add Comment
Please, Sign In to add comment