Need Help with my Code for Multiple Sensor Inputs

Hello everyone, I'm new to the forum and have started my first project and unfortunately I'm having problems right away. I hope you can help me a little bit.

The problem is that I want to log different analog inputs. In the first step, the output to the serial Monitor is enough for me. I have soldered the sensors (FSR sensors) correctly to the analog inputs of my Nano 33 Sense Rev 2 and measured them, so there is no wiring problem, but when I run the program shown below, I only get the values for A0. I must have defined something wrong or something else. When I test the sensors individually, using another program that was the template, for only one sensor (https://arduinogetstarted.com/tutorials/arduino-force-sensor?utm_content=cmp-true#google_vignette), it works with all inputs. I must have done something wrong when rewriting the code for multiple inputs.
Thanks for your help

const byte inputs[] = {A0,A1,A2,A3,A6};

void setup() {
  Serial.begin(9600);
  while (!Serial);
}

void loop() 
{

  for (int pin = 0; pin < 2; pin++)
  {
  int dummy = analogRead(inputs[A0]);
    int value = analogRead(inputs[A0]);
    Serial.print("Force sensor 1 reading = ");
    Serial.print(value); // print the raw analog reading

    if (value < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
    else if (value < 200) // from 10 to 199
    Serial.println(" -> light touch");
    else if (value < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
    else if (value < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
    else // from 800 to 1023
    Serial.println(" -> big squeeze");
  
    delay(100);
  }

  for (int pin = 0; pin < 2; pin++)
  {
    int dummy = analogRead(inputs[A1]);
    int value = analogRead(inputs[A1]);
    Serial.print("Force sensor 2 reading = ");
    Serial.print(value); // print the raw analog reading

    if (value < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
    else if (value < 200) // from 10 to 199
    Serial.println(" -> light touch");
    else if (value < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
    else if (value < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
    else // from 800 to 1023
    Serial.println(" -> big squeeze");
  
    delay(100);
  }

  for (int pin = 0; pin < 2; pin++)
  {
    int dummy = analogRead(inputs[A2]);
    int value = analogRead(inputs[A2]);
    Serial.print("Force sensor 3 reading = ");
    Serial.print(value); // print the raw analog reading

    if (value < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
    else if (value < 200) // from 10 to 199
    Serial.println(" -> light touch");
    else if (value < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
    else if (value < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
    else // from 800 to 1023
    Serial.println(" -> big squeeze");
  
    delay(100);
  }

  for (int pin = 0; pin < 2; pin++)
  {
    int dummy = analogRead(inputs[A3]);
    int value = analogRead(inputs[A3]);
    Serial.print("Force sensor 4 reading = ");
    Serial.print(value); // print the raw analog reading

    if (value < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
    else if (value < 200) // from 10 to 199
    Serial.println(" -> light touch");
    else if (value < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
    else if (value < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
    else // from 800 to 1023
    Serial.println(" -> big squeeze");
  
    delay(100);
  }

  for (int pin = 0; pin < 2; pin++)
  {
    int dummy = analogRead(inputs[A6]);
    int value = analogRead(inputs[A6]);
    Serial.print("Force sensor 5 reading = ");
    Serial.print(value); // print the raw analog reading

    if (value < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
    else if (value < 200) // from 10 to 199
    Serial.println(" -> light touch");
    else if (value < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
    else if (value < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
    else // from 800 to 1023
    Serial.println(" -> big squeeze");
  
    delay(100);
  }
}

inputs is an array with 5 elements. Use analogRead(inputs[0]).

You can print the value of A0 using Serial.println(); on an Uno it would print 14 which means that your code tries to access element 14 of the array (and there are only elements 0..4).

I'm not familiar with your board so I do not know what the Serial.println(A0) will give.

I think you meant to do this

const byte inputs[] = {A0,A1,A2,A3,A6};

void setup() {
  Serial.begin(9600);
  while (!Serial);
}

void loop() 
{

  for (int pin = 0; pin < 5; pin++)
  {
  int dummy = analogRead(inputs[pin]);
    int value = analogRead(inputs[pin]);
    Serial.print("Force sensor "); Serial.print(pin); 
    Serial.print(" reading = ");
    Serial.print(value); // print the raw analog reading

    if (value < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
    else if (value < 200) // from 10 to 199
    Serial.println(" -> light touch");
    else if (value < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
    else if (value < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
    else // from 800 to 1023
    Serial.println(" -> big squeeze");
  
    delay(100);
  }
  delay(2000);
}

Yes, thank you jim-p, that worked.

Sorry, but your first code is a mess.
To using the original code with a multiple sensor, you have to iterate throw your pin array in a loop, rather than creating a new loop for each input pin. Your using a for loop is complete nonsense:

I see @jim-p already present you a correct code...

Do you understand what you did wrong?

Yes I do, just a mistake that I thought I have to Insert the Pin what I will to measure, but it should be still [pin] and not [A0] etc.

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