Force sensor with RGB led

Hi,

I know it is something super easy but I have tried everything and I can't find what is wrong in my code.
I want to use a force sensor to control a RGB led and depending the range of the force have a different color of lignt but it is not working. This is the code, please help!!!!!! :frowning: :frowning: :frowning:

int forcePin = 0;
int forceReading;

int reading;
boolean startUp;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int sensVal1 = constrain(sensVal1, 200, 400);
int sensVal2 = constrain(sensVal2, 400, 600);
int sensVal3 = constrain(sensVal3, 600, 750);
int sensVal4 = constrain(sensVal4, 750, 900);

void setup(void)
{
Serial.begin(9600);

forceReading = 0;
forceReading = analogRead(forcePin);
startUp = true;
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);

}

void loop()
{

calibrate();

forceReading = analogRead(forcePin);
Serial.print("Analog reading = ");
Serial.println(forceReading);

if(forceReading = sensVal1)
{

analogWrite(redPin, 102);
analogWrite(greenPin, 45);
analogWrite(bluePin, 145); // PURPLE
}
else
{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}

forceReading = analogRead(forcePin);
if(forceReading = sensVal2)
{
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0); // RED
}
else
{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}

forceReading = analogRead(forcePin);
if(forceReading = sensVal3)
{
analogWrite(redPin, 245);
analogWrite(greenPin, 181);
analogWrite(bluePin, 43); // YELLOW
}
else
{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}

forceReading = analogRead(forcePin);
if(forceReading = sensVal4)
{
analogWrite(redPin, 255);
analogWrite(greenPin, 230);
analogWrite(bluePin, 180); // WHITE
}
else
{
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}

}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

void calibrate()
{
if(startUp == true)
{
forceReading = reading;
startUp = false;
}
}

Why do you say that it doesnt work??? it doesnt compile? do you have wrong outputs???

Well, for starters, you didn't read the forum sticky to find out how to best use this forum.

Nonetheless, here are a few things...

int forcePin = 0;

Why use an int?

if (forceReading = sensVal3)

is not doing what you think it is. Read up on the comparison operators at Arduino - Home

int sensVal1 = constrain(sensVal1, 200, 400);

Doing this in the declaration section is worthless - see if you can figure out why.

And I don't think you should use constrain, anyway. What it seems you want to do is determine whether your forceReading is within certain ranges. One way to do that is with if statements with two comparisons, such as:

if (frcRdng > lowEndofRange && frcRdng < highEndofRange) { do stuff}

Also, consider using if...else if...else if... https://www.arduino.cc/en/Reference/Else

Hi!

I think the force strength doesn't change as fast as loop() function goes through its steps. So all port readings return the same value and according to your code: if force was sensVal1, colors will be set to zero at other stages, because of else --> 0.

I think the code should look like this:

forceReading = analogRead(forcePin);

if(forceReading == sensVal1)  setColor(255, 230, 180); else
if(forceReading == sensVal2)  setColor(...another...); else
if(forceReading == sensVal3)  setColor(...another...); else
if(forceReading == sensVal4)  setColor(...another...); else setColor(0, 0, 0); 
}