//ATTiny85 being used to provide ADC input to the Raspberry Pi(tm)
// There will be a pair of TMP36 temp sensors hooked to Analog 2 and 3
// Sketch written by Brian Schulteis (UnaClocker) licensed Creative Commons
#define rxPin 0
#define txPin 1
#define zoneOneSensor 3
#define zoneTwoSensor 2
#include <SoftwareSerial.h>
unsigned long zoneOneAverage=0;
unsigned long zoneTwoAverage=0;
long readingTimer=0;
char fromPi, *iHatePointers;
int temporary=0;
SoftwareSerial cereal(rxPin, txPin);
void setup() {
pinMode(zoneOneSensor, INPUT);
pinMode(zoneTwoSensor, INPUT);
analogReference(INTERNAL);
cereal.begin(9600);
for (int i=0; i<=20; i++) {
zoneOneAverage += analogRead(zoneOneSensor);
}
for (int i=0; i<=20; i++) {
zoneTwoAverage += analogRead(zoneTwoSensor);
}
readingTimer=millis();
}
void sendZoneData(int zoneNumber) {
if (zoneNumber==1) {
cereal.print(zoneOneAverage/20);
}
else {
cereal.print(zoneTwoAverage/20);
}
}
void loop() {
if (cereal.available()) {// Check for data in on the serial port
fromPi=cereal.read();
//cereal.print(fromPi); //diagnostics
if (fromPi=='Z') {// Z is the request Zone command
if (cereal.available()) { // make sure there's data available
fromPi=cereal.read(); // read the zone requested.
//cereal.print(fromPi); //diagnostics
*iHatePointers=fromPi;// link the pointer to the variable?
int zoneRequested=atoi(iHatePointers); // atoi converts a char to an int
if ((zoneRequested>0) && (zoneRequested<3)) { // make sure we got a valid zone request
sendZoneData(zoneRequested); // call the subroutine to transmit the temperature of that zone
}
while (cereal.available()) {
fromPi=cereal.read(); // flush the incoming buffer
}
}
}
}
if ((millis()-readingTimer) > 1000) { // once every second
int averageTemp = zoneOneAverage/20; // get one sample out
zoneOneAverage -= averageTemp; // remove that sample
averageTemp=analogRead(zoneOneSensor); // Switch ADCs, toss out first reading
delay(2); // Delay to allow ADC to normalize
zoneOneAverage += analogRead(zoneOneSensor); // add a new sample to the record
averageTemp = zoneTwoAverage/20;
zoneTwoAverage -= averageTemp;
averageTemp=analogRead(zoneTwoSensor); // Switch ADCs, toss out first reading
delay(2); // Delay to allow ADC to normalize
zoneTwoAverage += analogRead(zoneTwoSensor);
readingTimer=millis(); // reset the timer
//cereal.println("Tick Tock"); // diagnostics=]
}
}