Advertisement
Guest User

USnodev01

a guest
Oct 23rd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. // Program: I2C slave sender template for multi-node Arduino I2C network
  2. // Programmer: Hazim Bitar (techbitar.com)
  3. // Date: March 30, 2014
  4. // This example code is in the public domain.
  5.  
  6. #include <Wire.h>
  7.  
  8. #define NODE_ADDRESS 2 // Change this unique address for each I2C slave node
  9. #define PAYLOAD_SIZE 14 // Number of bytes expected to be received by the master I2C node
  10. byte nodePayload[PAYLOAD_SIZE]; //Payload array
  11.  
  12.  
  13. //setup ultrasonic sencors
  14. #include <NewPing.h>
  15.  
  16. #define SONAR_NUM 4 // Number of sensors.
  17. #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
  18.  
  19. NewPing sonar[SONAR_NUM] = { // Sensor object array.
  20. NewPing(3, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  21. NewPing(5, 6, MAX_DISTANCE),
  22. NewPing(7, 8, MAX_DISTANCE),
  23. NewPing(9, 10, MAX_DISTANCE)
  24. };
  25.  
  26. void setup()
  27. {
  28.  
  29. nodePayload[0] = NODE_ADDRESS;
  30. nodePayload[1] = 3; // Mode #sensors/layout
  31.  
  32. Serial.begin(115200);
  33. Serial.println("SLAVE SENDER PING NODE");
  34. Serial.print("Node address: ");
  35. Serial.println(nodePayload[0]);
  36. Serial.print("Payload size: ");
  37. Serial.println(PAYLOAD_SIZE);
  38. Serial.println("***********************");
  39.  
  40. Wire.begin(NODE_ADDRESS); // Activate I2C network
  41. Wire.onRequest(requestEvent); // Request attention of master node
  42. }
  43.  
  44. void loop()
  45. {
  46.  
  47.  
  48. //package milliseconds
  49. unsigned long Now = millis();
  50. Serial.println(Now);
  51. for(int i = 0; i < 4; i++){
  52. uint8_t a = Now & 0xFF;
  53. nodePayload[i+1] = a;
  54. Serial.println(a);
  55. Now = Now >> 8;
  56. //Serial.println(Now);
  57. }
  58.  
  59. //package ultrasonic sensors
  60. unsigned long ultrasoundValue;
  61. for(uint8_t i=0; i<4; i++)
  62. {
  63. nodePayload[i+5] = sonar[i].ping_cm();
  64. delay(2);
  65. /*
  66. unsigned long ultrasoundValue = ping(i);
  67. // The speed of sound is 343.2 m/s or 2.9137529138 microseconds per millimeter.
  68. // The ping travels out and back, so to find the distance we need to divide
  69. // by double the time to find the distance to the object.
  70. unsigned long millimeters = ultrasoundValue*10000 / 5828;
  71. uint8_t b = millimeters & 0xFF;
  72. //usPayload[i] = b;
  73. nodePayload[i+4] = b;
  74. */
  75. }
  76. //nodePayload[0] = ; // I am sending Node address back. Replace with any other data
  77. //nodePayload[4] = analogRead(A0)/4; // Read A0 and fit into 1 byte. Replace this line with your sensor value
  78. //nodePayload[5] = 3;
  79. //nodePayload[6] = 14;
  80. //nodePayload[7] = 69;
  81. }
  82.  
  83. void requestEvent()
  84. {
  85. Wire.write(nodePayload,PAYLOAD_SIZE);
  86. //Serial.print("Sensor value: "); // for debugging purposes.
  87. //Serial.println(nodePayload[1]); // for debugging purposes.
  88. }
  89.  
  90. /*
  91. //Ping function
  92. unsigned long ping(int i)
  93. {
  94. pinMode(ultraSoundSignalPins[i], OUTPUT); //Switch signalpin to output
  95. digitalWrite(ultraSoundSignalPins[i], LOW); //Send low pulse
  96. delayMicroseconds(2); //Wait for 2 microseconds
  97. digitalWrite(ultraSoundSignalPins[i], HIGH); //Send high pulse
  98. delayMicroseconds(5); //Wait for 2 microseconds
  99. digitalWrite(ultraSoundSignalPins[i], LOW); //Send low pulse
  100. pinMode(ultraSoundEchoPins[i], INPUT); // Switch signalpin to input
  101. digitalWrite(ultraSoundEchoPins[i], HIGH); // Turn on pullup resistor
  102. unsigned long echo = pulseIn(ultraSoundEchoPins[i], HIGH); //Listen for echo
  103. return echo;
  104. }
  105. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement