Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ADDRESS 0x10
- byte pointer = 0;
- long temp = 0;
- long pres = 0;
- long bat = 0;
- long samples = 0;
- byte i2cdata[10];
- void setup()
- {
- TinyWireS.begin(ADDRESS);
- TinyWireS.onRequest(onRequest);
- TinyWireS.onReceive(onReceive);
- ADMUX |= _BV(REFS2) | _BV(REFS1);
- ADMUX &= ~_BV(REFS0);
- }
- void loop()
- {
- bat += analogRead(A3);
- TinyWireS_stop_check();
- pres += analogRead(A2);
- TinyWireS_stop_check();
- samples++;
- tws_delay(100);
- }
- void onRequest()
- {
- TinyWireS.send(i2cdata[pointer]);
- pointer++;
- }
- void onReceive(byte c)
- {
- byte x = TinyWireS.receive();
- if(x == 255)
- {
- temp = 0;
- pres = 0;
- bat = 0;
- samples = 0;
- }
- else if(x == 254)
- {
- temp /= samples;
- pres /= samples;
- i2cdata[0] = *(&temp);
- i2cdata[1] = *(&temp + 1);
- i2cdata[2] = *(&temp + 2);
- i2cdata[3] = *(&temp + 3);
- pointer = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement