Mapping Pressure sensors to RGB

Hello! I am new to Arduino! I have a project due tomorrow (oops) and it consists of mapping 5 RGB leds to 5 force pressure sensors. (my goal is for them to change colors depending on how much pressure is exerted). So far, the first one lights up based on the pressure however the other ones do not change colors according to how much I pressure the sensors.

Here is a picture of what I have. There are a lot of wires so feel free to ask questions. The resistors for the RGB lights are 200 ohms, the resistors for the pressure sensor is 10kOhms

FYI: the black and white writes are for the pressure sensors. white = analog and black = 5v. Plus, - is ground and + is 5V


I do not think this is a programming issue, but here is my code:

// Pin Definitions
const int numSensors = 5; // Number of sensors and LEDs

// Arrays to hold pin numbers for sensors and RGB LEDs
const int forceSensorPins[numSensors] = {A0, A1, A2, A3, A4}; // Analog inputs for force sensors
const int redPins[numSensors] = {A5, A7, A9, A11, A13}; // Red LED pins for each LED
const int greenPins[numSensors] = {A6, A8, A10, A12, A14}; // Green LED pins for each LED
const int bluePins[numSensors] = {9, 4, 2, 1, 5}; // Blue LED pins for each LED (not used, but reserved)

// Rescaled max value for force sensor reading
const int maxForceValue = 500;  // Maximum practical value for your application

// Color boundaries for Green to Yellow to Red transition
const int greenToYellowEnd = maxForceValue / 2;  // Midpoint where color is fully Yellow (Green+Red)
const int yellowToRedEnd = maxForceValue;  // End at Red (maxForceValue)

void setup() {
  // Initialize all LED pins as outputs
  for (int i = 0; i < numSensors; i++) {
    pinMode(redPins[i], OUTPUT);
    pinMode(greenPins[i], OUTPUT);
    pinMode(bluePins[i], OUTPUT);
  }

  Serial.begin(9600); // For debugging purposes
}

void loop() {
  // Loop through all sensors and adjust the corresponding LED color
  for (int i = 0; i < numSensors; i++) {
    // Read the force sensor value
    int forceValue = analogRead(forceSensorPins[i]);

    
    // Print the force value to the serial monitor for debugging
    Serial.print("Force Sensor ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(forceValue);

    // Adjust the color based on the force value with a smooth gradient
    if (forceValue <= greenToYellowEnd) {
      // Green to Yellow transition
      int redValue = map(forceValue, 0, greenToYellowEnd, 0, 255);
      int greenValue = 255;
      setColor(i, redValue, greenValue, 0); // No blue used
    } else if (forceValue <= yellowToRedEnd) {
      // Yellow to Red transition
      int redValue = 255;
      int greenValue = map(forceValue, greenToYellowEnd, yellowToRedEnd, 255, 0);
      setColor(i, redValue, greenValue, 0); // No blue used
    }

    delay(50); // Small delay for smooth visual transition
  }
}

// Function to set the RGB LED color for a specific sensor
void setColor(int sensorIndex, int redValue, int greenValue, int blueValue) {
  analogWrite(redPins[sensorIndex], redValue);
  analogWrite(greenPins[sensorIndex], greenValue);
  analogWrite(bluePins[sensorIndex], blueValue);
}

Verify your LED pins are PWM (right column)

https://docs.arduino.cc/retired/hacking/hardware/PinMapping2560/

1 Like

I put the LEDs in analog. I did not use the digital ones. Is this why since it doesn't say analog is PWM?

Sorry, most of your pins are good. You need to move pin 1. That pin is reserved for Hardware Serial.

When this starts, all the greens are ON. when I change the value of the input pins, nothing else seems to change. What is supposed to happen when each force increases?

Hey! Your originally suggestion worked for me however, the lights aren't turning dark enough (with the red)

Also, when each force increases, the light will start to go from yellow to red, like a gradient

Is that what you want?

I made some pin changes here, in Pin 1, but is this what you mean?

// Pin Definitions
const int numSensors = 5; // Number of sensors and LEDs

// Arrays to hold pin numbers for sensors and RGB LEDs
const int forceSensorPins[numSensors] = {A0, A1, A2, A3, A4}; // Analog inputs for force sensors
const int redPins[numSensors] = {A5, A7, A9, A11, A13}; // Red LED pins for each LED
const int greenPins[numSensors] = {A6, A8, A10, A12, A14}; // Green LED pins for each LED
const int bluePins[numSensors] = {9, 4, 2, 3, 5}; // Blue LED pins for each LED (not used, but reserved)
int forceValue[5];

// Rescaled max value for force sensor reading
const int maxForceValue = 500;
const int greenToYellowEnd = maxForceValue / 2;  // Midpoint where color is fully Yellow (Green+Red)
const int yellowToRedEnd = maxForceValue;  // End at Red (maxForceValue)

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < numSensors; i++) {
    pinMode(redPins[i], OUTPUT);
    pinMode(greenPins[i], OUTPUT);
    pinMode(bluePins[i], OUTPUT);
  }
}

