Guest User

Untitled

a guest
Jan 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. void LongToByte(long value, byte result[])
  2. {
  3. union
  4. {
  5. long value;
  6. byte bytes[4];
  7. } ul;
  8. ul.value = value;
  9. memcpy(result, ul.bytes, 4);
  10. }
  11.  
  12. long GetLong(byte* source)
  13. {
  14. union
  15. {
  16. long value;
  17. byte bytes[4];
  18. } ul;
  19. memcpy(ul.bytes, source, 4);
  20. return ul.value;
  21. }
  22.  
  23. long value = 2314;
  24. byte send[4];
  25. LongToByte(value, send);
  26.  
  27. Wire.beginTransmission(device_id);
  28. Wire.send(send, 4); // byte array, size
  29. Wire.endTransmission();
  30.  
  31. int position = 0;
  32. long value;
  33. byte read[4];
  34. while(Wire.available())
  35. {
  36. read[position++] = Wire.read();
  37. if(position == 4)
  38. {
  39. value = GetLong(read);
  40. Serial.println("Value: " + String(value));
  41. break;
  42. }
  43. }
Add Comment
Please, Sign In to add comment