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);
}