Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Last active January 13, 2019 23:21
On tente de résoudre un problème mathématique posé par l'Arduino.
/**********************************************************************
On tente de résoudre un problème mathématique posé par Arduino.
Matériel: Arduino Mega, Afficheur LCD, Keypad numérique 4X4.
26 juin 2012
http://electroniqueamateur.blogspot.com/2012/06/arduino-clavier-numerique-et-afficheur.html
**********************************************************************/
// deux bibiliothèques sont utilisées:
#include <LiquidCrystal.h>
#include <Keypad.h>
// les broches associéees à l'afficheur
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// initialisation du keypad
const byte ROWS = 4; //nombre de lignes
const byte COLS = 4; //nombre de colonnes
char keys[ROWS][COLS] = {
{
'1','2','3','A' },
{
'4','5','6','B' },
{
'7','8','9','C' },
{
'*','0','#','D' }
};
byte rowPins[ROWS] = {
39, 41, 43, 45}; //entrées numériques où sont branchées les lignes
byte colPins[COLS] = {
47, 49, 51, 53}; //entrées numériques où sont branchées les colonnes
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int colonne = 0;
char derniereTouche;
int mode = 0; // 0: addition, 1: soustraction, 2: multiplication 3: division
long premierNombre = 0;
long deuxiemeNombre = 0;
long reponseSoumise = 0;
void setup() {
// nombre de colonnes et de lignes de l'afficheur LCD
lcd.begin(16, 2);
keypad.setDebounceTime(300); //fonctionne bien pour mon modèle
randomSeed(analogRead(0));
AfficheQuestion();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY){
if (key == 'A'){
mode = 0; // addition
AfficheQuestion();
}
if (key == 'B'){
mode = 1; // addition
AfficheQuestion();
}
if (key == 'C'){
mode = 2; // addition
AfficheQuestion();
}
if (key == 'D'){
mode = 3; // addition
AfficheQuestion();
}
if (key == '*'){//on efface le dernier chiffre
if (colonne > 0){
colonne = colonne - 1;
lcd.setCursor(colonne, 1);
lcd.print(' ');
reponseSoumise = (reponseSoumise-(int(derniereTouche)-48))/10;
}
}
if ((key == '0') || (key == '1') || (key == '2')|| (key == '3')
|| (key == '4')|| (key == '5')|| (key == '6')|| (key == '7')
|| (key == '8')|| (key == '9')){ // alors on a tapé un chiffre
if (colonne < 10){
lcd.setCursor(colonne, 1);
lcd.print(key);
colonne=colonne+1;
reponseSoumise = reponseSoumise*10+(int(key)-48);
derniereTouche=key;
}
}
if (key == '#'){//on vérifie la réponse
lcd.clear();
lcd.setCursor(0, 0);
colonne = 0;
if (mode == 0){
if (reponseSoumise == (premierNombre+deuxiemeNombre)){
lcd.print("Bravo:");
lcd.setCursor(0, 1);
lcd.print("Bonne reponse!");
}
else{
lcd.print("NON! Reponse:");
lcd.setCursor(0, 1);
lcd.print(premierNombre+deuxiemeNombre);
}
}
if (mode == 1){
if (reponseSoumise == (premierNombre-deuxiemeNombre)){
lcd.print("Bravo:");
lcd.setCursor(0, 1);
lcd.print("Bonne reponse!");
}
else{
lcd.print("NON! Reponse:");
lcd.setCursor(0, 1);
lcd.print(premierNombre-deuxiemeNombre);
}
}
if (mode == 2){
if (reponseSoumise == (premierNombre*deuxiemeNombre)){
lcd.print("Bravo:");
lcd.setCursor(0, 1);
lcd.print("Bonne reponse!");
}
else{
lcd.print("NON! Reponse:");
lcd.setCursor(0, 1);
lcd.print(premierNombre*deuxiemeNombre);
}
}
if (mode == 3){
if (reponseSoumise == (premierNombre/deuxiemeNombre)){
lcd.print("Bravo:");
lcd.setCursor(0, 1);
lcd.print("Bonne reponse!");
}
else{
lcd.print("NON! Reponse:");
lcd.setCursor(0, 1);
lcd.print(premierNombre/deuxiemeNombre);
}
}
}
}
}
void AfficheQuestion ()
{
long temp;
if (mode == 0){ // addition: deux nombres entre 0 et 99
premierNombre = random(100);
deuxiemeNombre = random(100);
}
if (mode == 1){ // soustraction
premierNombre = random(100);
deuxiemeNombre = random(100);
if (deuxiemeNombre > premierNombre){
temp = premierNombre;
premierNombre = deuxiemeNombre;
deuxiemeNombre = temp;
}
}
if (mode == 2){ //multiplication
premierNombre = random(11);
deuxiemeNombre = random(11);
}
if (mode == 3){ //division
deuxiemeNombre = random(1,10);
premierNombre = random(11)*deuxiemeNombre;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(premierNombre);
if (mode == 0){
lcd.setCursor(3, 0);
lcd.print('+');
lcd.setCursor(5, 0);
}
if (mode == 1){
lcd.setCursor(3, 0);
lcd.print('-');
lcd.setCursor(5, 0);
}
if (mode == 2){
lcd.setCursor(3, 0);
lcd.print('*');
lcd.setCursor(5, 0);
}
if (mode == 3){
lcd.setCursor(3, 0);
lcd.print('/');
lcd.setCursor(5, 0);
}
lcd.print(deuxiemeNombre);
reponseSoumise = 0;
colonne = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment