Mehrere FSR-Sensoren mit Arduino Mega

Such Dir was aus.
Den jeweiligen Kommentar entfernen und an die nicht gebrauchten wieder einen voranstellen.

/*
   Bedingung ist, das der Sensor zwischen AnalogPin und GND geschalten ist
   zwischen AnalogPin und +5V liegt ein Pullup
   Berechnung der Widerstandswerte auf Basis der Kalkulation aus:
   https://forum.arduino.cc/t/mehrere-fsr-sensoren-mit-arduino-mega/908917/86?u=my_xy_projekt
*/

const byte fsrPins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9}; // the FSR and 10K pulldown are connected to a0 <- Dieser Kommentar war original drin und ist FALSCH!
const byte sensorZahl = sizeof(fsrPins) / sizeof(fsrPins[0]);
unsigned int fsrReading[sensorZahl];     // the analog reading from the FSR resistor divider

// Messwerte
// Das geht noch anders mit einem 2D-Array, aber erstmal anfangen....
const unsigned int adcWert[] = {1013, 1010, 1008, 1008, 1006, 1004, 1002, 1000, 995,  985,  977,  963,  951, 903,
                                861,  822,  778,  752,  721,  662,  621,  600,  584,  566,  535,  504,  479,  455,
                                442,  418,  396,  379,  372,  367,  365,  360,  357,  351,  346,  342,  339,  337,
                                333,  330,  327,  323,  320,  306,  294,  283,  277,  263,  234,  221,  209,  201,  194
                               };

const byte stuetzWerte = sizeof(adcWert) / sizeof(adcWert[0]);
const unsigned long nWert[stuetzWerte] = {310,  360,  410,  460,  520,  570,  620,  670,  720,  770,  820,  930,  1030, 1130,
                                          1240, 1440, 1650, 1860, 2060, 2270, 2470, 2680, 2890, 3090, 3300, 3510, 3710, 3920, 4120,
                                          4330, 4540, 4740, 4950, 5160, 5360, 5570, 5770, 5980, 6190, 6390, 6600, 6810, 7010, 7220,
                                          7420, 7630, 7840, 9280, 11340,  13400,  15470,  20620,  41240,  61870,  82490,  103110, 123730
                                         }; // Der Wert ist *1000 wegen ohne Nachkommastelle!!
unsigned long druck = 0;

unsigned long lastReading = 0;
const unsigned long readPause = 300;

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Start..."));
}
void loop(void)
{
  if (millis() - lastReading >= readPause)
  {
    readSensors ();
    rechneSensors();
    //rechneSensors2();
    //rechneSensors3();
    lastReading += readPause;
  }
}
void readSensors()
{
  for (byte i = 0; i < sensorZahl; i++)
  {
    fsrReading[i] = analogRead(fsrPins[i]);
  }
}

void rechneSensors3()
{
  static unsigned long lastDruck[sensorZahl] = {0};
  bool isNew = false;
  for (byte b = 0; b < sensorZahl; b++)
  {
    if (fsrReading[b] > adcWert[0])
      druck = 0;
    else
      druck = newton(fsrReading[b]);
    if (lastDruck[b] != druck)
    {
      if (!isNew) Serial.print(F("Sensor "));
      Serial.print(b); Serial.print(',');
      isNew = true;
      lastDruck[b] = druck;
    }
  }
  if (isNew)
  {
    Serial.println(F(" mit neuem Druck"));
    Serial.print(F("Druck in N: "));
    for (byte b = 0; b < sensorZahl; b++)
    {
      float pressure = lastDruck[b] / 1000.0;
      if (pressure < 10) Serial.print(' ');
      if (pressure < 100) Serial.print(' ');
      if (pressure < 1000) Serial.print(' ');
      if (pressure < 10000) Serial.print(' ');
      Serial.print(pressure, 3);
    }
    Serial.println();
  }
}


void rechneSensors2()
{
  Serial.print(F("Druck in N: "));
  for (byte b = 0; b < sensorZahl; b++)
  {
    if (fsrReading[b] > adcWert[0])
      druck = 0;
    else
      druck = newton(fsrReading[b]);
    float pressure = druck / 1000.0;
    if (pressure < 10) Serial.print(' ');
    if (pressure < 100) Serial.print(' ');
    if (pressure < 1000) Serial.print(' ');
    if (pressure < 10000) Serial.print(' ');
    Serial.print(pressure, 3);
  }
  Serial.println();
}


void rechneSensors()
{
  static unsigned long lastdruck[sensorZahl] = {0};
  for (byte b = 0; b < sensorZahl; b++)
  {
    if (fsrReading[b] > adcWert[0])
    {
      Serial.println(F("0"));
    }
    else
    {
      druck = newton(fsrReading[b]);
      if (druck != lastdruck[b])
      {
        Serial.print((float)druck / 1000, 3); Serial.println(F(" N"));
        lastdruck[b] = druck;
      }
    }
  }
}

unsigned long newton(const unsigned int analogWert)
{
  unsigned long j = 0;
  for (unsigned int i = 0; i < stuetzWerte; i++) // Abfrage des gesamten arrayinhalt
  {
    if (analogWert == adcWert[i])                // Messwert passt auf analogwert
    {
      return  nWert[i];                          // gebe passenden Druckwert zurueck
    }
    else if (analogWert > adcWert[i])            // Messwert liegt zwischen dem letzten (adcWert[i])
      //                                         // und dem vorletzten (adcWert[i-1])
    {
      j = map(analogWert, adcWert[i - 1], adcWert[i], nWert[i - 1], nWert[i]);
      return j;
    }
  }
  return j;
}