I'm doing a temperature control project for college using 4x 1 wire Temperature Sensors (DS18B20). The plan is to get the average of the 4 sensors and when it reaches a set point another pin on the Arduino Mega will come on (for a pump). I've had a friend help me out with the programming as i have no experience with C/C++ (Did Matlab in college) but my friend isn't familiar with the Arduino and i'm having trouble with bugs, maybe someone can help me out?
What i've got to date is as follows:
#include <OneWire.h>
#include <DallasTemperature.h>
//Project 37 P1 Source: Beginning Arduino
//CMC 06.02.11
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress AThermometer, BThermometer, CThermometer, DThermometer;
float setpoint = 0;
void setup()
{
Serial.begin(9600);
sensors.begin();
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" Devices.");
if (!sensors.getAddress(AThermometer, 0)) Serial.println("Unable to find address for Device A");
if (!sensors.getAddress(BThermometer, 1)) Serial.println("Unable to find address for Device B");
if (!sensors.getAddress(CThermometer, 2)) Serial.println("Unable to find address for Device C");
if (!sensors.getAddress(DThermometer, 3)) Serial.println("Unable to find address for Device D");
Serial.print("Device A Address: ");
printAddress(AThermometer);
Serial.println();
Serial.print("Device B Address: ");
printAddress(BThermometer);
Serial.println();
Serial.println();
Serial.print("Device C Address: ");
printAddress(CThermometer);
Serial.println();
Serial.println();
Serial.print("Device D Address: ");
printAddress(DThermometer);
Serial.println();
Serial.println();
Serial.print("Please enter the required setpoint");
setpoint = readSetpoint();
}
float readSetpoint() {
if(Serial.available() > 0) {
int index = 0;
delay(100); // let the buffer fill up
int numChar = Serial.available();
if (numChar > 15) {
numChar=15;
}
while(numChar--) {
buffer[index++] = Serial.read();
}
float setpoint = atof(buffer);
// Clear the text and serial buffers
for (int x=0;x<16;x++) {
buffer[x]='\0';
}
Serial.flush();
return setpoint;
}
void printAddress(DeviceAddress deviceAddress)
{
for(int i=0; i<8; i++)
{
//zero pad the address if nessesary
if (deviceAddress[i]<16) Serial.print("0");
Serial.print(deviceAddress[i],HEX);
}
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
// Serial.print("Temp F: "); Fahrenheit not needed
// Serial.print(DallasTemperature::toFahrenheit(tempC));
}
void printData(DeviceAddress deviceAddress)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.println();
}
void loop()
{
//call sensors.requestTemperature() to issue a global temperature
//request to all devices on bus
Serial.print("Requesting Temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
//print device information
printData(AThermometer);
printData(BThermometer);
printData(CThermometer);
printData(DThermometer);
Serial.println();
float mean = (sensors.getTempC(AThermometer) + sensors.getTempC(BThermometer) +
sensors.getTempC(CThermometer) + sensors.getTempC(CThermometer) ) / 4;
Serial.print("Setpoint: ");
Serial.print(setpoint);
Serial.print(", Mean temperature: ");
Serial.print(mean);
Serial.println();
if(mean >= setpoint) {
Serial.print("!!! Mean temperature greater than setpoint !!!");
Serial.println();
Serial.print("Setpoint: ");
Serial.print(setpoint);
Serial.print(", Mean temperature: ");
Serial.print(mean);
Serial.println();
// NOTE: This is where the code to active a pin would go.221
delay(1000);
}
and my first issue/bugs of this version is as follows:
sketch_mar18a.cpp: In function 'float readSetpoint()':
sketch_mar18a:67: error: 'buffer' was not declared in this scope
sketch_mar18a:70: error: 'buffer' was not declared in this scope
sketch_mar18a:82: error: a function-definition is not allowed here before '{' token
sketch_mar18a:142: error: expected `}' at end of input
Second problem i have is getting a pin to activate once the set point is reached, anyone have a solution?
Thanks a mil, i tried that but then eventually got it all to compile with the following 2 bits of code
const int SIZE = 100;
char buffer;
or #define SIZE 100
char buffer;
char *buffer;
buffer = new char[100];
However i have a new issue.... :~
The code (which isn't giving me errors) is as follows
#include <OneWire.h>
#include <DallasTemperature.h>
//Project 37 P1 Source: Beginning Arduino
//CMC 06.02.11
#define ONE_WIRE_BUS 3
#define SIZE 100
char buffer[SIZE];
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress AThermometer, BThermometer, CThermometer, DThermometer;
float setpoint = 0;
void setup()
{
Serial.begin(9600);
sensors.begin();
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" Devices.");
if (!sensors.getAddress(AThermometer, 0)) Serial.println("Unable to find address for Device A");
if (!sensors.getAddress(BThermometer, 1)) Serial.println("Unable to find address for Device B");
if (!sensors.getAddress(CThermometer, 2)) Serial.println("Unable to find address for Device C");
if (!sensors.getAddress(DThermometer, 3)) Serial.println("Unable to find address for Device D");
Serial.print("Device A Address: ");
printAddress(AThermometer);
Serial.println();
Serial.print("Device B Address: ");
printAddress(BThermometer);
Serial.println();
Serial.println();
Serial.print("Device C Address: ");
printAddress(CThermometer);
Serial.println();
Serial.println();
Serial.print("Device D Address: ");
printAddress(DThermometer);
Serial.println();
Serial.println();
Serial.print("Please enter the required setpoint");
setpoint = readSetpoint();
}
float readSetpoint()
{
if(Serial.available() > 0)
{
int index = 0;
delay(100); // let the buffer fill up
int numChar = Serial.available();
if (numChar > 15)
{
numChar=15;
}
while(numChar--)
{
buffer[index++] = Serial.read();
}
float setpoint = atof(buffer);
// Clear the text and serial buffers
for (int x=0;x<16;x++)
{
buffer[x]='\0';
}
Serial.flush();
return setpoint;
}
}
void printAddress(DeviceAddress deviceAddress)
{
for(int i=0; i<8; i++)
{
//zero pad the address if nessesary
if (deviceAddress[i]<16) Serial.print("0");
Serial.print(deviceAddress[i],HEX);
}
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
// Serial.print("Temp F: "); Fahrenheit not needed
// Serial.print(DallasTemperature::toFahrenheit(tempC));
}
void printData(DeviceAddress deviceAddress)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.println();
}
void loop()
{
//call sensors.requestTemperature() to issue a global temperature
//request to all devices on bus
Serial.print("Requesting Temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
//print device information
printData(AThermometer);
printData(BThermometer);
printData(CThermometer);
printData(DThermometer);
Serial.println();
float mean = (sensors.getTempC(AThermometer) + sensors.getTempC(BThermometer) +
sensors.getTempC(CThermometer) + sensors.getTempC(CThermometer) ) / 4;
Serial.print("Setpoint: ");
Serial.print(setpoint);
Serial.print(", Mean temperature: ");
Serial.print(mean);
Serial.println();
if(mean >= setpoint)
{
Serial.print("!!! Mean temperature greater than setpoint !!!");
Serial.println();
Serial.print("Setpoint: ");
Serial.print(setpoint);
Serial.print(", Mean temperature: ");
Serial.print(mean);
Serial.println();
}
// NOTE: This is where the code to active a pin would go.221
delay(1000);
}
I then press upload and i get some lovely messages
Tried that but to no avail,
I also loaded other scripts and circuits to the Arduino and they work fine, only get the error for the above mentioned code :~