ENIB 2022 - groupe A : The Fastest

De Les Fabriques du Ponant
Révision datée du 14 janvier 2022 à 14:22 par Théoooooo (discussion | contributions) (photo de l'équipe)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à : navigation, rechercher

photo de l'équipe

L'équipe The Fastest.jpg
1 contre 1
Cablage
The Fastest

Que fait ce projet ?

Jeu de réflexe en 1 contre 1. Une led s'allume, il faut être le premier à appuyer sur son bouton !

Liste des composants

  • Arduino (UNO/NANO)
  • Afficheur LCD (GROVE RGB backlight)
  • LED RGB
  • Résistances 10k x2
  • Résistances 220 x3
  • Boutons poussoirs x2
  • Breadboard
  • Buzzer
  • Bois

Code

#include <Wire.h>
#include "rgb_lcd.h"
#include "pitches.h"

rgb_lcd lcd;

const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

const byte COLOR_BLACK = 0b000;
const byte COLOR_RED = 0b100;
const byte COLOR_GREEN = 0b010;
const byte COLOR_BLUE = 0b001;
const byte COLOR_MAGENTA = 0b101;
const byte COLOR_CYAN = 0b011;
const byte COLOR_YELLOW = 0b110;
const byte COLOR_WHITE = 0b111;

int buttonStateP1;
int buttonStateP2;
bool p1Done;
bool p2Done;
float startTime;
float winningTime;
float finalTimeP1;
float finalTimeP2;
float endTimeP1;
float endTimeP2;
int scoreP1 = 0;
int scoreP2 = 0;
int ranDelay;

/* Broches */
const byte PIN_LED_R = 9;
const byte PIN_LED_G = 10;
const byte PIN_LED_B = 11;
const byte PIN_BUTTON_1 = 3;
const byte PIN_BUTTON_2 = 4;
const byte BUZZER = 8;

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

int songLength = sizeof(melody)/sizeof(melody[0]);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);

  // Initialise les broches
  pinMode(PIN_LED_R, OUTPUT);
  pinMode(PIN_LED_G, OUTPUT);
  pinMode(PIN_LED_B, OUTPUT);
  displayColor(COLOR_BLACK);

  pinMode(PIN_BUTTON_1, INPUT);
  pinMode(PIN_BUTTON_2, INPUT);

  pinMode(BUZZER, OUTPUT);

  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

void loop() {
  lcd.setRGB(200,200,200);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Push the button");
  lcd.setCursor(0, 1);
  lcd.print("to start game !");
  
  while ((digitalRead(PIN_BUTTON_2)== 0)&&(digitalRead(PIN_BUTTON_1)== 0)) {
  }
  lcd.clear();
  displayColorPWM(255,0,0);
  lcd.setCursor(0, 0);
  lcd.print("Get Ready!");
  delay(1500);
  displayColorPWM(255, 90,0);
  lcd.setCursor(0, 1);
  lcd.print("Get Set!");
  delay(500);
  ranDelay = random(2000, 5000);
  delay(ranDelay);
  Serial.println("Go!");
  
  startTime = millis();
  displayColor(COLOR_GREEN);
 
  while(p1Done == false || p2Done == false){
    buttonStateP1 = digitalRead(PIN_BUTTON_1);
    buttonStateP2 = digitalRead(PIN_BUTTON_2);
    // Listen for Player 1 button to be pressed and set Player 1 as done.
    if (buttonStateP1 == HIGH && p1Done == false) {
      endTimeP1 = millis();
      p1Done = true;     
    }
    // Listen for Player 2 button to be pressed and set Player 2 as done.
    if (buttonStateP2 == HIGH && p2Done == false) {
      endTimeP2 = millis();
      p2Done = true;
    }
  }

  displayColor(COLOR_BLACK); // Turn off the game LED
  finalTimeP1 = (endTimeP1 - startTime); //Calculate how long it took Player to push their button
  finalTimeP2 = (endTimeP2 - startTime); //Calculate how long it took Player to push their button

  if (endTimeP1 < endTimeP2){ // Run if Player 1 won the round
    lcd.setRGB(255,0,0);
    winningTime = (endTimeP2 - startTime) - (endTimeP1 - startTime);
    scoreP1 = scoreP1 + 1;
    lcd.clear();
    lcd.setCursor(5,0);
    lcd.print(scoreP1);
    lcd.print(" - ");
    lcd.print(scoreP2);
    lcd.setCursor(0, 1);
    lcd.print("P1 won by: ");
    lcd.print(winningTime/1000);
    delay(1000);
  }
  else{
    lcd.setRGB(0, 0, 255);
    winningTime = (endTimeP1 - startTime) - (endTimeP2 - startTime);
    scoreP2 = scoreP2 + 1;
    lcd.clear();
    lcd.setCursor(5,0);
    lcd.print(scoreP1);
    lcd.print(" - ");
    lcd.print(scoreP2);
    lcd.setCursor(0,1);
    lcd.print("P2 won by: ");
    lcd.print(winningTime/1000);
    delay(1000);
  }
  
  delay(3500);

  // Reset all variables to restart the game
  buttonStateP1 = 0;
  buttonStateP2 = 0;  
  p1Done = false;
  p2Done = false;
  ranDelay = 0;
  startTime = 0;
  endTimeP1 = 0;
  endTimeP2 = 0;
  finalTimeP1 = 0;
  finalTimeP2 = 0;
  winningTime = 0;
  delay(2000);
}
  

/** Affiche une couleur */
void displayColor(byte color) {
  digitalWrite(PIN_LED_R, !bitRead(color, 2));
  digitalWrite(PIN_LED_G, !bitRead(color, 1));
  digitalWrite(PIN_LED_B, !bitRead(color, 0));
}

void displayColorPWM(int pwmRouge, int pwmVert, int pwmBleu) { // reçoit valeur 0-255 par couleur

  analogWrite(PIN_LED_R, 255-pwmRouge);
  analogWrite(PIN_LED_G, 255-pwmVert);
  analogWrite(PIN_LED_B, 255-pwmBleu);
}

Catégories