Hello everyone,
I'm facing a problem with a project in Arduino IDE where I'm using several analog sensors to determine which letter to print based on sensor values. My code compiles without errors, but I'm struggling with the logic of conditionals and letter printing.
The specific issue I'm experiencing is that when I press the button, the letters are printed indefinitely as long as the conditions are met. However, I only want to print one letter, and then, if the conditions for another letter are met, print that new letter, but the previous letter should still be available to be printed again later if needed.
I've tried adding a control variable to prevent the repeated printing of the same letter, but I still can't achieve the desired behavior.
Could someone help me identify how I can modify my code so that only one letter is printed at a time and so that previously printed letters can be printed again if their conditions are met after another letter has been printed?
Any guidance or suggestions would be greatly appreciated. Thank you in advance for your help!
Best regards.
The code:
const int pinAnalogico[5] = {A0, A1, A2, A3, A4}; // Declaring which analog pins we will work with
int SensorDedo[5]; // Variable to store sensor readings
int pinBoton = 4; // Pin where we connect the button
int estadoBoton = 1; // Variable to store the button state
int Condicion = 0; // Condition to be met for printing a letter
int Condicion2 = 0; // Another condition to be met for printing a letter
char letras[] = {' ', 'A', 'I', 'E', 'U', 'B', 'H', 'L', 'M', 'Y', 'F'}; // Letters without button
char letrasB[] = {'O', 'D', 'N', 'P', 'W', 'G', 'HELP'}; // Letters with button
/* Val_min and Val_max define the range in which each sensor value must be to print a character */
int Val_min[11][5] = {{0, 0, 0, 500, 0}, // " "
{0, 500, 500, 500, 500}, // A
{150, 500, 500, 500, 0}, // I
{400, 500, 500, 500, 500}, // E
{300, 0, 0, 500, 500}, // U
{200, 0, 0, 0, 0}, // B
{0, 0, 0, 500, 500}, // H
{0, 0, 500, 500, 500}, // L
{200, 0, 0, 0, 500}, // M
{0, 500, 500, 500, 0}, // Y
{0, 300, 0, 0, 0}}; // F
int Val_max[11][5] = {{200, 200, 200, 1023, 200}, // " "
{200, 1023, 1023, 1023, 1023}, // A
{1023, 1023, 1023, 1023, 200}, // I
{1023, 1023, 1023, 1023, 1023}, // E
{1023, 200, 200, 1023, 1023}, // U
{1023, 200, 200, 200, 200}, // B
{200, 200, 200, 1023, 1023}, // H
{200, 200, 1023, 1023, 1023}, // L
{1023, 200, 200, 200, 1023}, // M
{200, 1023, 1023, 1023, 200}, // Y
{1023, 1023, 200, 200, 200}}; // F
int Val_minB[7][5] = {{500, 500, 500, 500, 500}, // O
{500, 500, 500, 500, 0}, // D
{500, 0, 0, 500, 500}, // N
{0, 0, 0, 500, 500}, // P
{500, 0, 0, 0, 500}, // W
{0, 0, 500, 500, 500}, // G
{0, 500, 500, 500, 500}}; // HELP
int Val_maxB[7][5] = {{1023, 1023, 1023, 1023, 1023}, // O
{1023, 1023, 1023, 1023, 200}, // D
{1023, 200, 200, 1023, 1023}, // N
{200, 200, 200, 1023, 1023}, // P
{1023, 200, 200, 200, 1023}, // W
{200, 200, 1023, 1023, 1023}, // G
{200, 1023, 1023, 1023, 1023}}; // HELP
void setup() {
pinMode(pinBoton, INPUT); // Set the button pin as input
Serial.begin(9600); // Initialize serial communication for serial monitor
}
void loop() {
for (int i = 0; i < 5; i++) { // Loop to read sensor values
SensorDedo[i] = analogRead(pinAnalogico[i]); // Read analog sensor values and store them in SensorDedo array
}
delay(1000); // Add a delay of 1000 milliseconds (1 second)
estadoBoton = digitalRead(pinBoton); // Read the state of the button (HIGH or LOW)
if (estadoBoton == LOW) { // If the button is pressed (LOW state)
bool s = false; // Declare a boolean variable to prevent repeated printing of the same letter
for (int j = 0; j < 7; j++) { // Loop through the characters associated with the button
Condicion = SensorDedo[0] >= Val_minB[j][0];
Condicion = Condicion && SensorDedo[0] <= Val_maxB[j][0];
Condicion = Condicion && SensorDedo[1] >= Val_minB[j][1];
Condicion = Condicion && SensorDedo[1] <= Val_maxB[j][1];
Condicion = Condicion && SensorDedo[2] >= Val_minB[j][2];
Condicion = Condicion && SensorDedo[2] <= Val_maxB[j][2];
Condicion = Condicion && SensorDedo[3] >= Val_minB[j][3];
Condicion = Condicion && SensorDedo[3] <= Val_maxB[j][3];
Condicion = Condicion && SensorDedo[4] >= Val_minB[j][4];
Condicion = Condicion && SensorDedo[4] <= Val_maxB[j][4];
if (Condicion && !s) { // If the conditions are met and the letter has not been printed before
Serial.print(letrasB[j]); // Print the corresponding letter
s = true; // Set s to true to prevent further printing of letters
break; // Exit the loop to avoid printing the same letter multiple times
}
}
} else { // If the button is not pressed
for (int i = 0; i < 11; i++) { // Loop through the characters without button
Condicion2 = SensorDedo[0] >= Val_min[i][0];
Condicion2 = Condicion2 && SensorDedo[0] <= Val_max[i][0];
Condicion2 = Condicion2 && SensorDedo[1] >= Val_min[i][1];
Condicion2 = Condicion2 && SensorDedo[1] <= Val_max[i][1];
Condicion2 = Condicion2 && SensorDedo[2] >= Val_min[i][2];
Condicion2 = Condicion2 && SensorDedo[2] <= Val_max[i][2];
Condicion2 = Condicion2 && SensorDedo[3] >= Val_min[i][3];
Condicion2 = Condicion2 && SensorDedo[3] <= Val_max[i][3];
Condicion2 = Condicion2 && SensorDedo[4] >= Val_min[i][4];
Condicion2 = Condicion2 && SensorDedo[4] <= Val_max[i][4];
if (Condicion2) { // If the conditions are met
Serial.print(letras[i]); // Print the corresponding letter
}
}
}
}
´´´