The best way to put all analogRead into one line of String?

Found a “For loop” example from the WebServer sketch to list out all analog Read

// output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }

Works great however, we would like to get all values on one line as String like this:

332,564,432,234,123,343

What would be the most efficient way to achieve this?

Thanks, TeamBTF

sprintf() or a combination of atoi() and strcat()

however, we would like to get all values on one line as String

Why? What are you going to do with the String?

Checkout Streaming
#include <Streaming.h>

Then use the “<<” operator to stream output

PaulS:

however, we would like to get all values on one line as String

Why? What are you going to do with the String?

Storing all values to a string separate by commas to be send to webserver

I would try this.

int sRead[6];
char outBuffer[64];

for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  sRead[analogChannel] = analogRead(analogChannel);
}

sprintf(outBuffer,"%u,%u,%u,%u,%u,%u
",sRead[0],sRead[1],sRead[2],sRead[3],sRead[4],sRead[5]);
client.println(outBuffer);

Below I took the original analog reading loop and converted it back to individual analog reads. Might be more understandable for single string development.

// zoomkat meta refresh server test code 4/12/13
// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

#include <SPI.h>
#include <Ethernet.h>
const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;


int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv='refresh' content='2'>"); //url='http://192.168.1.102:84/'
          client.print("<TITLE />Zoomkat's meta-refresh test 4-12-13</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test 4-12-13");
          client.println("
");
                    
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("
");
          client.println("
");
          
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
          
          // output the value of each analog input pin
            client.print("analog input0 ");
            client.print(" is ");
            client.print(analogRead(analogInPin0));
            client.println("
");
            
            client.print("analog input1 ");
            client.print(" is ");
            client.print(analogRead(analogInPin1));
            client.println("
");
                        
            client.print("analog input2 ");
            client.print(" is ");
            client.print(analogRead(analogInPin2));
            client.println("
");
            
            client.print("analog input3 ");
            client.print(" is ");
            client.print(analogRead(analogInPin3));
            client.println("
");
                                    
            client.print("analog input4 ");
            client.print(" is ");
            client.print(analogRead(analogInPin4));
            client.println("
");
            
            client.print("analog input5 ");
            client.print(" is ");
            client.print(analogRead(analogInPin5));
            client.println("
");
                                    
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

As has been asked, do you need the output in a String ?
How about

for (int analogChannel = 0; analogChannel < 6; analogChannel++) 
{
  client.print(analogRead(analogChannel));
  client.print(",");
}

@UKHeliBob: Your way = 12 packets. My way = 1 packet.

@SurferTim: My way easy to read and understand. No array, no buffer, no sprintf() and uses techniques from the original code. Your way very scary and more difficult for the for the OP to understand I suspect, even though I freely admit that it is better.