Last active
December 12, 2017 21:49
-
-
Save pavelmetrokhin/bd8bb5e71c1d402450345e5a8cd27d83 to your computer and use it in GitHub Desktop.
A C sketch to receive voltage reading from the analog sensor, convert to Celsius and Fahrenheit values, receive the special registers from the master device (Omega) and transmit the data back to the master
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the scale factor of TMP36 (temperature sensor) is 10 mV/°C with a 500 mV offset to allow for negative temperatures | |
#include <Wire.h> | |
// the analog pin number connected to the TMP36 | |
#define slaveAddr 0x08 | |
// defining global flags as actual registers | |
#define RD_REG_TEMP_INT_CEL 0x00 | |
#define RD_REG_TEMP_FRA_CEL 0x01 | |
#define RD_REG_TEMP_INT_FAR 0x02 | |
#define RD_REG_TEMP_FRA_FAR 0x03 | |
//define the analog port that is used on Arduino Dock 2 | |
int sensorPin = A0; | |
//Byte that is being receiving | |
byte recvData; | |
//Global variables of the temperature to be accessible at any time | |
int celsiusInt; | |
int celsiusFrac; | |
int fahrenheitInt; | |
int fahrenheitFrac; | |
void setup() | |
{ | |
//received data from the Master | |
recvData = 0xff; | |
//initializing serial communication with the Omega2 for sending sensor data | |
Serial.begin(9600); | |
// initializing i2c interface | |
Wire.begin(slaveAddr); | |
Wire.onReceive(receiveEvent); | |
Wire.onRequest(requestEvent); | |
} | |
void receiveEvent(int bytesReceived) | |
{ | |
// loop through all received bytes | |
for (int i = 0; i < bytesReceived; i++) | |
{ | |
if (i == 0) | |
{ | |
// only store the very first received byte | |
recvData = Wire.read(); | |
} | |
else | |
{ | |
// make sure to read the rest of the bytes to clear the i2c buffer | |
Wire.read(); | |
} | |
} | |
} | |
//Function definision for requesting the values from the slave (ATmega) and transfer it over to the master (Omega). | |
//We can only send one value at a time | |
void requestEvent() | |
{ | |
if (recvData == RD_REG_TEMP_INT_CEL) | |
{ | |
Wire.write((byte)celsiusInt); | |
} | |
else if (recvData == RD_REG_TEMP_FRA_CEL) | |
{ | |
Wire.write((byte)celsiusFrac); | |
} | |
else if (recvData == RD_REG_TEMP_INT_FAR) | |
{ | |
Wire.write((byte)fahrenheitInt); | |
} | |
else if (recvData == RD_REG_TEMP_FRA_FAR) | |
{ | |
Wire.write((byte)fahrenheitFrac); | |
} | |
else | |
{ | |
Wire.write(0xff); | |
} | |
} | |
float ReadSensor() | |
{ | |
// getting the voltage reading from the temperature sensor | |
int reading = analogRead(sensorPin); | |
// convert the analog reading (0 to 1023) to voltage (0 - 5V) | |
float voltage = (float)reading * 5.0; | |
voltage /= 1024.0; | |
return (voltage); | |
} | |
float convertVoltTocelsius(float voltage) | |
{ | |
float temperatureC = (voltage - 0.5) * 100; | |
return (temperatureC); | |
} | |
float convertCelcToFahrenheit (float temperatureC) | |
{ | |
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0; | |
return (temperatureF); | |
} | |
//A function definition to get an Integer part of the reading | |
//e.g. you get a value of 26.73. This function returns 26 as you can only send one byte at a time | |
int GetInteger (float inputFloat) | |
{ | |
int intPart=(int)inputFloat; | |
return intPart; | |
} | |
//A function definition to get a Fractional part of the reading | |
//e.g. you get a value of 26.73. This function returns 73 as you can only send one byte at a time | |
int GetFractional(float inputFloat) | |
{ | |
float frac = (inputFloat-GetInteger(inputFloat))+0.05; // +0.05 is added to either round up or down the end result | |
frac = frac*100; | |
return (int)frac; | |
} | |
void loop() | |
{ | |
float voltage_reading = ReadSensor(); | |
float celsius = convertVoltTocelsius(voltage_reading); | |
float fahrenheit = convertCelcToFahrenheit(celsius); | |
//functions to get Integer and Fractional part of celsius and Fahrenheit values | |
celsiusInt = GetInteger(celsius); | |
celsiusFrac = GetFractional(celsius); | |
fahrenheitInt = GetInteger(fahrenheit); | |
fahrenheitFrac = GetFractional(fahrenheit); | |
/* This is optional feature to see the values that are correspondin to Celsius and Fahrenheit | |
Serial.print("Celcius Integer: "); Serial.println(celsiusInt); | |
Serial.print("Celcius Fractional: "); Serial.println(celsiusFrac); | |
Serial.print("Fahrenheit Integer: "); Serial.println(fahrenheitInt); | |
Serial.print("Fahrenheit Fractional: "); Serial.println(fahrenheitFrac); | |
*/ | |
delay(1000); // delay between sensor reads | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment