Fablab ado > pong 1D
Ca y est, nous sommes à la fin de notre parcours avec le ruban LED.
vous trouverez ci-dessous le schéma :
2 joueurs, joueur 1 (coté branchement, fils) et joueur 2 à l’opposé.
- explication code pong1D
Voici le code travaillé par Stéphane du pong 1D :
// PONG 1D V2
// PLMCB - Juin 2019
// Stéphane BEURRET
#include <FastLED.h>
#define LED_PIN 3
#define NUM_LEDS 144
#define BRIGHTNESS 30
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define DATA_PIN 3
#define BT1_PIN 4
#define BT2_PIN 5
#define DELAI_DEPART 30
#define NORMAL 0
#define ATTEIND1 1
#define ATTEIND2 2
#define DEPASSEMENT1 3
#define DEPASSEMENT2 4
CRGB leds[NUM_LEDS];
int delai=DELAI_DEPART;
int sens=1;
int ii;
int score_J1=0;
int score_J2=0;
int tour_joueur=1;
int nb_chgt_sens=0;
int longueur=15;
void setup()
{
Serial.begin(9600);
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
FastLED.addLeds<WS2812B,LED_PIN,RGB>(leds,NUM_LEDS);
Serial.println("*******************");
Serial.println("*** PONG 1D ***");
Serial.println("*** PLMCB ***");
Serial.println("*** Juin 2019 ***");
Serial.println("*******************");
Serial.println();
affiche_scores();
ii=0;
leds[ii] = CRGB::Blue;
FastLED.show();
attend_appui_joueur(tour_joueur);
}
//*******************************************************
int BT1_pressed()
{
if (digitalRead(BT1_PIN))
return true;
else
return false;
}
//*******************************************************
int BT2_pressed()
{
if (digitalRead(BT2_PIN))
return true;
else
return false;
}
//*******************************************************
void affiche_scores()
{
Serial.println("** Joueur 1: "+String(score_J1)+" - Joueur 2: "+String(score_J2));
}
//*******************************************************
void chgt_sens()
{
sens=-sens;
nb_chgt_sens++;
if (!(nb_chgt_sens%2)) reduit_delai(5);
}
//*******************************************************
int DeplacementPoint(int taille)
{
int jj,etat=NORMAL;
if ((sens==1)&&(ii>=NUM_LEDS))
{
for (jj=ii-taille;jj<NUM_LEDS;jj++)
leds[jj] = CRGB::Green;
etat=ATTEIND2;
}
else if ((sens==-1)&&(ii<0))
{
for (jj=0;jj<ii+taille;jj++)
leds[jj] = CRGB::Green;
etat=ATTEIND1;
}
else if((ii>-taille) && (ii<NUM_LEDS))
{
leds[ii] = CRGB::Blue;
etat=NORMAL;
}
if((sens==1)&&(ii>=taille))
{
leds[ii-taille] = CRGB::Black;
}
else if ((sens==-1)&&(ii<NUM_LEDS-taille))
{
leds[ii+taille] = CRGB::Black;
}
FastLED.show();
delay(delai);
ii=ii+sens;
if ((sens==1)&&(ii>=NUM_LEDS+taille-1))
{
etat=DEPASSEMENT2;
ii=NUM_LEDS-1;
}
else if ((sens==-1)&&(ii<=-taille))
{
etat=DEPASSEMENT1;
ii=0;
}
return etat;
}
//*******************************************************
void attend_appui_joueur(int joueur)
{
if (joueur==1)
while (!BT1_pressed()); //On ne fait rien tant que BT1 pas appuyé
else
while (!BT2_pressed()); //On ne fait rien tant que BT2 pas appuyé
}
//*******************************************************
void change_joueur()
{
delai=DELAI_DEPART;
if (tour_joueur==1)
{
tour_joueur=2;
ii=NUM_LEDS-1;
sens=-1;
}
else
{
tour_joueur=1;
ii=0;
sens=1;
}
leds[ii] = CRGB::Blue;
FastLED.show();
attend_appui_joueur(tour_joueur);
}
//*******************************************************
void loose(int joueur)
{
if (joueur==1)
{
for(ii=0;ii<NUM_LEDS;ii++)
{
leds[ii] = CRGB::Red;
FastLED.show();
}
for(ii=0;ii<NUM_LEDS;ii++)
{
leds[ii] = CRGB::Black;
FastLED.show();
}
score_J2++;
}
else
{
for(ii=NUM_LEDS-1;ii>=0;ii--)
{
leds[ii] = CRGB::Red;
FastLED.show();
}
for(ii=NUM_LEDS-1;ii>=0;ii--)
{
leds[ii] = CRGB::Black;
FastLED.show();
}
score_J1++;
}
affiche_scores();
change_joueur();
}
//*******************************************************
void reduit_delai(int val)
{
delai=delai-val;
if (delai<0) delai=0;
//Serial.println("delai="+String(delai));
}
//*******************************************************
void loop()
{
int etat=DeplacementPoint(longueur);
//Serial.println("sens="+String(sens)+" - ii="+String(ii)+" - Etat="+String(etat));
if((etat==ATTEIND1)&&(BT1_pressed()))
{
chgt_sens();
//Serial.println("BT1_pressed et etat=ATTEIND1");
ii=0;
}
else if((etat==ATTEIND2)&&(BT2_pressed()))
{
chgt_sens();
//Serial.println("BT2_pressed et etat=ATTEIND2");
ii=NUM_LEDS-1;
}
else if (etat==DEPASSEMENT1)
{
//Serial.println("Depassement 1 - LOOSE 1");
loose(1);
}
else if (etat==DEPASSEMENT2)
{
//Serial.println("Depassement 2 - LOOSE 2");
loose(2);
}
else if(etat==NORMAL)
if(BT1_pressed()&&(sens==-1))
{
//Serial.println("BT1_pressed avant d'atteindre ATTEIND1 - LOOSE 1");
loose(1);
}
else if(BT2_pressed()&&(sens==1))
{
//Serial.println("BT2_pressed avant d'atteindre ATTEIND2 - LOOSE 2");
loose(2);
}
}
- battle !
Partager cette page