-2

I have a middle school student who is having problems getting her pollution sensor to work when asked to. I have attached her document to this post. Can anyone help her fix the issue?

int conductivitySensorValue; //initialize an integer variable to hold the sensor reading

void setup() {
Serial.begin(9600); //start serial communications so we can print the reading out

}

void loop() {
  conductivitySensorValue = analogRead(A0); //gat the integer representation of the sensor reading 

  float voltage = conductivitySensorValue*(5.0/1023.0); //put the data in terms of our voltage (5 volts)

  Serial.println("The Conductivity is" + voltage); //print the current reading to the serial port
}  


Arduino: 1.8.2 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\sushmit\Documents\Arduino\sketch_apr19a\sketch_apr19a.ino: In function 'void loop()':

sketch_apr19a:13: error: invalid operands of types 'const char [20]' and 'float' to binary 'operator+'

   Serial.println("The Conductivity is" + voltage); //print the current reading to the serial port

                                          ^

exit status 1
invalid operands of types 'const char [20]' and 'float' to binary 'operator+'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

End copy.......

Code Gorilla
  • 5,637
  • 1
  • 15
  • 31
  • 3
    Please edit your question (click "edit") and mark the code as code. In the editing box, highlight the code and press ctrl-k. Or highlight the code and click the {} icon in the toolbar at the top of the editing box. To highlight text click-drag across it or use the arrow keys while holding the shift key. ¶ Add a few blank lines between the end of the code and the beginning of the error messages. – James Waldby - jwpat7 Apr 20 '17 at 17:08
  • Try Serial.println(String("The Conductivity is ") + voltage); or simply Serial.print(F("The Conductivity is ")); Serial.println(voltage); – Mikael Patel Apr 20 '17 at 17:23
  • 1
    Welcome to Arduino SE. Please take a minute to view the tour: https://arduino.stackexchange.com/Tour - there are tips in there about how to format your questions. – SDsolar Apr 20 '17 at 18:50
  • Your post reads as if you are a teacher. I would have expected a teacher to understand this issue, maybe you ought to do some revision. If you are the student why hide? – Code Gorilla Apr 21 '17 at 07:15
  • 1
    Serial.println("The Conductivity is" + String(voltage)); works and is readable, though there's more efficient ways with lower level code. – dandavis Apr 21 '17 at 08:09

1 Answers1

4

You can't add a string literal and a number together.

Instead you either have to convert one or both into a format that can be joined, or just treat them as separate entities. The simplest way is:

Serial.print(F("The Conductivity Is: "));
Serial.println(voltage).

Note the use of F(...) around the string literal - that forces it to stay in Flash so it doesn't waste RAM. Maybe not an issue with this project, but a good habit to get into nonetheless.

Majenko
  • 105,095
  • 5
  • 79
  • 137
  • This is how I do it (although I didn't know about the F part) - Serial.print will do the conversion implicitly. No need to do any conversions in your code. – SDsolar Apr 20 '17 at 18:52