Étape 4: Programmer votre Arduino Gameboy
Il y a beaucoup de programmes que vous pouvez télécharger à ce système. D'entre eux est voie de camion (ne pas créé par moi, mais modifié pour ce système)Le code est :
#include < LiquidCrystal.h >
Affichage innovente
initialiser la bibliothèque avec les numéros des broches interface
Innovente lcd (12, 11, 5, 4, 3, 2) ;
Potentiomètre de volant
const int POTPIN = 1 ;
const int MAXPOT = 800 ; pas besoin de tourner la roue jusqu'à 1023:)
Haut-parleur piézo
const int SPEAKERPIN = 9 ;
const int RANDSEEDPIN = 0 ; une broche analogique qui n’est pas connectée à n’importe quoi
const int MAXSTEPDURATION = 300 ; Commencez lentement, chaque étape est plus courte de 1 millisecondes.
const int MINSTEPDURATION = 150 ; Il s’agit aussi vite que possible
const int NGLYPHS = 6 ;
les glyphes seront définies à partir de 1 (pas 0),
pour permettre à lcd.print() de chaînes se terminant par null
glyphes octet [NGLYPHS] [8] = {}
1: voiture vers le haut
{B00000,
B01110,
B11111,
B01010,
B00000,
B00000,
B00000,
B00000}
2: voiture vers le bas
, {B00000,
B00000,
B00000,
B00000,
B01110,
B11111,
B01010,
B00000}
3: camion vers le haut
, {B00000,
B11110,
B11111,
B01010,
B00000,
B00000,
B00000,
B00000}
4: camion vers le bas
, {B00000,
B00000,
B00000,
B00000,
B11110,
B11111,
B01010,
B00000}
5: crash vers le haut
, {B10101,
B01110,
B01110,
B10101,
B00000,
B00000,
B00000,
B00000}
6: crash down
, {B00000,
B00000,
B00000,
B10101,
B01110,
B01110,
B10101,
B00000}
};
const int NCARPOSITIONS = 4 ;
Chaque position est mappée à une colonne de 2 glyphes
Utilisé pour donner un sens quand j’avais une 5ème position
où la voiture ou crash a été dessiné comme 2 glyphes
(ne peut pas le faire étant donné que les chaînes sont terminées par 0),
donc c’est un peu bête maintenant, mais il n’est pas cassé:)
const char blanc = 32 ;
char car2glyphs [NCARPOSITIONS] [2] = {}
{1, BLANK}, {2, BLANK}, {VIDE, 1}, {VIDE, 2}
};
char truck2glyphs [NCARPOSITIONS] [2] = {}
{3, VIDE}, {4, VIDE}, {NULS, 3}, {VIDE, 4}
};
char crash2glyphs [NCARPOSITIONS] [2] = {}
{5, BLANK}, {6, BLANK}, {VIDE, 5}, {VIDE, 6}
};
const int ROADLEN = 15 ; Largeur de l’écran LCD (sans compter notre voiture)
int route [ROADLEN] ; positions des autres voitures
char line_buff [2 + ROADLEN] ; aux chaîne pour drawRoad()
int road_index ;
int car_pos ;
Position off-the-grid signifie colonne vide, tellement MAXROADPOS
détermine la probabilité d’une voiture dans une colonne
par exemple 3 * NCARPOSITIONS donne p = 1/3
const int MAXROADPOS = 3 * NCARPOSITIONS ;
int step_duration ;
accident d’int ; true si s’est écrasé
unsigned int crashtime ; Millis() en écrasant
const int CRASHSOUNDDURATION = 250 ;
const char * INTRO1 = « Camions, » ;
const char * INTRO2 = « Conduisez prudemment » ;
const int INTRODELAY = 2000 ;
void setup()
{
accident = crashtime = road_index = 0 ;
step_duration = MAXSTEPDURATION ;
line_buff [ROADLEN + 1] = « \0 » ; valeur null y mettre fin
randomSeed(analogRead(RANDSEEDPIN)) ;
pour (int i = 0; i < NGLYPHS; i ++) {}
lcd.createChar(i+1,glyphs[i]) ;
}
pour (int i = 0; i < ROADLEN; i ++) {}
chemin [i] =-1 ;
}
pinMode(SPEAKERPIN,OUTPUT) ;
analogWrite(SPEAKERPIN,0) ; pour être du bon côté
LCD.Begin(16,2) ;
getSteeringWheel() ;
drawRoad() ;
lcd.setCursor(1,0) ;
LCD.Print(INTRO1) ;
lcd.setCursor(1,1) ;
LCD.Print(Intro2) ;
Delay(INTRODELAY) ;
}
void loop() {}
unsigned long maintenant = millis ()-INTRODELAY ;
Si (! crash) {}
getSteeringWheel() ;
accident = (car_pos==road[road_index]) ;
}
Si {(crash)
Si (! crashtime) {}
crashtime = maintenant ;
drawRoad() ;
Jeu sur le texte
(les 2 premières colonnes de « crash » garder intacte)
lcd.setCursor(2,0) ;
LCD.Print ("s’est écrasé après") ;
lcd.setCursor(2,1) ;
LCD.Print(Now/1000) ;
LCD.Print ("secondes.") ;
}
Si ((now-crashtime) < CRASHSOUNDDURATION) {}
analogWrite(SPEAKERPIN,random(255)) ; bruit blanc
}
else {}
analogWrite(SPEAKERPIN,0) ; dramatique après écrasement silence:)
}
Delay(10) ; Attendre un peu entre les écritures
}
else {}
int prev_pos = route [(road_index-1) % ROADLEN] ;
int this_pos = random(MAXROADPOS) ;
tandis que (abs(this_pos-prev_pos) < 2) {/ / ne jam la route
this_pos = random(MAXROADPOS) ;
}
route [road_index] = this_pos ;
road_index = (road_index + 1) % ROADLEN ;
drawRoad() ;
Delay(step_duration) ;
Si (step_duration > MINSTEPDURATION) {}
step_duration--; aller plus vite
}
}
}
void getSteeringWheel() {}
car_pos = map(analogRead(POTPIN),0,1024,0,NCARPOSITIONS) ;
}
void drawRoad() {}
pour (int i = 0; i < 2; i ++) {}
Si {(crash)
line_buff [0] = crash2glyphs [car_pos] [i] ;
}
else {}
line_buff [0] = car2glyphs [car_pos] [i] ;
}
pour (int j = 0; j < ROADLEN; j ++) {}
int pos = route [(j+road_index) % ROADLEN] ;
line_buff [j + 1] = pos > = 0 & & pos < NCARPOSITIONS ? truck2glyphs [pos] [i]: vierges ;
}
lcd.setCursor(0,i) ;
LCD.Print(line_buff) ;
}
}