Issue with sensor readings and conditionals in Arduino IDE

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
      }
    }
  }
}
´´´

Most people use a state variable to "remember" that the character has been printed. Set it to true after the character is printed, and check that before printing again. When a new character is to be printed, set the state variable to false.

Likewise, use a state variable to detect when a button becomes pressed (or released) and take action at then, rather than while the button is pressed.

See the Arduino State Change example (Files>02.Digital>StateChangeDetection).

1 Like

Hi @daniel_vallagar

welcome to the arduino-forum.
Well done posting your code as a code-section in your very first posting.

I assume that your code shall print different letters multiple times.
But with your too short description it stays unclear what you really want.

To me it seems that your code has mimimum 5 logical problems.

As one example:

for (int i = 0; i < 11; i++)

runs through

and repeats printing if

the last condition is true

      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
      }

inside this for loop you are assigning repetatedly a result to variable "Condicion2"

lets assume

      // SensorDedo[0] >= Val_min[i][0]; results in true
      Condicion2 = true; 

      // Condicion2 && SensorDedo[0] <= Val_max[i][0]; results in false
      Condicion2 = false 

     // Condicion2 && SensorDedo[1] >= Val_min[i][1]; results in true;
      Condicion2 = true; 

Is this code behaviour that you want?
I am pretty sure you want something different

This means - as you are making false assumptions about the codes logic-
only presenting your code is INsufficient.

From your code with bugs it is impossible to understand what you really want.

So please give a functional description of what your code shall do.
Write this functional description with normal words.

Explicitly avoid using lines of code because your assumptions about how the code works are wrong.

Now there are two ways to achieve a solution

  1. a days and weeks of time consuming way and many many postings

  2. a way that take only a few hours and a few postings

which way to do you prefer?

I am pretty sure way 2
2. a way that takes only a few hours

This way is to take 30 minutes - maybe 1 or 2 hours of time to write down a detailed description of what your code shall do

And then post this detailed description

Most people prefer to be in a hurry to slow them down to achieve a solution only after days and weeks and many many postings.

What is your choice ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.