// Define Connections to 74HC595
int stcp_Pin = 10;
int shcp_Pin = 9;
//int ds_Pin[2] = {8,11}; // Broches de données pour le deuxième 74HC595
int ds_Pin1 = 8;
int ds_Pin2 = 11;
// Define Connections to 74HC165
// PL pin 2
int load = 2;
// CE pin 3
int clockEnablePin = 3;
// Q7 pin 4
int dataIn = 4;
// CP pin 5
int clockIn = 5;
void setup() {
// put your setup code here, to run once:
// Initialise le port série
Serial.begin(9600);
// Setup 74HC165 connections
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(dataIn, INPUT);
// Setup 74HC595 connections
pinMode(stcp_Pin, OUTPUT);
pinMode(shcp_Pin, OUTPUT);
pinMode(ds_Pin1, OUTPUT);
pinMode(ds_Pin2, OUTPUT);
//Serial.print(72+1*4);
}
// Déclaration des variables globales
String boutons_appuyes = ""; // Stocke les boutons appuyés
void loop() {
// put your main code here, to run repeatedly:
char* bouton = bouton_appuye_et_relache();
// Si le bouton est '=', on analyse les opérations d'addition et de soustraction
if (bouton[0] == '=') {
float resultat = analyser_operations();
// Affiche le résultat sur le port série
Serial.print("Résultat : ");
Serial.println(resultat);
// Réinitialise la variable des boutons appuyés
boutons_appuyes = "";
} else {
//Sinon, ajoute le bouton à la liste des boutons appuyés
boutons_appuyes += bouton;
Serial.print(bouton);
}
}
float analyser_operations() {
float resultat = 0;
int index_nb = 0;
int index_op = -1;
int x_or = 0;
String nombre = "";
String nombres [50] = {};
String operateur [50] = {};
bool erreur_operateur = false;
for (int i = 0; i < boutons_appuyes.length(); i++) {
//Serial.print(boutons_appuyes[i]);
if (isdigit(boutons_appuyes[i])) {
nombre += boutons_appuyes[i];
erreur_operateur = false;
}else {
if(boutons_appuyes[i] == 'x'){
x_or = 1;
nombre = nombres[index_nb - 1].toInt() * nombre.toInt();
nombres[index_nb - 1] = nombre;
index_nb -= 1;
}else{
nombres[index_nb] = nombre;
index_op += 1;
operateur[index_op] = boutons_appuyes[i];
}
if (erreur_operateur) {
Serial.println("Erreur: Deux opérateurs tapés de suite");
return 0; // Ou une autre valeur pour signaler l'erreur
}
index_nb += 1;
nombre = "";
erreur_operateur = true;
}
}
nombres[index_nb] = nombre;
Serial.println();
Serial.print("nombres [50] = {");
for (int o = 0; o <= index_nb; o++) {
Serial.print(nombres[o]);
Serial.print("; ");
}
Serial.print("};");
/*
for (int o = 0; o <= index_op; o++) {
if(o == 0){
if (operateur[o] == "+") {
resultat = nombres[o].toInt() + nombres[o+1].toInt();
} else if (operateur[o] == "-") {
resultat = nombres[o].toInt() - nombres[o+1].toInt();
} else if (operateur[o] == "x") {
resultat = nombres[o].toInt() * nombres[o+1].toInt();
} else if (operateur[o] == "/") {
resultat = nombres[o].toInt() / nombres[o+1].toInt();
}
}else {
if (operateur[o] == "+") {
resultat = resultat + nombres[o+1].toInt();
} else if (operateur[o] == "-") {
resultat = resultat - nombres[o+1].toInt();
} else if (operateur[o] == "x") {
resultat = resultat * nombres[o+1].toInt();
} else if (operateur[o] == "/") {
resultat = resultat / nombres[o+1].toInt();
}
}
}
*/
Serial.println();
Serial.print(resultat);
return resultat;
}
char* bouton_appuye_et_relache() {
char* bouton = NULL;
// On attend l'appui sur un bouton
do {
bouton = bouton_presse();
} while(bouton == NULL);
delay(100);
// On attend que l'utilisateur relache le bouton
while(bouton_presse() != NULL);
delay(100);
// On retourne le bouton qui a été pressé
return bouton;
}
char* lire_etat_colonnes(int ligne) {
int taille_colones = 6;
byte nb_colones[taille_colones] = {
B01111111, B10111111, B11011111, B11101111,
B11110111, B11111011};
const char* boutons[9][6] = {
{"SHIFT","ALPHA","left","up","MODE","ON"},
{"x-1","nCr","down","right","Pol(","Ax3"},
{"abk","#","x2","^","log","In"},
{"(-)",",,,","hyp","sin","cos","tan"},
{"RCL","ENG","(",")",",","M+"},
{"7","8","9","DEL","AC","?"},
{"4","5","6","x","/","?"},
{"1","2","3","+","-","?"},
{"0",".","EXP","Ans","=","?"}
};
// On recupère l'état des colonnes
// Write pulse to load pin
digitalWrite(load, LOW);
delayMicroseconds(5);
digitalWrite(load, HIGH);
delayMicroseconds(5);
// Get data from 74HC165
digitalWrite(clockIn, HIGH);
digitalWrite(clockEnablePin, LOW);
byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
digitalWrite(clockEnablePin, HIGH);
for (int i = 0; i < taille_colones; ++i) {
if(incoming == nb_colones[i]) {
return boutons[ligne][i];
}
}
return NULL;
}
char* bouton_presse() {
char* bouton = NULL;
int nb_595 = 2;
int nb_lignes = 9;
byte nb_lignes_valeurs[nb_lignes] = {
B10000000, B01000000, B00100000, B00010000,
B00001000, B00000100, B00000010, B00000001, B01000000
};
for (int i = 0; i < nb_lignes; ++i) {
int ds_Pin = (i == 8) ? ds_Pin2 : ds_Pin1;
digitalWrite(stcp_Pin, LOW);
shiftOut(ds_Pin, shcp_Pin, LSBFIRST, nb_lignes_valeurs[i]);
digitalWrite(stcp_Pin, HIGH);
bouton = lire_etat_colonnes(i);
if (bouton != NULL) {
return bouton;
}
}
return NULL;
}
void envoie_bouton_presse_sur_port_serie(char* bouton_caractere) {
Serial.print(bouton_caractere);
Serial.print(" ");
}