Advertisement
martin2250

Untitled

May 10th, 2014
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <TinyWireS.h>
  4.  
  5. #define ADDRESS 0x10
  6.  
  7. byte pointer = 0;
  8. unsigned long last;
  9.  
  10. long temp = 0;
  11. long pres = 0;
  12. long bat = 0;
  13.  
  14. int samples = 0;
  15.  
  16. byte i2cdata[10];
  17.  
  18. OneWire oneWire(1);
  19. DallasTemperature sensors(&oneWire);
  20.  
  21. void setup()
  22. {
  23.   TinyWireS.begin(ADDRESS);
  24.   TinyWireS.onRequest(onRequest);
  25.   analogReference(6);
  26.   sensors.begin();
  27.   sensors.setWaitForConversion(true);
  28.   last = millis();
  29. }
  30.  
  31. void loop()
  32. {
  33.   bat += analogRead(A3);
  34.   pres += analogRead(A2);
  35.   temp += (sensors.getTempCByIndex(0) * 10);
  36.   samples++;
  37.   sensors.requestTemperatures();
  38.   delay(250);
  39. }
  40.  
  41. void onRequest()
  42. {
  43.   if(millis() - last > 5000)
  44.   {
  45.    
  46.     bat /= samples;
  47.     pres /= samples;
  48.     temp /= samples;
  49.    
  50.     int presi = pres;
  51.     int bati = bat;
  52.     int tempi = temp;
  53.    
  54.     i2cdata[0] = lowByte(bati);
  55.     i2cdata[1] = highByte(bati);
  56.    
  57.     i2cdata[2] = lowByte(presi);
  58.     i2cdata[3] = highByte(presi);
  59.    
  60.     i2cdata[4] = lowByte(tempi);
  61.     i2cdata[5] = highByte(tempi);
  62.    
  63.     pres = 0;
  64.     bat = 0;
  65.     temp = 0;
  66.  
  67.     samples = 0;
  68.     pointer = 1;
  69.     TinyWireS.send(i2cdata[0]);
  70.     last = millis();
  71.   }
  72.   else
  73.   {
  74.     TinyWireS.send(i2cdata[pointer]);
  75.     pointer++;
  76.     last = millis();
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement