RGB/ FSR pressure sensor, code not doing what it says.

I have hooked up my arduino and used the code below. What the code says isn’t what the code is doing. When there is no pressure, the RGB is still on. The pressure sensor is working, but not changing the colours right. Here is the code and a photo.
Any help ??
Thanks

*/

//Setup all pins
int sensPin = 2; // set the touch sensor (analog) input pin on Arduino
int redPin = 3; // set the PWM (analog) output pin on Arduino controlling the red anode
int grnPin = 5; // set the PWM (analog) output pin on Arduino controlling the green anode
int bluPin = 6; // set the PWM (analog) output pin on Arduino controlling the blue anode

//Setup all initial values
int val = 0; // initial value for touch sensor input
int state = 1; // initial machine state

//Initialize variables
int redVal; // pulse width variable for red anode
int grnVal; // pulse width variable for green anode
int bluVal; // pulse width variable for blue anode

void setup() {
pinMode(redPin, OUTPUT); // set the LED pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
val = analogRead(sensPin); // read touch sensor values
Serial.println(val);

if (state == 1) {sleep();} // turn off LED
else if (state == 2) {redIn();} // fade in red color
else if (state == 3) {grnIn();} // fade in green color
else if (state == 4) {bluIn();} // fade in blue color
}

void sleep() {
alloff(); // turn off LED

if (val > 20 && val <= 500) {state = 2;} // test for low pressure
if (val > 500 && val <= 850) {state = 3;} // test for medium pressure
if (val > 850) {state = 4;} // test for high pressure
}

void redIn() { // function to fade in red color and transition to other states
grnVal = 0;
analogWrite(grnPin, grnVal); // turn off green in case the last state was green

if (redVal == 255) { // if red is at maximum, continue to light it up
analogWrite(redPin, redVal);
} else { // else fade in the red color
redVal ++;
analogWrite(redPin, redVal);
}

if (val < 20) {state = 1;} // turn off the LED if no pressure detected
if (val > 500) {state = 3;} // fade in green if pressure has increased
}

void grnIn() { // function to fade in green color and transition to other states
redVal = 0;
bluVal = 0;
analogWrite(redPin, redVal); // turn off red in case the last state was blue
analogWrite(bluPin, bluVal); // turn off blue in case the last state was blue

if (grnVal == 255) { // if green is at maximum, continue to light it up
analogWrite(grnPin, grnVal);
} else { // else fade in the green color
grnVal ++;
analogWrite(grnPin, grnVal);
}

if (val <= 500) {state = 2;} // fade in red if pressure has decrease
if (val >= 850) {state = 4;} // fade in blue if pressure has increased
}

void bluIn() { // function to fade in blue color and transition to other states
grnVal = 0;
analogWrite(grnPin, grnVal); // turn off green in case the last state was green

if (bluVal == 255) { // if blue is at maximum, continue to light it up
analogWrite(bluPin, bluVal);
} else { // else fade in the blue color
bluVal ++;
analogWrite(bluPin, bluVal);
}

if (val <= 850) {state = 3;} // fade in green if pressure has decreased
}

void alloff() { // function to turn off the LED
redVal = 0;
grnVal = 0;
bluVal = 0;

analogWrite(redPin, redVal);
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
}

  if (bluVal == 255) {            // if blue is at maximum, continue to light it up
    analogWrite(bluPin, bluVal);
  } else {                    // else fade in the blue color
    bluVal ++;
    analogWrite(bluPin, bluVal);
  }

can be shorter ==>

  if (bluVal < 255) bluVal ++;
  analogWrite(bluPin, bluVal);
  }

also other colors :slight_smile:

I would rethink the code,

you have 3 leds that should overlap

RED [0…512] => 0…255…0
GREEN [256-768] ==> 0…255…0
BLUE [512-768] ==> 0…255

That would give something like the following code - give it a try

//Setup all pins
int sensPin = 2;    // set the touch sensor (analog) input pin on Arduino
int redPin = 3;    // set the PWM (analog) output pin on Arduino controlling the red anode
int grnPin = 5;        // set the PWM (analog) output pin on Arduino controlling the green anode
int bluPin = 6;    // set the PWM (analog) output pin on Arduino controlling the blue anode

//Initialize variables
int redVal;         // pulse width variable for red anode
int grnVal;         // pulse width variable for green anode
int bluVal;         // pulse width variable for blue anode

void setup() 
{
  pinMode(redPin, OUTPUT);   // set the LED pins as output
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  int val = analogRead(sensPin);           // read touch sensor values

  if (val < 256)
  {
    redVal = val;
	greenVal = 0;
	blueVal = 0;
  }
  else if (val < 512)
  {
    redVal = 512 - val;
	greenVal = val-256;
	blueVal = 0;
  }
  else if (val < 768)
  {
    redVal = 0;
	greenVal = 768-val;
	blueVal = val-512;
  }
  else
  {
    redVal = 1023-val;
	greenVal = 1023-val;
	blueVal = 255;
  }
  display(redVal, greenVal, blueVal);
}

void display(int r, int g, int b) 
{
  analogWrite(redPin, r);
  analogWrite(grnPin, g);
  analogWrite(bluPin, b);
}

hey robtillaart !

Your code was exactly what was missing in my auracamara project :)!

But maybe you can help me a little bit more. I addapted your code to two RGB leds, but when I press the sensor in a value higher than 830 both of them are white :( and that is something that I want to avoid for the fotos.

I was trying but it is not working, do you know what should I change?

Thanks!!

@Yoryeline

In my example code I mapped the max on 1023 (max of the sensor) and divided the range 0..1023 in equal parts. [that's the theory] THink you should remap these values to ones that make sense with your sensor, so 850 as max and 200, 400 and 600 as 'midpoints'

Post your code so I can see how you adapted it.