//**Master3**// #include // liquid crystal library #include // wire library #define SLAVE_ADDR 8 // slave address LiquidCrystal lcd(3,4,5,6,7,8); // LCD piouts int bcount; // counter to count bytes in response byte FreqVolTemp[3]; // array to hold variable from the slave void setup() { Wire.begin(); // begin wire communication for I2C Serial.begin(115200); // begin serial communication for debugging lcd.begin(16, 2); // begin LCD communication lcd.clear(); // clear LCD screen lcd.print("Volume:"); // LCD print "Volume:" lcd.setCursor(9,0); // set LCD cursor to 1st row 9th column } byte readI2C(int address) { // function fo rreading i2c values byte FVT; // variable to hold byte of data long entry = millis(); // Wire.requestFrom(address,1); //read one byte at a time while(Wire.available() == 0 && (millis() - entry < 100)) Serial.print("waiting"); // wait 100 milli secomds for data to stabilize if (millis() - entry < 100) FVT = Wire.read(); // place data into byte return FVT; } void loop(){ while (readI2C(SLAVE_ADDR) < 255){ Serial.print("waiting"); // until first byte has been recived print waiting } for(bcount = 0; bcount < 3 ;bcount++){ FreqVolTemp[bcount] = readI2C(SLAVE_ADDR); } for(int i = 0; i < 3; i++){ Serial.print(FreqVolTemp[i]); // serial print array Serial.print("\t"); // serial print a tab between each array values lcd.print(FreqVolTemp[1]); // LCD print the volume from the array lcd.setCursor(15,0); // set LCD cursor to the 1st row, 14th colunm lcd.print("%"); // LCD print "%" lcd.setCursor(0,1); // set LCD cursor to the 2nd row, 0th colunm //lcd.print("freq:"); // LCD print "freq:" //lcd.setCursor(0,6); // set LCD cursor to the 2nd row, 6th colunm lcd.print(FreqVolTemp[0]); // LCD print frequency variable from FreqVolTemp[0] array lcd.setCursor(12,1); // set LCD cursor to the 2nd row, 12th colunm // lcd.print("T:"); // LCD print "T:" fro tempurature //lcd.setCursor(14,1); // set LCD cursor to the 2nd row, 14th colunm lcd.print(FreqVolTemp[2]); // LCD print tempurature variable from FreqVolTemp[2] array } Serial.println(); lcd.write(1); delay(400); } //**Slave3**// #include #define SLAVE_ADDR 8 // slave address is 8 #define pulse_ip 9 // frequency input pin #define TempPin 15 // tempurature input pin int ontime,offtime; // ontime is the time in milliseconds that the waveform is high(4.5V), offtime is the time in milliseconds the waveform is low(0.5V) float freq,period,frequency; //frequency of the wave from is 1000000(msec)/( ontime(msec) + offtime(msec)) int Fmin = 6000; // max frequency of the system from experimentation int Fmax = 9100; // min frequency of the system from experimentation int Fdiff; // frequency range Fmax - Fmin float volume; // volume of grease in the tank float adc_val; // analog to digital(0-1024) value from LM35 temp sensor float temp; // temp value of the liquid in the tank / sensor int FreqVolTemp[3]; // array for the freuency volume and temperature (float values), one element per variable int bcount = 0; // counter to count bytes in response void setup(){ pinMode(pulse_ip,INPUT); // input pin that for the incoming waveform pinMode(TempPin, INPUT); // input pin fopr the LM35 tempurature sensor Wire.begin(SLAVE_ADDR); // join i2c bus with address #8 Wire.onRequest(requestEvent); // function to run when data requested from master } void requestEvent(){ byte FVT; // defineing a byte variable to hold the data switch (bcount){ // cycle through data case 0: // first response is always 255 FVT = 255; // marker for beginning of data transmission break; case 1: FVT = FreqVolTemp[0]; // frequency variable break; case 2: FVT = FreqVolTemp[1]; // volume variable break; case 3: FVT = FreqVolTemp[2]; // temperature variable break; } Wire.write(FVT); // send response back to master bcount = bcount + 1; // increment byte counter if (bcount > 3) bcount =0; } void loop(){ // void loop runs and updates the data in FreqVolTemp array every 0.4 seconds readFrequency(); // function for reading frequency and volume readTemp(); // function for reading temperature readVolume() delay(400); } void readFrequency(){ ontime = pulseIn(pulse_ip,HIGH); // on time in milli seconds offtime = pulseIn(pulse_ip,LOW); // low time in milli seconds period = ontime+offtime; // period of the waveform in milli seconds freq = 1000000.0/period; // frequency of the waveform in Hz if(period==0){ freq=0; } FreqVolTemp[0] = freq; // assigning the frequency to FreqVolTemp array Fdiff = Fmax - Fmin; // computing the differential of frequencies from the max value to the low value delay(10); } void readVolume(){ volume = ((Fmax-freq)/Fdiff)*100;// computing the volume from the frequencies of the sensor FreqVolTemp[1] = volume; // assigning the volume to FreqVolTemp array delay(10); } void readTemp(){ adc_val=analogRead(A1); // temperature readings from LM35 on pin A1 temp=(adc_val*4.85)/10; // temperature conversions to degree celcius FreqVolTemp[2] = temp; // assign temp to FreqVolTemp array delay(10); }