#define VERT_PIN A0
#define HORZ_PIN A1
#include <Servo.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Alert.h"
Servo leftServo; // create servo object to control a servo
Servo rightServo;
int leftPos = 0; // variable to store the servo position
int rightPos = 0;
bool buttonPressed = false;
bool leftServoOn = false;
bool rightServoOn = false;
bool interrupted = false;
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int birdPosX = 120;
int birdPosY = 160;
int upwardMomentum = 0;
// 240 by 320 - use bitmaps
void setup() {
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
// attach to pins suitable for PWM waves
leftServo.attach(3);
rightServo.attach(6);
tft.begin();
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello, TFT!");
tft.fillScreen(ILI9341_WHITE);
}
void hitLeft() {
if (birdPosX <= (320 - 128)) {
birdPosX += 10;
}
upwardMomentum -= 15;
}
// goes left
void hitRight() {
if (birdPosX > 10) {
birdPosX -= 10;
}
upwardMomentum -= 15;
}
void adjustY() {
if (birdPosY >= 320 - 64) {
// hits floor: reverse
upwardMomentum = -upwardMomentum;
}
if (birdPosY <= 64) {
// hits ceiling: reverse
upwardMomentum = -upwardMomentum;
}
// apply upward momentum
// apply gravity:
upwardMomentum += 10;
// modify position
birdPosY += upwardMomentum;
}
// creates signal via buttonPressed and the appropriate button
void detectPad() {
int vert = analogRead(VERT_PIN);
int horz = analogRead(HORZ_PIN);
if (horz == 1023 && vert == 512) {
//left
// servo1
buttonPressed = true;
leftServoOn = true;
}
if (horz == 0 && vert == 512) {
// right
// servo1
buttonPressed = true;
rightServoOn = true;
}
}
void draw() {
for(uint8_t r=0; r<1; r++) {
tft.setRotation(r);
tft.drawRGBBitmap(
birdPosX,
birdPosY,
alertBitmap,
alertWidth,
alertHeight);
delay(1); // Allow ESP8266 to handle watchdog & WiFi stuff
}
}
void loop() {
detectPad();
if (buttonPressed && leftServoOn) {
// consume signal
buttonPressed = false;
leftServoOn = false;
for (leftPos = 0; leftPos <= 45; leftPos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
leftServo.write(leftPos); // tell servo to go to position in variable 'pos'
delay(5);
}
tft.fillRect(birdPosX, birdPosY, alertWidth, alertHeight, ILI9341_WHITE);
hitLeft();
}
else if (buttonPressed && rightServoOn) {
// consume signal
buttonPressed = false;
rightServoOn = false;
for (rightPos = 0; rightPos <= 45; rightPos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
rightServo.write(rightPos); // tell servo to go to position in variable 'pos'
delay(5);
// check pad every loop to break, get new instruction
}
tft.fillRect(birdPosX, birdPosY, alertWidth, alertHeight, ILI9341_WHITE);
hitRight();
}
// adjustY();
draw();
}