Go Down

Topic: Help With pressure sensor controlling color fade (Read 1 time) previous topic - next topic


I am building a pressure sensor that controls RGB lED's. I want to be able to get a smooth transition from blue (light pressure) to green (medium pressure) up to red (highest pressure) and am having trouble with the code.

//main sketch for pressure sensor(POT1) controlling brightness of LED's

#define GREENLED 9  //
#define BLUELED 10  //
#define REDLED 11  //
#define POT1 A0

int brt = 0;  //pot value
int redval= 0;
int blueval = 0;
int greenval = 0;

void setup()
 pinMode(REDLED, OUTPUT);   //tell Arduino LED is an output
 pinMode(BLUELED, OUTPUT);  //tell Arduino LED is an output
 pinMode(GREENLED, OUTPUT); //tell Arudino LED is an output
 pinMode(POT1, INPUT);      //tell Arduino LED is an output


void loop(){
 brt = analogRead(POT1);
 Serial.print (blueval);

 //blue def
 //green def
 //red def

 analogWrite(BLUELED, blueval);
 analogWrite(GREENLED, greenval);
 analogWrite(REDLED, redval);

Anyone out there who can help with this?


First question is why is Serial.begin() in loop()? That statement belongs in setup(), since it only should be called once.

Second question is whether you have any idea what values the sensor actually returns. The normal range of values in brt would be from 0 to 1023. That would give blueval a value in the range 250 to -5. Not all values in that range are valid PWM values.

The valuation of greenval is a bit strange. If the value in brt is greater than or equal to 0 (it always will be) OR it is less than 500, set greenval to half of brt, which gives greenval a range of values from 0 to 512. Not all values in that range are valid PWM values. I think you wanted AND (&&) in that statement, not OR (||).

So, what is the LED doing that you don't want, or not doing that you do want?

PS> I hope you know that the eye won't perceive any kind of linear transition from blue to red as the pressure on the sensor increases. The eye does not perceive green as being any kind of intermediary value between blue and red.
The art of getting good answers lies in asking good questions.


Thank you for catching my serial statement that was out of place.

When I originally tested my pressure sensor, it didn't seem to get above a 1000 range, so I set it from 0-1000. What I want the color to represent is the amount of pressure that is on the sensor, so I want blue to be in the range of light pressure, green to be the middle pressure color and red to be the one with the highest amount of pressure at the (1000) point.
In other words
at 0, blue=250;
at 500, green=250;
at 1000, red = 250;

But the LED is only giving me blue at this point.


Add this code to loop():
Code: [Select]
Serial.print("Sensor reading: ");

At the end of loop, then, add a small delay:
Code: [Select]

This will print one reading per second. What is the range of values you see?

Change the pin numbers around. Does one color consistently light up, while the others do not? Perhaps not all the LEDs are wired correctly. You do have current limiting resistors, right?
The art of getting good answers lies in asking good questions.


Okay I added that into my code and this is what I got:

Sensor reading;0             (this being no pressure)
Sensor reading;16
Sensor reading;3
Sensor reading;21
Sensor reading;19
Sensor reading;23
Sensor reading;22
Sensor reading;21
Sensor reading;22
Sensor reading;16
Sensor reading;0

And it's blue that consistently lights up, but I'm pretty sure they are wired correctly.


For a range of values that is supposed to span from 0 to 1023, that is a very small range of values. I would only expect blueval to contain a significant enough value to cause any visible light to appear.

What happens if you really mash on the sensor? Do the readings get significant?
The art of getting good answers lies in asking good questions.


No that is when I severely push on it to get it to those numbers.

Once I take the serial.print out of the loop the numbers change.


Reading such low values gives you almost gives you no resolution and no possibility to see a red light.

Which sensor are you using,  do you have a link to a data sheet and perhaps a schematic how it's connected in your project ?

Go Up