How to convert integer values into strings in this case?

Hi Guys,
I want to display on my serial monitor a string values based on the integer values that i've got in my code.

Here is my code:

int dustPin = A0;
int ledPin = D2;    
 
float voltsMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
 
void setup()
{
  Serial.begin(57600);
  pinMode(ledPin,OUTPUT);
}
 
 
void loop()
{
  digitalWrite(ledPin,LOW); // power on the LED
  delayMicroseconds(280);
 
  voltsMeasured = analogRead(dustPin); // read the dust value
 
  delayMicroseconds(40);
  digitalWrite(ledPin,HIGH); // turn the LED off
  delayMicroseconds(9680);
 
  //measure your 5v and change below
  calcVoltage = voltsMeasured * (3.3 / 1024.0);
  dustDensity = 0.17 * calcVoltage - 0.1;
  Serial.println("GP2Y1010AU0F readings"); 
  Serial.print("Raw Signal Value = ");
  Serial.println(voltsMeasured); 
  Serial.print("Voltage = ");
  Serial.println(calcVoltage);
  Serial.print("Dust Density = ");
  Serial.println(dustDensity); // mg/m3
  Serial.println("");
  delay(1000);
}

What i want to display on the serial monitor:

  1. When the value is below 0.14 --> Lower Dust Level
  2. When the value is 0.14 or higher --> Normal Dust Level
  3. When the value is above than 0.20 --> Need to clean solar panel

So what is the best practice that i can use to have this result displaying on my serial monitor?

You want a simple, easy way? Jut place the text prints in 'if' statements that test the values. You can translate directly from your requirement:

//When the value is below 0.14 --> Lower Dust Level
if (value < 0.14) {
Serial.println("Lower Dust Level");
}
else
{...

I don’t understand the strings reference. Do you not just need to compare the dust density variable against your level limits and do an if else statement

If < low level
Print low dust
Else if < high level
Print normal
Else
Print clean panel

Best practices are always context dependant. So what are your design goals? Maintainability? Scaleability? Memory conservation? Etc. etc...

No conversion is needed here. You can directly apply the method shown in post#2.

Here is one way (to expand on what others have posted).

float dustValue = .18;

void setup()
{
   Serial.begin(115200);
   if (dustValue < 0.14)
   {
      Serial.println("Lower dust level");
   }
   else if (dustValue >= 0.14 && dustValue < 0.20)
   {
      Serial.println("Normal dust level");
   }
   else if (dustValue >= 0.20)
   {
      Serial.println("Need to clean solar panel");
   }
}

void loop()
{

}

`Do you need the comparator in the middle since it must be >= 0.14 to pass the first if

float dustValue = .18;

void setup()
{
   Serial.begin(115200);
   if (dustValue < 0.14)
   {
      Serial.println("Lower dust level");
   }
   else if (dustValue < 0.20)
   {
      Serial.println("Normal dust level");
   }
   else 
   {
      Serial.println("Need to clean solar panel");
   }
}

void loop()
{

}

Good point.

and, its a monitor, so he needs to be in loop(), not setup. I assume that was a clerical error? Just wanted to clarify, op seems green, might confuse them.

what I dont understand is, the original code the op posted seems to demonstrate they know how to do what they asked how to do...weird.

so @abddi_16 : whats the actual problems you're running into? Were you just not familiar with if() statements?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.