Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MiniTimerOne.h>
- #define BUTTON_1 2
- #define BUTTON_2 3
- #define BUTTON_3 4
- #define BUTTON_4 5
- #define LED_1 8
- #define LED_2 9
- #define LED_3 10
- #define LED_4 7
- #define RED_LED 11
- #define POT A0
- #define L_1 127
- #define L_2 255
- #define L_3 383
- #define L_4 511
- #define L_5 639
- #define L_6 767
- #define L_7 895
- #define L_8 1023
- #define MULTIPLIER 8
- #define MIN 0
- #define MAX 3
- #define MICROS 1000000
- int state_1;
- int state_2;
- int state_3;
- int state_4;
- int fadeAmount;
- int currentIntensity;
- int gameOver;
- int start;
- int count;
- float tMin;
- int leds[4];
- int currPos;
- int isStop;
- //Function for game initialization
- void initialize();
- //Function used to fade the red led
- void ledFading();
- //
- void buttonState();
- float setDifficulty();
- void generateRandomPos();
- void setGameOver();
- void setup() {
- Serial.begin(9600);
- //Input setup
- pinMode(BUTTON_1, INPUT);
- pinMode(BUTTON_2, INPUT);
- pinMode(BUTTON_3, INPUT);
- pinMode(BUTTON_4, INPUT);
- pinMode(POT, INPUT);
- //Output setup
- pinMode(LED_1, OUTPUT);
- pinMode(LED_2, OUTPUT);
- pinMode(LED_3, OUTPUT);
- pinMode(LED_4, OUTPUT);
- pinMode(RED_LED, OUTPUT);
- leds[0] = LED_1;
- leds[1] = LED_2;
- leds[2] = LED_3;
- leds[3] = LED_4;
- MiniTimer1.init();
- MiniTimer1.attachInterrupt(setGameOver);
- //Game initialization
- initialize();
- }
- void loop() {
- buttonState();
- if(start == 1) {
- if(state_1 == LOW) {
- ledFading();
- } else if(state_1 == HIGH) {
- digitalWrite(RED_LED, LOW);
- randomSeed(millis());
- currPos = random(MIN,MAX + 1);
- Serial.println(currPos);
- start = 0;
- tMin = setDifficulty();
- MiniTimer1.setPeriod(tMin);
- }
- } else if(start == 0) {
- if(isStop == 0) {
- MiniTimer1.setPeriod(tMin);
- MiniTimer1.start();
- digitalWrite(leds[currPos], HIGH);
- isStop = 1;
- } else if (isStop == 1) {
- //generateRandomPos();
- if(gameOver == 1) {
- MiniTimer1.stop(); //da mettere nel controllo bottone
- Serial.println("Game over");
- digitalWrite(RED_LED, HIGH);
- delay(2000);
- digitalWrite(RED_LED, LOW);
- initialize();
- }
- }
- }
- }
- void initialize() {
- gameOver = 0;
- start = 1;
- currentIntensity = 0;
- fadeAmount = 5;
- isStop = 0;
- Serial.println("Welcome to the Track the LED fly game. Press key T1 to start");
- }
- void ledFading() {
- analogWrite(RED_LED, currentIntensity);
- currentIntensity += fadeAmount;
- if(currentIntensity == 0 || currentIntensity == 255) {
- fadeAmount = -fadeAmount;
- }
- delay(20);
- }
- void buttonState() {
- state_1 = digitalRead(BUTTON_1);
- state_2 = digitalRead(BUTTON_2);
- state_3 = digitalRead(BUTTON_3);
- state_4 = digitalRead(BUTTON_4);
- }
- float setDifficulty() {
- float value = analogRead(POT);
- Serial.println(value);
- if(value <= L_1) {
- Serial.println("L 1");
- return 60 * MICROS;
- }
- if(value > L_1 && value <= L_2) {
- Serial.println("L 2");
- return 55 * MICROS;
- }
- if(value > L_2 && value <= L_3) {
- Serial.println("L 3");
- return 50 * MICROS;
- }
- if(value > L_3 && value <= L_4) {
- Serial.println("L 4");
- return 45 * MICROS;
- }
- if(value > L_4 && value <= L_5) {
- Serial.println("L 5");
- return 40 * MICROS;
- }
- if(value > L_5 && value <= L_6) {
- Serial.println("L 6");
- return 35 * MICROS;
- }
- if(value > L_6 && value <= L_7) {
- Serial.println("L 7");
- return 30 * MICROS;
- }
- if(value > L_7 && value <= L_8) {
- Serial.println("L 8");
- return 25 * MICROS;
- }
- return 0;
- }
- void generateRandomPos() {
- randomSeed(millis());
- int dir = random(0,2);
- Serial.print("DIRECTION: ");
- Serial.println(dir);
- if(dir == 0) {
- if(currPos == MIN) {
- currPos = MAX;
- } else {
- currPos--;
- }
- } else if(dir == 1) {
- if(currPos == MAX) {
- currPos = MIN;
- } else {
- currPos++;
- }
- }
- }
- void setGameOver() {
- digitalWrite(leds[currPos], LOW);
- gameOver = 1;
- isStop = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement