Pages: [1]   Go Down
Author Topic: Help With pressure sensor controlling color fade  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.begin(9600);
  Serial.print (blueval);

  //blue def
  blueval=((1000-brt)/4);
  
  //green def
  if((brt>=0)||(brt<500)){
    greenval=.5*brt;
  }
  else{
    greenval=250-((brt-500)/2);
  }
  
  //red def
  redval=.25*brt;
  
 
  
  analogWrite(BLUELED, blueval);
  analogWrite(GREENLED, greenval);
  analogWrite(REDLED, redval);
  delay(10);
}



Anyone out there who can help with this?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add this code to loop():
Code:
Serial.print("Sensor reading: ");
Serial.println(brt);

At the end of loop, then, add a small delay:
Code:
delay(1000);

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Netherlands
Offline Offline
God Member
*****
Karma: 7
Posts: 656
A naughty mind is a joy forever.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?


Logged

Pages: [1]   Go Up
Jump to: