FSR + LED "Light Bar" Project

I am fairly new to programming an Arduino, we are using an Arduino Mega 2560 to test the amount of pressure being applied to an FSR for a school project.

We need it to light up in this order:
the first LED for “Normal Pressure”
the second LED for “Light Pressure”
the third LED for “Medium Pressure”
the fourth LED for “Heavy Pressure”

The Code:

/* FSR to LED bar VTC Project */
 
int fsrPin = 0;     // the FSR and 10K pulldown are connected to a0
int fsrReading;     // the analog reading from the FSR resistor divider 
int LEDbrightness;
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 4;    // the number of LEDs in the bar graph

int ledPins[] = { 
  2, 3, 4, 5 };   // an array of pin numbers to which LEDs are attached

 
void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }
}

 
void loop(void) {
  fsrReading = analogRead(fsrPin);  
  Serial.print("Analog reading = ");
  Serial.print(fsrReading);     // the raw analog reading
 
  // We'll have a few threshholds, qualitatively determined
  if (fsrReading < 10) {
    Serial.println(" - No Pressure");
  } else if (fsrReading < 200) {
    Serial.println(" - Normal Pressure");
  } else if (fsrReading < 500) {
    Serial.println(" - Light Pressure");
  } else if (fsrReading < 800) {
    Serial.println(" - Medium Pressure");
  } else {
    Serial.println(" - Heavy Pressure");
  }
  delay(250);
 // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  int ledLevel = map(fsrReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    } 
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW); 
    }
  }
 
  delay(100); 
}
const int ledCount = 4;    // the number of LEDs in the bar graph

// used by analogWrite (0-255) with map!
  int ledLevel = map(fsrReading, 0, 1023, 0, ledCount);

ledLevel ends up 0 to 4. Is that right?

groundfungus:

const int ledCount = 4;    // the number of LEDs in the bar graph

// used by analogWrite (0-255) with map!
  int ledLevel = map(fsrReading, 0, 1023, 0, ledCount);




ledLevel ends up 0 to 4. Is that right?

Yes, 0 to 4

I understand now. I guess the comment threw me. I set up a Uno with your code and found that ledLevel never gets to 4.
If you

map(fsrReading, 0, 1023, 0, ledCount + 1);

It will go to 4 (but not to 5).

groundfungus:
I understand now. I guess the comment threw me. I set up a Uno with your code and found that ledLevel never gets to 4.
If you

map(fsrReading, 0, 1023, 0, ledCount + 1);

It will go to 4 (but not to 5).

For some reason the first LED will not light up for "Normal Pressure" but it will light up for "Light Pressure"
The middle two lights only light up for Medium and then the final light will light up for Heavy.

The divisions from the map statement don't match the values in the if else statements that choose the serial print message, perhaps. You have 2 things going on that don't match. Either actuate the LEDs in the serial print part or the serial prints in the LED array part.

groundfungus:
The divisions from the map statement don't match the values in the if else statements that choose the serial print message, perhaps. You have 2 things going on that don't match. Either actuate the LEDs in the serial print part or the serial prints in the LED array part.

That makes complete sense but, I am very unsure how to re-program it.

Using arrays:

int fsrPin = 0;     // the FSR and 10K pulldown are connected to a0
int fsrReading;     // the analog reading from the FSR resistor divider
int LEDbrightness;
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 4;    // the number of LEDs in the bar graph
char* messages[] = {"NO PRESSURE", "NORMAL PRESSURE", "LIGHT PRESSURE", "MEDIUM PRESSURE", "HEAVY PRESSURE"};

int ledPins[] = {
    2, 3, 4, 5 };   // an array of pin numbers to which LEDs are attached


void setup(void) {
    Serial.begin(9600);   // We'll send debugging information via the Serial monitor
    // loop over the pin array and set them all to output:
    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
        pinMode(ledPins[thisLed], OUTPUT);
    }
}


void loop(void) {
    fsrReading = analogRead(fsrPin);
    Serial.print("Analog reading = ");
    Serial.println(fsrReading);     // the raw analog reading   
    delay(250);
    // we'll need to change the range from the analog reading (0-1023) down to the range
    // used by analogWrite (0-255) with map!
    int ledLevel = map(fsrReading, 0, 1023, 0, ledCount+1);
    //Serial.println(ledLevel);
    Serial.println(messages[ledLevel]);  // print message depending on mapped value
    // loop over the LED array:
    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
        // if the array element's index is less than ledLevel,
        // turn the pin for this element on:
        if (thisLed < ledLevel) {
            digitalWrite(ledPins[thisLed], HIGH);
        }
        // turn off all pins higher than the ledLevel:
        else {
            digitalWrite(ledPins[thisLed], LOW);
        }
    }

    delay(500);
}

Is that what you needed? It would be nice to know if I helped or not.