Premier Jet - Attente Fin Code

This commit is contained in:
Profouzors (LE) 2022-11-04 00:40:35 +01:00
parent 206a50a5fe
commit 79ab9547ff
2 changed files with 170 additions and 7 deletions

View File

@ -12,4 +12,6 @@
platform = atmelavr
board = pro8MHzatmega328
framework = arduino
lib_deps = adafruit/Adafruit NeoPixel@^1.10.6
lib_deps =
adafruit/Adafruit NeoPixel@^1.10.6
featherfly/SoftwareSerial@^1.0

View File

@ -1,19 +1,152 @@
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define CUIZZ_Color 1 // 0 - MASTER, 1 - Blanc, 2 - Rouge, 3 - Vert, 4 - Bleu, 5 - Jaune
#define CUIZZ_Interrupt 6
#define CUIZZ_PIN_Buzzer 2
#define CUIZZ_PIN_LED 4
#define CUIZZ_NB_LED 8
#define CUIZZ_SERIAL_TX 8
#define CUIZZ_SERIAL_RX 9
byte CUIZZ_GameMode; // 0 - Attente Init, 1 -
byte CUIZZ_GameState;
byte CUIZZ_Players[6];
byte CUIZZ_Score;
byte CUIZZ_MyID;
SoftwareSerial HC12(CUIZZ_SERIAL_TX,CUIZZ_SERIAL_RX);
Adafruit_NeoPixel CUIZZPixel(CUIZZ_NB_LED,CUIZZ_PIN_LED,NEO_GRB + NEO_KHZ800);
// Calcul Couleur RGB sur 1 Byte
void CUIZZ_Color_C2B(byte IDColor, byte nRed, byte nGreen, byte nBlue){
nRed = (IDColor >> 5) * 32;
nGreen = ((IDColor & 28) >> 2) * 32;
nBlue = (IDColor & 3) * 64;
}
byte CUIZZ_Color_B2C(byte nRed, byte nGreen, byte nBlue){
return ((nRed << 5) | (nGreen << 2) | nBlue);
}
//Envoi simple Message
void CUIZZ_SendMessage(byte TypeMessage, byte Destinataire){
char TheMessage[15] = "";
// Définition des Types de Messages
/*
1 - Send Lock Box
*/
// Send message
HC12.write(TheMessage,15);
Serial.write(TheMessage,15);
Serial.flush();
HC12.flush();
}
void CUIZZ_UpdateLED(unsigned long nDelay = 1){
byte CUIZZPixel_R[8];
byte CUIZZPixel_G[8];
byte CUIZZPixel_B[8];
// OFF
CUIZZPixel.clear();
switch (CUIZZ_GameMode)
{
case 0: // Game Mode - Attente Initialisation
if(CUIZZ_Score < 20 ){
// LED en ROUGE
for (byte i = 0; i < 8; i++)
{
CUIZZPixel_R[i] = 255;
CUIZZPixel_G[i] = 0;
CUIZZPixel_B[i] = 0;
}
}
else{
if(CUIZZ_Score < 40 ){
// LED en VERT
for (byte i = 0; i < 8; i++)
{
CUIZZPixel_R[i] = 0;
CUIZZPixel_G[i] = 255;
CUIZZPixel_B[i] = 0;
}
}
else{
// LED en Bleu
for (byte i = 0; i < 8; i++)
{
CUIZZPixel_R[i] = 0;
CUIZZPixel_G[i] = 0;
CUIZZPixel_B[i] = 255;
}
}
}
break;
default:
break;
}
// ON
for(byte i=0; i<CUIZZ_NB_LED; i++) {
CUIZZPixel.setPixelColor(i, CUIZZPixel.Color(CUIZZPixel_R[i], CUIZZPixel_G[i], CUIZZPixel_B[i]));
}
CUIZZPixel.show();
delay(nDelay);
}
// Réception Message
void CUIZZ_GetMessage(){
char TheMessage[15] = "";
bool bMessageIN = false;
int nNB_Bytes;
nNB_Bytes = HC12.available();
if(nNB_Bytes > 0){
if(nNB_Bytes == 15){
for(int i=0;i<nNB_Bytes;i++){
TheMessage[i] = HC12.read();
}
bMessageIN = true;
}
}
else{
nNB_Bytes = Serial.available();
if(nNB_Bytes > 0){
if(nNB_Bytes == 15){
for(int i=0;i<nNB_Bytes;i++){
TheMessage[i] = Serial.read();
}
bMessageIN = true;
}
}
}
if(bMessageIN){
// Traitement du Message
if(TheMessage[1] == 0 || TheMessage[1] == CUIZZ_MyID){
// Message for me !
switch (TheMessage[2]) // Récupération du Type de Message
{
case 1 :
/* code */
break;
default:
break;
}
}
}
}
// Interrupt BUZZER
void CUIZZ_Buzzer(){
// Buzzer Déveirrouillé
if(CUIZZ_GameState == 1){
}
}
void setup() {
@ -21,10 +154,38 @@ void setup() {
CUIZZ_GameState = 0;
CUIZZ_GameMode = 0;
CUIZZ_Score = 0;
// Init Players
CUIZZ_Players[0] = 0; CUIZZ_Players[1] = 0; CUIZZ_Players[2] = 0; CUIZZ_Players[3] = 0; CUIZZ_Players[4] = 0; CUIZZ_Players[5] = 0;
// Init HC12
HC12.begin(9600);
Serial.begin(9600);
// Init Pixels
CUIZZPixel.begin();
// INIT Pin Buzzer
pinMode(CUIZZ_PIN_Buzzer,INPUT_PULLUP);
if(CUIZZ_PIN_Buzzer == 2 || CUIZZ_PIN_Buzzer == 3)
{
attachInterrupt(digitalPinToInterrupt(CUIZZ_PIN_Buzzer),CUIZZ_Buzzer,CHANGE);
}
// Attente MASTER
while(CUIZZ_GameMode == 0){
CUIZZ_GetMessage();
if(CUIZZ_Score > 60){
CUIZZ_Score = 0;
}
CUIZZ_Score++;
CUIZZ_UpdateLED(50);
}
}
void loop() {
// put your main code here, to run repeatedly:
// Lecture Message
CUIZZ_GetMessage();
if (digitalRead(CUIZZ_PIN_Buzzer) == LOW) {
CUIZZ_Buzzer();
}
}