I want to make a pressure sensor that when it detects a voltage it emits a light

here is my problem is that the pressure sensor all works there but the lights won’t turn on at all ive looked at it and can’t really find the problem may i get some help just on where’s the problem

#define REDPIN 5//Redpin
#define GREENPIN 6//Greenpin
#define BLUEPIN 3//bluepin
#define FADESPEED 15/ how it fades form one color to the next
void setup()//setup operation
{
  // begin serial and pins for lights
   pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  // ints for the light and sensor value indicator
  int r, g, b;
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  delay(500);
  float final = voltage -2.5;
  if(final < 0)
  {
    // if the value is a negative it will just show as 0
    Serial.println("0");
  }
 if(final > 0, final < .5)
  {
    // if the final is between 0 and .5 volts
  Serial.println(final);
      for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
  }
  if(final == 0)
  {
    //if 0 just put green
 analogWrite(GREENPIN, HIGH);
  }
  if(final > 0.5, final <1)
  {
    //same
    for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);  
  }
  }
  if(final > 1, final <2.25)
  {
    //same
      for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
    }
    if(final >2.25)
    {
      //same
      for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }  //Done
      
    }
  }

Immediately after the line int sensorValue = analogRead(A0); temporarily add a line to print the value that was detected. And just to slow things to a human scale temporarily add delay(200); as the last thing in loop()
It might also be wise to print the value of final because it is so important to the logic.

Where you have a cascade of IF statements it makes life easier to use else if

if ( x < 0)

else if (x < 0.5)

else if (X < 1)

By the way, with reference to your title, I always assumed pressure sensors detect pressure, not voltage :slight_smile:

…R

By the way, with reference to your title, I always assumed pressure sensors detect pressure, not voltage

I don't believe that I've ever seen a pressure sensor emit a light, either. Is that anything like a chicken emitting an egg?

Sorry maybe i wasn’t descriptive enough, i wan’t a pressure sensor, where the more pressure it detects it give a value to the arduino which i wan’t to read as a voltage, thanks guys. Also i want a pressure sensor to be decoded and then transmitted to a light that will show a certain color

smerkousdavid: Sorry maybe i wasn't descriptive enough, i wan't a pressure sensor, where the more pressure it detects it give a value to the arduino which i wan't to read as a voltage,

We know that. We were just having fun with your rather imprecise use of language. Did the advice in Reply #1 help?

...R

if(final > 0.5, final <1)

Wrong! Try:

if(final > 0.5 && final <1)//greater than 0.5 AND less than 1

There are a couple of other places where you’ve made that error.

Thanks guys all the responses helped me

That whole move to floating point only complicates your code, and btw the scaling is * 5 / 1024.

if ( val < 204 ) // less than 1V
{
}
else if ( val < 409 ) // less than 2V
{
}
else if ( val < 614 ) // less than 3V
{
}
etc, etc, etc. What analogRead() returns is directly all that your code needs.

Floating point just adds steps, slow chews CPU cycles and introduces inaccuracies.
You really don’t need it even if that’s what you’ve been taught.