Advertisement
UnaClocker

Tiny ADC

Sep 23rd, 2012
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. //ATTiny85 being used to provide ADC input to the Raspberry Pi(tm)
  2. // There will be a pair of TMP36 temp sensors hooked to Analog 2 and 3
  3. // Sketch written by Brian Schulteis (UnaClocker) licensed Creative Commons
  4.  
  5. #define rxPin 0
  6. #define txPin 1
  7. #define zoneOneSensor 3
  8. #define zoneTwoSensor 2
  9. #include <SoftwareSerial.h>
  10.  
  11. unsigned long zoneOneAverage=0;
  12. unsigned long zoneTwoAverage=0;
  13. long readingTimer=0;
  14. char fromPi, *iHatePointers;
  15. int temporary=0;
  16. SoftwareSerial cereal(rxPin, txPin);
  17.  
  18.  
  19. void setup() {
  20.   pinMode(zoneOneSensor, INPUT);
  21.   pinMode(zoneTwoSensor, INPUT);
  22.   analogReference(INTERNAL);
  23.   cereal.begin(9600);
  24.   for (int i=0; i<=20; i++) {
  25.     zoneOneAverage += analogRead(zoneOneSensor);
  26.   }
  27.   for (int i=0; i<=20; i++) {
  28.     zoneTwoAverage += analogRead(zoneTwoSensor);
  29.   }  
  30.   readingTimer=millis();
  31. }
  32.  
  33. void sendZoneData(int zoneNumber) {
  34.   if (zoneNumber==1) {
  35.     cereal.print(zoneOneAverage/20);
  36.   }
  37.   else {
  38.     cereal.print(zoneTwoAverage/20);
  39.   }
  40. }
  41.  
  42. void loop() {
  43.   if (cereal.available()) {// Check for data in on the serial port
  44.     fromPi=cereal.read();
  45.     //cereal.print(fromPi); //diagnostics
  46.     if (fromPi=='Z') {// Z is the request Zone command
  47.       if (cereal.available()) { // make sure there's data available
  48.         fromPi=cereal.read(); // read the zone requested.
  49.         //cereal.print(fromPi); //diagnostics
  50.         *iHatePointers=fromPi;// link the pointer to the variable?
  51.         int zoneRequested=atoi(iHatePointers); // atoi converts a char to an int
  52.         if ((zoneRequested>0) && (zoneRequested<3)) { // make sure we got a valid zone request
  53.           sendZoneData(zoneRequested); // call the subroutine to transmit the temperature of that zone
  54.         }
  55.         while (cereal.available()) {
  56.           fromPi=cereal.read(); // flush the incoming buffer
  57.         }
  58.       }
  59.     }
  60.   }
  61.   if ((millis()-readingTimer) > 1000) { // once every second
  62.     int averageTemp = zoneOneAverage/20; // get one sample out
  63.     zoneOneAverage -= averageTemp; // remove that sample
  64.     averageTemp=analogRead(zoneOneSensor); // Switch ADCs, toss out first reading
  65.     delay(2); // Delay to allow ADC to normalize
  66.     zoneOneAverage += analogRead(zoneOneSensor); // add a new sample to the record
  67.     averageTemp = zoneTwoAverage/20;
  68.     zoneTwoAverage -= averageTemp;
  69.     averageTemp=analogRead(zoneTwoSensor); // Switch ADCs, toss out first reading
  70.     delay(2); // Delay to allow ADC to normalize
  71.     zoneTwoAverage += analogRead(zoneTwoSensor);
  72.     readingTimer=millis(); // reset the timer
  73.     //cereal.println("Tick Tock"); // diagnostics=]
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement