Trouble combining WiFi Web Server code with analog input code

Hi all,

I've been having trouble getting some code that I combined from different sources to work properly, and after a few days of tweaking the code, I haven't been successful in getting it to work as it should. Any help would be much appreciated as I'm completely new to Arduino programming.

Goal

I want to collect weight readings from a digital scale and store them in a Google Doc/Spreadsheet via WiFi.

I wired a digital scale load cell to my Arduino Uno and have been getting good readings, as discussed on this site: Arduino Load Cell Circuit & Sketch for Calibration Test | Airtripper's 3D Printer and Arduino Blog (the load cell code I'm using is here: load-cell-test/load_cell_test.ino at master · Airtripper/load-cell-test · GitHub). I found a tutorial on storing Arduino input data in a Google Doc/Spreadsheet and the first step is to connect the Arduino to a wireless network and establish a web server where input data can be displayed (tutorial: http://www.daviom.com/tutorial/arduino-server-google-drive-storage/). That tutorial uses data from a temperature sensor as the input data. I am trying to replace the temperature calculation code with the Airtripper weight calculation code (pasted above), but I have made some kind of mistake in doing so (my combined code is attached).

Problem with Combined Code

The code includes the following line to display specific information when you navigate to the WiFi shield's IP address:

// Output the JSON object
client.println("{\"projectid\":\""+projectID+"\",\"value\":"+weightAsString+",\"sheet\":\"weight\"}");

When I run the code with nothing on the scale, weightAsString displays as -500, but it should be 0, of course. When I run the code with something on the scale like my iPhone (113 grams), weightAsString displays as like -470.

I'm sure the problem arises because I included the Airtripper code in a way that undermines its functionality in calculating the weight, but I don't how to correct this.

Any ideas or help on how to remedy this issue would be much appreciated, and please let me know if I can clarify anything or provide more information that could be useful in addressing this.

Thanks so much!

My Combined Code

Due to the maximum allowed post length, I can't include the code, but the .ino file is attached.

Load Cell Code

WiFI Code

http://www.daviom.com/tutorial/arduino-server-google-drive-storage/.

Arduino_to_Google_Spreadsheet_7_14.ino (6.71 KB)

In case it is helpful, here is the part of my code that I think is causing the issue:

void loop()
{

	// Listen for incoming clients
	WiFiClient client = server.available();

	if (client)
	{
		Serial.println("new client");

		// An http request ends with a blank line
		boolean currentLineIsBlank = true;
		while (client.connected())
		{
			if (client.available())
			{
				char c = client.read();
				Serial.write(c);
				// If you've gotten to the end of the line (received a newline character)
				// and the line is blank, the http request has ended, so you can send a reply
				if (c == '\n' && currentLineIsBlank)
				{
					// Send a standard http response header

					client.println("HTTP/1.1 200 OK");
					client.println("Content-Type: application/json");
					client.println("Connection: close"); // The connection will be closed after completion of the response
					client.println();

					//Calculate Weight
					int loadcellValue = analogRead(loadCell); // Get analog reading from load cell

					// Add new analog reading to buffer and return oldest reading
					int oldestSample = addSample(loadcellValue);
					// Get running average, pass the latest and oldest analog sample reading
					analogSamplesAverage = runningAverage(loadcellValue, oldestSample);

					if(millis() > time + plotDelay)
					{
						// Convert analog value to load in grams
						float loadGrams = map(analogSamplesAverage, analogLow, analogHigh, loadLow, loadHigh);
						loadGrams = loadGrams - loadAdjustment; // Adjust to measure from 0

						String a;
						char weight[4];
						dtostrf(loadGrams, 4, 0, weight);
						String weightAsString = String(weight);

						time = millis(); // Set time to mark start of delay

						// Output the JSON object
						client.println("{\"projectid\":\"" + projectID + "\",\"value\":" + weightAsString + ",\"sheet\":\"weight\"}");

						break;
					}
				}

				if (c == '\n')
				{
					// Starting a new line
					currentLineIsBlank = true;
				}
				else if (c != '\r')
				{
					// Character on the current line
					currentLineIsBlank = false;
				}
			}
		}
		// Give web browser time to receive the data
		delay(1);

		// Close the connection:
		client.stop();
		Serial.println("client disconnected");
	}
}

// Function - Running average
long runningAverage(long newSample, long oldSample)
{
// Add new sample and subtract old sample from the samples buffer total, then return sample average
	sampleBufferTotal += newSample; // Add new analog sample
	sampleBufferTotal -= oldSample; // Subtract oldest analog sample
	return sampleBufferTotal / samplesBuffer; // Return average analog sample reading
}

// Function - Add and remove analog samples from ring buffer, return oldest sample
// The ring buffer is used to keep track of the oldest sample
int addSample(int newSample)
{
	// Add new analog read sample to the ring buffer and return the oldest analog reading
	int oldSample;
	if (lastSampleIndex == samplesBuffer - 1)   // Check if end of buffer is reached
	{
		oldSample = analogSamples[0]; // Get old analog sample from start of buffer
		analogSamples[0] = newSample; // Add new analog sample to start of buffer
		lastSampleIndex = 0; // Record sample index currently working on
	}
	else
	{
		lastSampleIndex ++; //Move to next index
		oldSample = analogSamples[lastSampleIndex]; // Get old analog sample
		analogSamples[lastSampleIndex] = newSample; // Add new analog sample
	}
	return oldSample; // Return old analog sample
}