void loop() {
  Serial.print("\nForce Sensor | ");
  for (int i = 0; i < numSensors; i++) {
    forceValue[i] = analogRead(forceSensorPins[i]);
    Serial.print(i);
    Serial.print(": ");

    if (forceValue[i] < 1000) Serial.print(" ");
    if (forceValue[i] < 100) Serial.print(" ");
    if (forceValue[i] < 10) Serial.print(" ");
    Serial.print (forceValue[i]);
    Serial.print(" | ");

    if (forceValue[i] <= greenToYellowEnd) // < 250
      analogWrite(greenPins[i], 255);
    else
      analogWrite(greenPins[i], 0);

    if (forceValue[i] > greenToYellowEnd && forceValue[i] <= maxForceValue) // > 250, < 500
      analogWrite(bluePins[i], HIGH);
    else
      analogWrite(bluePins[i], LOW);

    if (forceValue[i] > maxForceValue) // > 500
      analogWrite(redPins[i], 255);
    else
      analogWrite(redPins[i], 0);
    delay(50);
  }
}
diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-mega", "id": "mega", "top": -9, "left": 25.2, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led7",
      "top": 191.2,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led8",
      "top": 210.4,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led9",
      "top": 229.6,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led10",
      "top": 248.8,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led11",
      "top": 268,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led12",
      "top": -58.8,
      "left": 285,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led13",
      "top": -78,
      "left": 285,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led14",
      "top": -97.2,
      "left": 285,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led15",
      "top": -116.4,
      "left": 285,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led16",
      "top": -135.6,
      "left": 285,
      "rotate": 90,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot1",
      "top": 144.2,
      "left": -53.4,
      "rotate": 180,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot2",
      "top": 211.4,
      "left": -53.4,
      "rotate": 180,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot3",
      "top": 278.6,
      "left": -53.4,
      "rotate": 180,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot4",
      "top": 345.8,
      "left": -53.4,
      "rotate": 180,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot5",
      "top": 413,
      "left": -53.4,
      "rotate": 180,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 287.2,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "green", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 306.4,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "green", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 325.6,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "green", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": 344.8,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "green", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led5",
      "top": 364,
      "left": 381,
      "rotate": 90,
      "attrs": { "color": "green", "flip": "1" }
    }
  ],
  "connections": [
    [ "mega:A13", "led7:A", "green", [ "v0" ] ],
    [ "mega:A11", "led8:A", "green", [ "v0" ] ],
    [ "mega:A9", "led9:A", "green", [ "v0" ] ],
    [ "mega:A7", "led10:A", "green", [ "v0" ] ],
    [ "mega:A5", "led11:A", "green", [ "v112.5", "h102.55" ] ],
    [ "mega:GND.2", "led7:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led8:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led9:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led10:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led11:C", "black", [ "v0" ] ],
    [ "mega:9", "led16:A", "green", [ "v0" ] ],
    [ "mega:5", "led15:A", "green", [ "v0" ] ],
    [ "mega:4", "led14:A", "green", [ "v0" ] ],
    [ "mega:GND.1", "led12:C", "black", [ "v0" ] ],
    [ "mega:GND.1", "led13:C", "black", [ "v0" ] ],
    [ "mega:GND.1", "led14:C", "black", [ "v0" ] ],
    [ "mega:GND.1", "led15:C", "black", [ "v0" ] ],
    [ "mega:GND.1", "led16:C", "black", [ "v0" ] ],
    [ "pot1:SIG", "mega:A0", "green", [ "h0" ] ],
    [ "pot2:SIG", "mega:A1", "green", [ "h0" ] ],
    [ "pot3:SIG", "mega:A2", "green", [ "h0" ] ],
    [ "pot4:SIG", "mega:A3", "green", [ "h0" ] ],
    [ "pot5:SIG", "mega:A4", "green", [ "h0" ] ],
    [ "mega:GND.2", "pot1:VCC", "black", [ "v0" ] ],
    [ "mega:GND.2", "pot2:VCC", "black", [ "v0" ] ],
    [ "mega:GND.2", "pot3:VCC", "black", [ "v0" ] ],
    [ "mega:GND.2", "pot4:VCC", "black", [ "v0" ] ],
    [ "mega:GND.2", "pot5:VCC", "black", [ "v0" ] ],
    [ "mega:A14", "led1:A", "green", [ "v0" ] ],
    [ "led2:A", "mega:A12", "green", [ "h0" ] ],
    [ "led3:A", "mega:A10", "green", [ "h0" ] ],
    [ "led4:A", "mega:A8", "green", [ "h0" ] ],
    [ "mega:A6", "led5:A", "green", [ "v0" ] ],
    [ "mega:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led3:C", "black", [ "v0" ] ],
    [ "mega:GND.2", "led4:C", "black", [ "v198.9", "h184.55" ] ],
    [ "mega:GND.2", "led5:C", "black", [ "v0" ] ],
    [ "mega:2", "led12:A", "green", [ "v0" ] ],
    [ "mega:3", "led13:A", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

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