Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <TinyWireS.h>
- #define ADDRESS 0x10
- byte pointer = 0;
- unsigned long last;
- long temp = 0;
- long pres = 0;
- long bat = 0;
- int samples = 0;
- byte i2cdata[10];
- OneWire oneWire(1);
- DallasTemperature sensors(&oneWire);
- void setup()
- {
- TinyWireS.begin(ADDRESS);
- TinyWireS.onRequest(onRequest);
- analogReference(6);
- sensors.begin();
- sensors.setWaitForConversion(true);
- last = millis();
- }
- void loop()
- {
- bat += analogRead(A3);
- pres += analogRead(A2);
- temp += (sensors.getTempCByIndex(0) * 10);
- samples++;
- sensors.requestTemperatures();
- delay(250);
- }
- void onRequest()
- {
- if(millis() - last > 5000)
- {
- bat /= samples;
- pres /= samples;
- temp /= samples;
- int presi = pres;
- int bati = bat;
- int tempi = temp;
- i2cdata[0] = lowByte(bati);
- i2cdata[1] = highByte(bati);
- i2cdata[2] = lowByte(presi);
- i2cdata[3] = highByte(presi);
- i2cdata[4] = lowByte(tempi);
- i2cdata[5] = highByte(tempi);
- pres = 0;
- bat = 0;
- temp = 0;
- samples = 0;
- pointer = 1;
- TinyWireS.send(i2cdata[0]);
- last = millis();
- }
- else
- {
- TinyWireS.send(i2cdata[pointer]);
- pointer++;
- last = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement