Is there something about the ethernet shield that would cause a problem with the ADC on the mega328? I made a small voltage divider that incorporates a 100K resister and a thermistor and when it is plugged into the arduino directly it works fine but when it is plugged into the arduino through the ethernet shield I get values that are completely off. Can anyone shed any light on this for me? Sketch is below.
/*
* BBQControl.c
*
* Created: 8/7/2011 12:34:32 PM
* Author: Saleem and Leslie
*/
#define LCD_Command_A 0x7C
#define LCD_Command_B 0xFE
#define LCD_Clear_Screen 0x01
#define LCD_Cursor_Position(a) {Serial.print(LCD_Command_B,BYTE); Serial.print(a+128,BYTE);}
#define chipSelect 10
#define thermPin A1
#include <math.h>
#include <stdint.h>
#include <PID_v1.h>
#include <stdio.h>
#include <string.h>
//PID Variables
double Input = 0; //Our Current Temperature
double Output = 0; //Our Control Variable
double Setpoint = 0; //Our Setpoint
double Kp = 10; //Proportional
double Ki = .1; //Integral
double Kd = 50; //Derivative
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup()
{
pinMode(thermPin,INPUT); //Thermistor is connected to A1
Serial.begin(9600);
myPID.SetMode(AUTOMATIC); //start PID
}
void loop()
{
uint16_t ADCVal;
uint16_t temperature;
//String dataString = "";
//ADCVal = analogRead(thermPin);
//temperature = (get_temperature(ADCVal)); //get temperature conversion
for(int i = 0;i<100;i++)
{
ADCVal = analogRead(thermPin);
temperature += (get_temperature(ADCVal)); //add 99 more conversions
}
temperature = temperature/100; //average them
Input = (double)temperature; //load new temperature into our PID Algorithm
myPID.Compute(); //compute new output value
//Serial.println(Input); //print output to out monitor
//Serial.println(Output);
if(Serial.available()) //check to see if the user has sent
//any new parameters
{
char myString[40]; //buffer to hold my received string
char *str; //pointer for strtok function
receive_serial(&myString[0]); //get serial data
//Serial.println(myString); //debugging
double PID_Params[4]; //array to hold my new parameters
double *my_params = PID_Params;
str = strtok (myString,","); //split mystring into 4 pieces using comma delimiter, reference:
while (str != NULL) //http://www.cplusplus.com/reference/clibrary/cstring/strtok/
{
*my_params = atof(str); //convert strings segments to doubles
my_params++;
//Serial.println(str);
str = strtok (NULL, ","); //strtok expects a NULL pointer for all
} //subsequent calls after the first call
Setpoint = PID_Params[0];
myPID.SetTunings(PID_Params[1],PID_Params[2],PID_Params[3]);
}
//Send all of the data to our monitor in comma delimited format
Serial.print(temperature);
Serial.print(",");
Serial.print(ADCVal);
Serial.print(",");
Serial.print(Setpoint,0);
Serial.print(",");
Serial.print(myPID.GetKp(),4);
Serial.print(",");
Serial.print(myPID.GetKi(),4);
Serial.print(",");
Serial.println(myPID.GetKd(),4);
delay(100);
}
void receive_serial(char *String)
{
int i = Serial.available(); //Serial.available returns number of bytes
delay(10); //in the Serial buffer
//wait to make sure that all bytes have been received
while(i != Serial.available()) //if i does not equal Serial.available then
{ //that means we are still receiving bytes.
i = Serial.available();
delay(10);
}
//Read serial data into address pointed to by String
while(Serial.available())
{
*String = Serial.read();
String++;
*String = '\0';
}
}
void get_parameters(uint16_t ADCvalue, uint16_t *beta, float *r_infinity)
{
if (ADCvalue < 15) {*beta = 5191; *r_infinity=0.07028;}
else if (ADCvalue <= 39) {*beta = 5085; *r_infinity=0.08597;}
else if (ADCvalue <= 158) {*beta = 4942; *r_infinity=0.11637;}
else if (ADCvalue <= 530) {*beta = 4767; *r_infinity=0.17766;}
else if (ADCvalue <= 577) {*beta = 4671; *r_infinity=0.23249;}
else if (ADCvalue <= 670) {*beta = 4641; *r_infinity=0.25289;}
else if (ADCvalue <= 757) {*beta = 4604; *r_infinity=0.28168;}
else if (ADCvalue <= 832) {*beta = 4566; *r_infinity=0.31618;}
else if (ADCvalue <= 892) {*beta = 4526; *r_infinity=0.35779;}
else if (ADCvalue <= 938) {*beta = 4485; *r_infinity=0.40755;}
else if (ADCvalue <= 1023){*beta = 4453; *r_infinity=0.45243;}
return;
}
//*************Hardware SetUp***************
//
// ADC Input
// |
// Rpad | Rtherm
// Vcc--------/\/\/\------/\/\/\------ground
//
//******************************************
float get_temperature(uint16_t ADCVal){
//temperature in kelvin = beta/ln(R/Rinfinity)
uint16_t beta = 0;
uint8_t servo_command;
float Ri = 0;
float Rpad = 100000;
float Rtherm = Rpad/(1-((float)ADCVal/1024.0)) - 100000;
float Temperature;
float t;
//Serial.println(Rtherm);
get_parameters(ADCVal, &beta, &Ri);
t=beta/log(Rtherm/Ri);
t = ((9.0/5.0)*(t-273))+32;
Temperature = t;
//Out to LCD
//LCD_Cursor_Position(72);
//Serial.print(ADCVal);
//Serial.print(" ");
//LCD_Cursor_Position(28);
//Serial.print(beta);
//Serial.print(" ");
//LCD_Cursor_Position(8);
return Temperature;
}
