|
楼主 |
发表于 2019-4-29 23:17
|
显示全部楼层
源码在此:
[mw_shl_code=arduino,true]#include <Servo.h>
#include <IRremote.h>
#include "TM1637.h"
#define pinIR 2
#define pinIndicatorLed 3
#define pinLaser 4
#define pinServoH 7
#define pinServoV 8
#define picLcdCLK 9
#define pinLcdDIO 10
Servo servoH;
Servo servoV;
IRrecv irrecv(pinIR);
decode_results results;
TM1637 tm1637(picLcdCLK, pinLcdDIO);
unsigned int rangeH = 25;
unsigned int rangeH_pre;
unsigned int rangeV = 20;
unsigned int rangeV_pre;
unsigned int rangeT = 60;
unsigned int rangeT_pre;
unsigned int limitH = 75;
unsigned int limitV = 35;
unsigned int midH = 80;
unsigned int midV = 40;
unsigned int heartBeat = 5;
bool preButton ;
bool playing = false;
unsigned long lastDisplay = 0;
unsigned long lastButtonChange = 0;
unsigned long gameStartTime = 0;
unsigned int buttonWaitDelay = 2000;
unsigned long lastCheckTime = 0;
bool displayCleared = false;
unsigned int tmp_second;
int gameTimeRemain = 0;
bool timerPoint = false;
void setup() {
pinMode(pinLaser, OUTPUT);
pinMode(pinIndicatorLed, OUTPUT);
digitalWrite(pinLaser, HIGH);
randomSeed(analogRead(0));
irrecv.enableIRIn();
// welcome
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);
hi();
// debug
// Serial.begin(9600);
}
void hi() {
tm1637.display(3, 0);
delay(400);
tm1637.display(2, 0);
tm1637.display(3, 0);
delay(400);
tm1637.point(1);
tm1637.display(1, 0);
tm1637.display(2, 0);
tm1637.display(3, 0);
delay(400);
tm1637.display(0, 0);
tm1637.display(1, 0);
tm1637.display(2, 0);
tm1637.display(3, 0);
delay(400);
tm1637.clearDisplay();
delay(200);
tm1637.point(1);
tm1637.display(0, 0);
tm1637.display(1, 0);
tm1637.display(2, 0);
tm1637.display(3, 0);
delay(200);
tm1637.clearDisplay();
delay(200);
tm1637.point(1);
tm1637.display(0, 0);
tm1637.display(1, 0);
tm1637.display(2, 0);
tm1637.display(3, 0);
delay(1000);
servoH.attach(pinServoH);
servoV.attach(pinServoV);
servoMigrateOne(servoH, 90, 15);
servoMigrateOne(servoV, 10, 15);
servoH.detach();
servoV.detach();
}
void servoMigrateOne(Servo &myServo, int newPos, int wait) {
int pos = myServo.read();
if (pos < newPos) {
for (int i = pos; i < newPos; i++) {
myServo.write(i);
delay2(wait);
}
} else {
for (int i = pos; i > newPos; i--) {
myServo.write(i);
delay2(wait);
}
}
flashOnboardLed();
}
void servoMigrateHV(int h, int v, int wait) {
int posH = servoH.read();
int posV = servoV.read();
int maxSteps = max(abs(h - posH), abs(v - posV));
for (int i = 0; i <= maxSteps; i++) {
if (posH < h) {
servoH.write(posH + i * (h - posH) / maxSteps);
delay2(wait);
} else {
servoH.write(posH - i * (posH - h) / maxSteps);
delay2(wait);
}
if (posV < v) {
servoV.write(posV + i * (v - posV) / maxSteps);
delay2(wait);
} else {
servoV.write(posV - i * (posV - v) / maxSteps);
delay2(wait);
}
}
flashOnboardLed();
}
void flashLaser() {
digitalWrite(pinLaser, HIGH);
delay2(1000);
for (int i = 0; i <= 10; i++) {
digitalWrite(pinLaser, LOW);
delay2(25);
digitalWrite(pinLaser, HIGH);
delay2(50);
}
digitalWrite(pinLaser, LOW);
}
void loop() {
if (irrecv.decode(&results)) {
// Serial.println(results.value, HEX);
switch (results.value) {
case 0xFF38C7:
// ok
if (playing) {
pauseGame();
} else {
delay(1000);
servoH.attach(pinServoH);
servoV.attach(pinServoV);
playing = true;
gameStartTime = millis();
digitalWrite(pinLaser, LOW);
}
break;
case 0xFFA25D:
// 1
rangeT = 10;
break;
case 0xFF629D :
// 2
rangeT = 20;
break;
case 0xFFE21D :
// 3
rangeT = 30;
break;
case 0xFF22DD :
// 4
rangeT = 40;
break;
case 0xFF02FD :
// 5
rangeT = 50;
break;
case 0xFFC23D :
// 6
rangeT = 60;
break;
case 0xFFE01F :
// 7
rangeT = 70;
break;
case 0xFFA857 :
// 8
rangeT = 80;
break;
case 0xFF906F :
// 9
rangeT = 90;
break;
case 0xFF18E7 :
//up
if (rangeV + 5 <= limitV) {
rangeV += 5;
}
break;
case 0xFF10EF :
//left
if (rangeH >= 5) {
rangeH -= 5;
}
break;
case 0xFF5AA5 :
//right
if (rangeH + 5 <= limitH) {
rangeH += 5;
}
break;
case 0xFF4AB5 :
//down
if (rangeV >= 5) {
rangeV -= 5;
}
break;
}
irrecv.resume(); // Receive the next value
}
// check range
if (rangeH != rangeH_pre) {
rangeH_pre = rangeH;
// display new H value
tm1637.point(1);
tm1637.display(0, 21);
tm1637.display(1, 16);
tm1637.display(2, rangeH / 10);
tm1637.display(3, rangeH % 10);
lastDisplay = millis();
displayCleared = false;
}
if (rangeV != rangeV_pre) {
rangeV_pre = rangeV;
tm1637.point(1);
tm1637.display(0, 21);
tm1637.display(1, 18);
tm1637.display(2, rangeV / 10);
tm1637.display(3, rangeV % 10);
lastDisplay = millis();
displayCleared = false;
}
if (rangeT != rangeT_pre) {
rangeT_pre = rangeT;
// display new T value
tm1637.point(1);
tm1637.display(0, 20);
tm1637.display(1, rangeT / 60);
tmp_second = rangeT - 60 * (rangeT / 60);
tm1637.display(2, tmp_second / 10);
tm1637.display(3, tmp_second % 10);
lastDisplay = millis();
displayCleared = false;
}
// clear display
if (millis() - lastDisplay > 2000 && !displayCleared) {
//tm1637.point(0);
//tm1637.clearDisplay();
displayCleared = true;
}
if (playing) {
playGame();
tasks();
if (gameTimeRemain <= 0) {
// stop game
pauseGame();
}
}
}
void playGame() {
servoMigrateHV(random(midH - rangeH, midH + rangeH), random(midV - rangeV, midV + rangeV), random(10, 30));
if (random(15) <= 1) {
flashLaser();
}
}
void pauseGame() {
// debug
digitalWrite(pinLaser, HIGH);
// Serial.println("sleep");
delay(1000);
servoMigrateOne(servoH,90,15);
servoMigrateOne(servoV,30,15);
delay(1000);
servoH.detach();
servoV.detach();
playing = false;
tm1637.point(1);
tm1637.display(0, 0);
tm1637.display(1, 0);
tm1637.display(2, 0);
tm1637.display(3, 0);
}
void flashOnboardLed() {
digitalWrite(pinIndicatorLed, HIGH);
delay(15);
digitalWrite(pinIndicatorLed, LOW);
}
void displayTimer() {
gameTimeRemain = (rangeT_pre - (millis() - gameStartTime) / 1000) ;
if (gameTimeRemain < 0) {
gameTimeRemain = 0;
}
if (!displayCleared) {
return;
}
timerPoint = !timerPoint;
tm1637.point(timerPoint);
tm1637.display(0, 21);
tm1637.display(1, gameTimeRemain / 60);
tmp_second = gameTimeRemain - 60 * (gameTimeRemain / 60);
tm1637.display(2, tmp_second / 10);
tm1637.display(3, tmp_second % 10);
}
void tasks() {
if (millis() - lastCheckTime >= 500) {
lastCheckTime = millis();
displayTimer();
}
}
void delay2(int delayMs) {
int loops = delayMs / heartBeat;
for (int i = 0; i <= loops; i++) {
tasks();
delay(heartBeat);
}
}
[/mw_shl_code]
|
|