Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program: I2C slave sender template for multi-node Arduino I2C network
- // Programmer: Hazim Bitar (techbitar.com)
- // Date: March 30, 2014
- // This example code is in the public domain.
- #include <Wire.h>
- #define NODE_ADDRESS 2 // Change this unique address for each I2C slave node
- #define PAYLOAD_SIZE 14 // Number of bytes expected to be received by the master I2C node
- byte nodePayload[PAYLOAD_SIZE]; //Payload array
- //setup ultrasonic sencors
- #include <NewPing.h>
- #define SONAR_NUM 4 // Number of sensors.
- #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
- NewPing sonar[SONAR_NUM] = { // Sensor object array.
- NewPing(3, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
- NewPing(5, 6, MAX_DISTANCE),
- NewPing(7, 8, MAX_DISTANCE),
- NewPing(9, 10, MAX_DISTANCE)
- };
- void setup()
- {
- nodePayload[0] = NODE_ADDRESS;
- nodePayload[1] = 3; // Mode #sensors/layout
- Serial.begin(115200);
- Serial.println("SLAVE SENDER PING NODE");
- Serial.print("Node address: ");
- Serial.println(nodePayload[0]);
- Serial.print("Payload size: ");
- Serial.println(PAYLOAD_SIZE);
- Serial.println("***********************");
- Wire.begin(NODE_ADDRESS); // Activate I2C network
- Wire.onRequest(requestEvent); // Request attention of master node
- }
- void loop()
- {
- //package milliseconds
- unsigned long Now = millis();
- Serial.println(Now);
- for(int i = 0; i < 4; i++){
- uint8_t a = Now & 0xFF;
- nodePayload[i+1] = a;
- Serial.println(a);
- Now = Now >> 8;
- //Serial.println(Now);
- }
- //package ultrasonic sensors
- unsigned long ultrasoundValue;
- for(uint8_t i=0; i<4; i++)
- {
- nodePayload[i+5] = sonar[i].ping_cm();
- delay(2);
- /*
- unsigned long ultrasoundValue = ping(i);
- // The speed of sound is 343.2 m/s or 2.9137529138 microseconds per millimeter.
- // The ping travels out and back, so to find the distance we need to divide
- // by double the time to find the distance to the object.
- unsigned long millimeters = ultrasoundValue*10000 / 5828;
- uint8_t b = millimeters & 0xFF;
- //usPayload[i] = b;
- nodePayload[i+4] = b;
- */
- }
- //nodePayload[0] = ; // I am sending Node address back. Replace with any other data
- //nodePayload[4] = analogRead(A0)/4; // Read A0 and fit into 1 byte. Replace this line with your sensor value
- //nodePayload[5] = 3;
- //nodePayload[6] = 14;
- //nodePayload[7] = 69;
- }
- void requestEvent()
- {
- Wire.write(nodePayload,PAYLOAD_SIZE);
- //Serial.print("Sensor value: "); // for debugging purposes.
- //Serial.println(nodePayload[1]); // for debugging purposes.
- }
- /*
- //Ping function
- unsigned long ping(int i)
- {
- pinMode(ultraSoundSignalPins[i], OUTPUT); //Switch signalpin to output
- digitalWrite(ultraSoundSignalPins[i], LOW); //Send low pulse
- delayMicroseconds(2); //Wait for 2 microseconds
- digitalWrite(ultraSoundSignalPins[i], HIGH); //Send high pulse
- delayMicroseconds(5); //Wait for 2 microseconds
- digitalWrite(ultraSoundSignalPins[i], LOW); //Send low pulse
- pinMode(ultraSoundEchoPins[i], INPUT); // Switch signalpin to input
- digitalWrite(ultraSoundEchoPins[i], HIGH); // Turn on pullup resistor
- unsigned long echo = pulseIn(ultraSoundEchoPins[i], HIGH); //Listen for echo
- return echo;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement