Arduino code for Bed_data
int sensorValue[80]; // an array to store the sensor values
// the address pins will go in order from the first one:
#define firstAddressPin 8
#define ThermistorPIN 0 // Analog Pin 0
double temp;
//////////////////////////////temperature alg
#include
// [Ground] —- [10k-Resister] ——-|——- [Thermistor] —- [+5v]
// |
// Analog Pin 0
double Thermistor(int RawADC) {
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius
// requires: include
// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
// where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
long Resistance;
double Temp; // Dual-Purpose variable to save space.
Resistance=((10240000/RawADC) – 10000); // Assuming a 10k Thermistor. Calculation is actually: Resistance = (1024/ADC)
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // “Temp” means “Temporary” on this line.
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); // Now it means both “Temporary” and “Temperature”
Temp = Temp – 273.15; // Convert Kelvin to Celsius // Now it only means “Temperature”
// Uncomment this line for the function to return Fahrenheit instead.
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
return Temp; // Return the Temperature
}
double printDouble(double val, byte precision) {
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimal places
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
if( precision > 0) {
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision–) mult *=10;
if(val >= 0) frac = (val – int(val)) * mult;
else frac = (int(val) – val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding–;
if(frac > 0.5){
val = val+1;
}
else{
val = val;
}
Serial.print((int) val);
}
}
////////////////end of temp
int analogInput = 0;
int sensorNum = 0;
void setup() {
Serial.begin(115200);
// set the output pins:
for (int pinNumber = firstAddressPin; pinNumber < firstAddressPin + 4; pinNumber++) {
pinMode(pinNumber, OUTPUT);
}
}
void loop() {
// iterate once for every multiplexer (called muxes for short):
for (int mux = 0; mux < 5; mux++) {
for (int channelNum = 0; channelNum < 16; channelNum ++) {
// determine the four address pin values from the channelNum:
setChannel(channelNum);
// read the analog input and store it in the value array:
sensorValue[channelNum] = analogRead(analogInput+mux);
if(analogInput+mux == 4&& channelNum == 14){
//////temp
temp=sensorValue[channelNum];
temp = Thermistor(temp); // read ADC and convert it to Celsius
//Serial.print(", Celsius: "); printDouble(temp,3); // display Celsius
temp = (temp * 9.0)/ 5.0 + 32.0; // converts to Fahrenheit
// Serial.print("Fahrenheit: ");
printDouble(temp,3); // display Fahrenheit
Serial.print(",");
}
else{
// print the values as a single tab-separated line:
Serial.print(sensorValue[channelNum], DEC);
Serial.print(",");
}
}
}
// print a carriage return at the end of each read of the mux:
Serial.println();
}
void setChannel(int whichChannel) {
for (int bitPosition = 0; bitPosition < 4; bitPosition++) {
// shift value x bits to the right, and mask all but bit 0:
int bitValue = (whichChannel >> bitPosition) & 1;
// set the address pins:
int pinNumber = firstAddressPin + bitPosition;
digitalWrite(pinNumber, bitValue);
}
}
