#include //include the wire object and set a variable h to represent #define SLAVE_ADDRESS 0x40 // slave address for the connection to your pi char ADC_Convert[8]; //To store the data of the ADC in the array so that it can be passed to the pi char DArray[8]; //To store the data of distance in the array so that it can be passed to the pi int AVERAGING_LOOP_SIZE = 100; int READ_PIN = 0; // Analogue pin for SharpIR sensor int DISTANCE_BUFFER = 1; //cm int MIN_DISTANCE = 7; // minimum distance that the sensor can record. in cm int MAX_DISTANCE = 60; // maximum distance that the sensor can record in cm int IGNORE_BUFFER_THRESHHOLD = 35; // Readings int thisDistance, lastDistance, ignoreBuffer, val; //decaring variables thisDistance(Distance) val(ADC value) String thisPrint, lastPrint; //declaring variables for printing the output int groundpin = 0; // write 0 to get ground int greenpin = 12; // select the pin for the green LED RGB int redpin = 13; // select the pin for the red LED RGB int DO = 2; //Pin for Digital Output - DO for sound sensor int DA = 3; // Pin for Analog Output - AO for sound sensor int threshold = 30;//Set minimum threshold for LED lit int sensorvalue = 0; void setup() { pinMode(READ_PIN, INPUT); pinMode(DA,INPUT); pinMode (redpin, OUTPUT);// for distance pinMode (greenpin, OUTPUT);// for distance pinMode (groundpin, OUTPUT);// for distance digitalWrite (groundpin, LOW); pinMode(7, OUTPUT);// led light for sound Serial.begin(9600); Wire.begin(SLAVE_ADDRESS); ignoreBuffer = 0; } void loop() { sensorvalue = analogRead(DA); //Read the analog value for sound Serial.print(" Sound Analog: "); Serial.print(sensorvalue); //Print the analog value Serial.print(" "); Serial.print("Digital: "); Serial.print(digitalRead(DO)); //Print the digital value if (sensorvalue >= threshold) { //Compare analog value with threshold digitalWrite(7, HIGH); } else{ digitalWrite(7,LOW);// off sound led } distance_loop(); } void distance_loop(){ delay(10); int adc = analogRead(READ_PIN); val = 0; // Averaging Loop for (int i = 0; i < AVERAGING_LOOP_SIZE ; i++) { val += analogRead(READ_PIN); } val = val / AVERAGING_LOOP_SIZE; thisDistance = (6762 / (val - 9)) - 4; if ((thisDistance < MIN_DISTANCE) || (thisDistance > MAX_DISTANCE)) { thisPrint = "No Object"; thisDistance=0; adc=0; } else if ((thisDistance < lastDistance + DISTANCE_BUFFER) || (thisDistance > lastDistance - DISTANCE_BUFFER)) { ignoreBuffer ++; if (ignoreBuffer > IGNORE_BUFFER_THRESHHOLD) { thisPrint = "Object is now at: " + String(thisDistance) + "cm."; lastDistance = thisDistance; ignoreBuffer = 0; } } if (thisPrint != lastPrint) { Serial.println(thisPrint); //printing the distance Serial.println(val); //printing the ADC lastPrint = thisPrint; } if (thisDistance >=15 && thisDistance<=30) { //if distance is between 15cm and 30cm, it will light up the red led and turn off the green led digitalWrite(greenpin,LOW); digitalWrite(redpin, HIGH); } if (thisDistance>=31 && thisDistance<=60) { //light up the green led and turn off the red led when the distance is between 31cm and 60cm digitalWrite(redpin,LOW); digitalWrite(greenpin, HIGH); } if (thisDistance<14 || thisDistance>60){ //to turn off both the green led and red led when the distance is less than 14cm and more than 60cm digitalWrite(greenpin,LOW); digitalWrite(redpin,LOW); } Serial.println("Distance:"+thisDistance); dtostrf(adc, 4, 0, ADC_Convert); //converts the ADC value to string so that it can be passed to the pi dtostrf(thisDistance, 4, 1, DArray); //converts the Distance value to string so that it can be passed to the pi Wire.onRequest(sendDataToPi); // calling the method sendDataToPi so that it will write the information to the pi } void sendDataToPi(){ Wire.write(DArray); //writing the distance data to the pi Wire.write(ADC_Convert); //writing the ADC data to the pi }