Prise en main de l’arduino : la station météo

Pour finaliser ce projet on démarre par le commencement :

La carte Arduino Uno

et son interface de programmation

#include

void setup(){

}
void loop(){

}

prise en main avec quelques leds et boutons, puis l’écran LCD, puis le capteurs de température LM35, puis le DHT22, un peu de pression atmosphérique avec le BMP280. On programme tout cela et …

ce n’est pas fini car les plus courageux pourront apporter pleins de raffinements et un emballage super design.

 

Le jeu de 421 électronique

Un aperçu de ce que pourrait donner ce jeu de 421, avec un affichage lumineux dans des dalles d’Altuglas gravées.

L’affichage de la valeur d’un dé se fait avec seulement 4 « allumages » de led :

  1. une led centrale
  2. deux leds au milieu de chaque cotés
  3. une led en bas à gauche et une led en haut à droite, groupées. (+ le 1 bien sur)
  4. une led en haut à gauche et une led en bas à droite, groupées. (+ le 3 )
  5. = le 4 + le 3 et le 1
  6. = le 2 + le 3 et le 4

Un câblage qui utilise toutes les entrées-sorties de la carte ! :

  • 4 X 3 sorties leds pour les 3 dés
  • 4 entrées pour les boutons
  • 2 entrées pour l’affichage 7 segments

Un aperçu du programme arduino (à améliorer certes !)

#include "SevenSegmentTM1637.h"

const int D1_1 = 2; // the number of the pushbutton pin
const int D1_2 = 3; // the number of the pushbutton pin
const int D1_3 = 4; // the number of the pushbutton pin
const int D1_4 = 5; // the number of the pushbutton pin
const int D2_1 = 6; // the number of the pushbutton pin
const int D2_2 = 7; // the number of the pushbutton pin
const int D2_3 = 8; // the number of the pushbutton pin
const int D2_4 = 9; // the number of the pushbutton pin
const int D3_1 = 10; // the number of the pushbutton pin
const int D3_2 = 11; // the number of the pushbutton pin
const int D3_3 = 12; // the number of the pushbutton pin
const int D3_4 = 13; // the number of the pushbutton pin

const int Bt_1 = 15; // the number of the pushbutton pin
const int Bt_2 = 16; // the number of the pushbutton pin
const int Bt_3 = 17; // the number of the pushbutton pin
const int Bt_G = 14; // the number of the pushbutton pin
const byte PIN_CLK = 18; // define CLK pin (any digital pin)
const byte PIN_DIO = 19; // define DIO pin (any digital pin)

int Temps = 2000;
bool Bt_GState = 0;
bool Bt_1State = 0;
bool Bt_2State = 0;
bool Bt_3State = 0;
bool D1 = 0;
bool D2 = 0;
bool D3 = 0;
bool Jeu = 0; // jeu = 0 : charge ; jeu = 1 : decharge
bool Joueur = 0 ; //joueur1 = 0 : joueur2 = 1
bool Debut = 0 ; // debut = 0 joueur1 debute sinon joueur2
int Val = 0;
int Val1 = 0;
int Val2 = 0;
int Score = 0;
int Score1 = 0 ;
int Score2 = 0 ;
int Jetons1 = 0 ;
int Jetons2 = 0 ;
int Lance = 0; // si jeu = 1 lance = 3
int Pot = 21;
int Temp ;
String Seg;
 String ScoreT;
int Tirage[3];

SevenSegmentTM1637 display(PIN_CLK, PIN_DIO);

void setup() {
 Serial.begin(9600); // initializes the Serial connection @ 9600 baud
 display.begin(); // initializes the display
 display.setBacklight(100); // set the brightness to 100 %
 display.print("INIT"); // display INIT on the display


 pinMode(14, INPUT_PULLUP);
 pinMode(15, INPUT_PULLUP);
 pinMode(16, INPUT_PULLUP);
 pinMode(17, INPUT_PULLUP);
 for (int thisPin = 2; thisPin < 14; thisPin++) {
 pinMode(thisPin, OUTPUT);
 }
 randomSeed(analogRead(7));
 delay(Temps); // wait 1000 ms
 Efface();
 Affiche(1, 4);
 Affiche(2, 2);
 Affiche(3, 1);
 delay(Temps);
 Efface();
}

void loop() {

while (Pot < 22 && Pot > 0) { // phase de charge
 Jeu = false;
 
 if(!Joueur){
 Efface();
// Serial.print("Joueur : ");
// Serial.println(Joueur + 1);
 while(!Bt_GState && !Joueur) {
 AfficheJoueur(); 
 if (digitalRead(Bt_G) == LOW){ Bt_GState = 1;
 //Serial.println("Prêt au Lancement des dés");
 }
 LanceCharge();
 }
 }
 
 if(Joueur){
 Efface();
// Serial.print("Joueur : ");
// Serial.println(Joueur + 1);
 while(!Bt_GState && Joueur) {
 AfficheJoueur();
 if (digitalRead(Bt_G) == LOW){ Bt_GState = 1;
 Serial.println("Prêt au Lancement des dés J2");
 }
 LanceCharge();
 }
 }
 }
 if (Pot == 0 && Jeu == false) { // phase de decharge
 if (Jetons1 > Jetons2) {
 int tmp = Jetons1;
 Jetons1 = Jetons2;
 Jetons2 = tmp;
 }
 Jeu = true;
 display.clear();
 display.setColonOn(0);
 display.print("DECHARGE"); 
 Joueur = 0;
 AfficheJoueur();
// Serial.print("Passage en phase decharge : ");
// Serial.println(Jeu);
 Efface();
 }

while (Jetons1 > 0 && Jetons2 > 0 && Jeu == true) {
 
 if(!Joueur){
 
 while(!Bt_GState && !Joueur) {
 AfficheJoueur(); 
 Efface();
 if (digitalRead(Bt_G) == LOW){ Bt_GState = 1;Serial.println("Prêt au Lancement des dés");}
 
 if (Lance != 0) {
 if (digitalRead(Bt_1) == LOW){ Bt_1State = !Bt_1State;Serial.println("Verouillage D1");}
 if (digitalRead(Bt_2) == LOW){ Bt_2State = !Bt_2State;Serial.println("Verouillage D2");}
 if (digitalRead(Bt_3) == LOW){ Bt_3State = !Bt_3State;Serial.println("Verouillage D3");}
 
 if (Bt_1State){ D1=1; 
 Affiche(1, Tirage[0]);}
 if (Bt_2State){ D2=1; 
 Affiche(2, Tirage[1]);}
 if (Bt_3State){ D3=1; 
 Affiche(3, Tirage[2]);}
 }
 
 LanceDecharge();
 
}
 }
 if(Joueur){
 
 while(!Bt_GState && Joueur) {
 AfficheJoueur(); 
 Efface();
 if (digitalRead(Bt_G) == LOW){ Bt_GState = 1;Serial.println("Prêt au Lancement des dés");}

if (Lance != 0) {
 if (digitalRead(Bt_1) == LOW){ Bt_1State = !Bt_1State; }
 if (digitalRead(Bt_2) == LOW){ Bt_2State = !Bt_2State; }
 if (digitalRead(Bt_3) == LOW){ Bt_3State = !Bt_3State; }
 
 if (Bt_1State){ D1=1; 
 Affiche(1, Tirage[0]);}
 if (Bt_2State){ D2=1; 
 Affiche(2, Tirage[1]);}
 if (Bt_3State){ D3=1; 
 Affiche(3, Tirage[2]);}
 }


LanceDecharge();
}
}
 }
 if(Pot==0 && Jetons1==0){display.clear();
 display.setColonOn(0);
 display.print("JOUEUR 1 GAGNE"); 
 if (digitalRead(Bt_G) == LOW){
 Jeu = 0; Jetons1 = Jetons2 = 0 ; Pot = 21; display.clear(); display.print("Reset");}
 }
 if(Pot==0 && Jetons2==0){display.clear();
 display.setColonOn(0);
 display.print("JOUEUR 2 GAGNE"); 
 if (digitalRead(Bt_G) == LOW){
 Jeu = 0; Jetons1 = Jetons2 = 0 ; Pot = 21; display.clear(); display.print("Reset");}}
}
void Efface() {
 //if(!D1) {
 digitalWrite (D1_1, LOW);
 digitalWrite (D1_2, LOW);
 digitalWrite (D1_3, LOW);
 digitalWrite (D1_4, LOW);
 // }
 // if(!D2) {
 digitalWrite (D2_1, LOW);
 digitalWrite (D2_2, LOW);
 digitalWrite (D2_3, LOW);
 digitalWrite (D2_4, LOW);
 //}
 //if(!D3) {
 digitalWrite (D3_1, LOW);
 digitalWrite (D3_2, LOW);
 digitalWrite (D3_3, LOW);
 digitalWrite (D3_4, LOW);
 //}
}
void LanceCharge() {
 
 if (Bt_GState == 1 && Jeu == false) {
 Serial.println("Lancement des dés");
 //Efface();
 Tirage[0] = random(1, 7); Tirage[1] = random(1, 7); Tirage[2] = random(1, 7);

Tri();
 
// Serial.print("Tirage : ");
// Serial.print(Tirage[0]);
// Serial.print(Tirage[1]);
// Serial.println(Tirage[2]);

Affiche(1, Tirage[0]);
 Affiche(2, Tirage[1]);
 Affiche(3, Tirage[2]);
 delay (Temps);


CalculScore();
// Serial.println("Calcul score");
 if(Joueur){ CalculJetons();
 AfficheJetons();
 }
 Joueur = !Joueur;
// Serial.print("Passage au joueur : ");
// Serial.println(Joueur + 1);
 Bt_GState = 0; //Serial.println("Remise à zéro du lancement");
 }
}
void LanceDecharge() {
 
 if (Bt_GState == 1 && Jeu == true) {
 Lance += 1;
// Serial.print("N° lancement: "); Serial.println(Lance);
// Serial.print("D1-D2-D3 avant tirage : ");
// Serial.print(D1); Serial.print(D2); Serial.println(D3);
 
 if (Lance < 4){
 
 if (!D1) {Tirage[0] = random(1, 7);}
 if (!D2) {Tirage[1] = random(1, 7);}
 if (!D3) {Tirage[2] = random(1, 7);}

Tri();
 
// Serial.print("Tirage Décharge: ");
// Serial.print(Tirage[0]);
// Serial.print(Tirage[1]);
// Serial.println(Tirage[2]);

Efface();

Affiche(1, Tirage[0]);
 Affiche(2, Tirage[1]);
 Affiche(3, Tirage[2]);

Bt_1State = Bt_2State = Bt_3State = D1 = D2 = D3 = 0;
 
delay (Temps);
 
 
 
 if(Lance==3){
 CalculScore(); Lance = 0;
// Serial.println("Calcul score Décharge");
 if(Joueur){
 CalculJetons();
// Serial.println("Calcul jetons Décharge");
 AfficheJetons();
// Serial.println("Affiche Jetons Decharge");}
 Joueur = !Joueur;
// Serial.print("Passage au joueur : ");
// Serial.println(Joueur + 1);
 }
 }
 
 Bt_GState = 0; //Serial.println("Remise à zéro du lancement"); 
 }
}

void Affiche(int nd, int d) {
 if (d == 1) {
 if (nd == 1) {
 digitalWrite (D1_1, HIGH);
 }
 if (nd == 2) {
 digitalWrite (D2_1, HIGH);
 }
 if (nd == 3) {
 digitalWrite (D3_1, HIGH);
 }
 }
 if (d == 2) {
 if (nd == 1) {
 digitalWrite (D1_2, HIGH);
 }
 if (nd == 2) {
 digitalWrite (D2_2, HIGH);
 }
 if (nd == 3) {
 digitalWrite (D3_2, HIGH);
 }
 }
 if (d == 3) {
 if (nd == 1) {
 digitalWrite (D1_1, HIGH);
 digitalWrite (D1_3, HIGH);
 }
 if (nd == 2) {
 digitalWrite (D2_1, HIGH);
 digitalWrite (D2_3, HIGH);
 }
 if (nd == 3) {
 digitalWrite (D3_1, HIGH);
 digitalWrite (D3_3, HIGH);
 }
 }
 if (d == 4) {
 if (nd == 1) {
 digitalWrite (D1_4, HIGH);
 digitalWrite (D1_3, HIGH);
 }
 if (nd == 2) {
 digitalWrite (D2_4, HIGH);
 digitalWrite (D2_3, HIGH);
 }
 if (nd == 3) {
 digitalWrite (D3_4, HIGH);
 digitalWrite (D3_3, HIGH);
 }
 }
 if (d == 5) {
 if (nd == 1) {
 digitalWrite (D1_1, HIGH);
 digitalWrite (D1_3, HIGH);
 digitalWrite (D1_4, HIGH);
 }
 if (nd == 2) {
 digitalWrite (D2_1, HIGH);
 digitalWrite (D2_3, HIGH);
 digitalWrite (D2_4, HIGH);
 }
 if (nd == 3) {
 digitalWrite (D3_1, HIGH);
 digitalWrite (D3_3, HIGH);
 digitalWrite (D3_4, HIGH);
 }
 }
 if (d == 6) {
 if (nd == 1) {
 digitalWrite (D1_2, HIGH);
 digitalWrite (D1_3, HIGH);
 digitalWrite (D1_4, HIGH);
 }
 if (nd == 2) {
 digitalWrite (D2_2, HIGH);
 digitalWrite (D2_3, HIGH);
 digitalWrite (D2_4, HIGH);
 }
 if (nd == 3) {
 digitalWrite (D3_2, HIGH);
 digitalWrite (D3_3, HIGH);
 digitalWrite (D3_4, HIGH);
 }
 }
}
void Tri() {
 while (Tirage[0] < Tirage[1] || Tirage[0] < Tirage[2] || Tirage[1] < Tirage[2]) {
 if (Tirage[0] < Tirage[1]) {
 Temp = Tirage[0];
 Tirage[0] = Tirage[1];
 Tirage[1] = Temp;
 }
 if (Tirage[1] < Tirage[2]) {
 Temp = Tirage[1];
 Tirage[1] = Tirage[2];
 Tirage[2] = Temp;
 }
 }
}

void CalculScore() {
 // tri des des
 Tri();
 // combinaisons
 // 421
 if (Tirage[0] == 4 && Tirage[1] == 2 && Tirage[2] == 1 ) {
 Val = 100;
 (!Joueur) ? Val1 = Val : Val2 = Val;
 }
 
 // 111
 if (Tirage[0] == 1 && Tirage[1] == 1 && Tirage[2] == 1 ) {Val = 70;
(!Joueur) ? Val1 = Val : Val2 = Val;}
 
 // 666 - 611
 if ( Tirage[0] == Tirage[1] && Tirage[1] == Tirage[2] && Tirage[0] == 6 ) {Val = 66;
 (!Joueur) ? Val1 = Val : Val2 = Val;} 
 if ( Tirage[0] == 6 && Tirage[1] == 1 && Tirage[2] == 1 ) {Val = 65;
 (!Joueur) ? Val1 = Val : Val2 = Val;}
 
// 555 - 511
if ( Tirage[0] == Tirage[1] && Tirage[1] == Tirage[2] && Tirage[0] == 5 ) { Val = 55; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
if ( Tirage[0] == 5 && Tirage[1] == 1 && Tirage[2] == 1 ) { Val = 54; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}

// 444 - 411
if ( Tirage[0] == Tirage[1] && Tirage[1] == Tirage[2] && Tirage[0] == 4 ) { Val = 44; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
if ( Tirage[0] == 4 && Tirage[1] == 1 && Tirage[2] == 1 ) { Val = 43; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
 
// 333 - 311
if ( Tirage[0] == Tirage[1] && Tirage[1] == Tirage[2] && Tirage[0] == 3 ) { Val = 33; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
if ( Tirage[0] == 3 && Tirage[1] == 1 && Tirage[2] == 1 ) { Val = 32; 
 (!Joueur) ? Val1 = Val : Val2 = Val;
 }
// 222 - 211
if ( Tirage[0] == Tirage[1] && Tirage[1] == Tirage[2] && Tirage[0] == 2 ) { Val = 22; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
if ( Tirage[0] == 2 && Tirage[1] == 1 && Tirage[2] == 1 ) { Val = 21; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
 
// Suites
if ( Tirage[0] == 6 && Tirage[1] == 5 && Tirage[2] == 4 ) { Val = 20; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}
 if ( Tirage[0] == 5 && Tirage[1] == 4 && Tirage[2] == 3 ) { Val = 19; 
(!Joueur) ? Val1 = Val : Val2 = Val;}
 if ( Tirage[0] == 4 && Tirage[1] == 3 && Tirage[2] == 2 ) { Val = 18; 
(!Joueur) ? Val1 = Val : Val2 = Val;}
 if ( Tirage[0] == 3 && Tirage[1] == 2 && Tirage[2] == 1 ) { Val = 17; 
(!Joueur) ? Val1 = Val : Val2 = Val;}

//221 Nenette
if ( Tirage[0] == 2 && Tirage[1] == 2 && Tirage[2] == 1 ) { Val = 16; 
 (!Joueur) ? Val1 = Val : Val2 = Val;}

// Hors combinaisons
if ( Val1 == 0 && Val2 == 0){ 
Val =Tirage[0] * 100 + Tirage[1] * 10 + Tirage[2];
Val1 = Val; 
 //Serial.print("Hors combinaison J1 : ");
 //Serial.println(Val1);
} 
if (Val1>100 && Val2==0 && Joueur) { Val = 0;
 Val =Tirage[0] * 100 + Tirage[1] * 10 + Tirage[2];
Val2 = Val; 
 //Serial.print("Hors combinaison J2 : ");
 //Serial.println(Val2);
}

if (Joueur) {
 if ( Val1 - Val2 == 0 ) { Score1 = 0; Score2 = 0;}
 if ((Val1 <101 && Val1 > Val2 )||(Val1 <101 && Val2 > 100 )) {Score2 = 0;
 switch(Val1){
 case(100): Score1 = 10;break;
 case(70): Score1 = 7;break;
 case(66): Score1 = 6;break;
 case(65): Score1 = 6;break;
 case(55): Score1 = 5;break;
 case(54): Score1 = 5;break;
 case(44): Score1 = 4;break;
 case(43): Score1 = 4;break;
 case(33): Score1 = 3;break;
 case(32): Score1 = 3;break;
 case(22): Score1 = 2;break;
 case(21): Score1 = 2;break;
 case(20): Score1 = 2;break;
 case(19): Score1 = 2;break;
 case(18): Score1 = 2;break;
 case(17): Score1 = 2;break;
 case(16): Score1 = 4;break;
 default: Score1 = 1;break; 
 }
 }
 if ((Val2<101 && Val1 < Val2 )||(Val2 <101 && Val1 > 100 )) {Score1 = 0;
 switch(Val2){
 case(100): Score2 = 10;break;
 case(70): Score2 = 7;break;
 case(66): Score2 = 6;break;
 case(65): Score2 = 6;break;
 case(55): Score2 = 5;break;
 case(54): Score2 = 5;break;
 case(44): Score2 = 4;break;
 case(43): Score2 = 4;break;
 case(33): Score2 = 3;break;
 case(32): Score2 = 3;break;
 case(22): Score2 = 2;break;
 case(21): Score2 = 2;break;
 case(20): Score2 = 2;break;
 case(19): Score2 = 2;break;
 case(18): Score2 = 2;break;
 case(17): Score2 = 2;break;
 case(16): Score2 = 4;break;
 default: Score2 = 1;break; 
 }
 }
 if(Val1>100 && Val2>100) {
 if (Val1==Val2) { Score1=Score2=0 ; //Serial.println("Egalité Hors combinaison ");
 }
 if(Val1>Val2){ Score1=1;}
 if(Val2>Val1){ Score2=1;}
 }
 
 //Serial.print("Valeur Val1 : ");
// Serial.println(Val1);
// Serial.print("Valeur Val2 : ");
// Serial.println(Val2);
 Val1 = Val2 = 0;
// Serial.print("Calcul du score : ");
// Serial.println(Score1 + Score2);
}

}
 
void CalculJetons() {
 if (!Jeu) {
 if (Score1 > Pot) {
 Score1 = Pot;
 }
 if (Score2 > Pot) {
 Score2 = Pot;
 }
 if (Score1 > Score2) {
 Jetons2 = Jetons2 + Score1;
 }
 if (Score2 > Score1) {
 Jetons1 = Jetons1 + Score2;
 }
 Pot = Pot - Score1 - Score2;
 Score1 = Score2 = 0;
 
// Serial.print("Calcul du pot : ");
// Serial.println(Pot);
 }
 if(Jeu) {
 if (Score1 > Score2) {
 if (Score1 > Jetons1) {
 Score1 = Jetons1;
 }
 Jetons2 = Jetons2 + Score1;
 Jetons1 = Jetons1 - Score1;
 }
 if (Score2 > Score1) {
 if (Score2 > Jetons2) {
 Score2 = Jetons2;
 }
 Jetons1 = Jetons1 + Score2;
 Jetons2 = Jetons2 - Score2;
 }
 Score1 = Score2 = 0;
 }
 
}
void AfficheJetons(){
String str1;
 str1="J2";
String str2 = String(Jetons2);
if(Jetons2>=0 && Jetons2<=9){str2 = '0'+ str2;}
String str0 = str1 + str2;
display.clear();
 display.setColonOn(1);
 display.print(str0); // display LOOP on the display
delay(Temps); 
String str3;
 str3="J1";
String str4 = String(Jetons1);
if(Jetons1>=0 && Jetons1<=9){str4 = '0'+ str4;}
String str = str3 + str4;
display.clear();
 display.setColonOn(1);
 display.print(str); // display LOOP on the display 
delay(Temps);

// Serial.print("Jetons joueur 1 : ");
// Serial.println(Jetons1);
// Serial.print("Jetons joueur 2 : ");
// Serial.println(Jetons2);
}
void AfficheJoueur(){
 String str1;
(Joueur) ? str1="J2" : str1= "J1";
display.clear();
 display.setColonOn(1);
 display.print(str1); // display LOOP on the display
display.blink(); 
}

Voilà