#include #include #include #include #include #include int masu = 85; //weight of masu int sake = 18; //weight of sake int pour = 0; HX711 scale1; #define scale1_DOUT 3 #define scale1_SCK 2 float calibration_factor1 = -445; int weight1 = 0; HX711 scale2; #define scale2_DOUT 5 #define scale2_SCK 4 float calibration_factor2 = 1200; int weight2 = 0; HX711 scale3; #define scale3_DOUT 1 #define scale3_SCK 0 float calibration_factor3 = 1170; int weight3 = 0; int ledPin = 12; // LED connected to digital pin 12 int switchPin = A0; // Switch to behave scale weight........................add Servo myservo1; // create servo1 object to control a servo Servo myservo2; // create servo2 object to control a servo int pos = 0; // change the servo degrees int pos_now = 1; int Liquid_level = 0; // received liquid_level from Secondary..............add //int int_x = 0; // .........................................................add void setup() { Serial.begin(9600); scale1.begin(scale1_DOUT, scale1_SCK); scale1.set_scale(); scale1.tare(); //Reset the scale to 0 scale2.begin(scale2_DOUT, scale2_SCK); scale2.set_scale(); scale2.tare(); //Reset the scale2 to 0 scale3.begin(scale3_DOUT, scale3_SCK); scale3.set_scale(); scale3.tare(); //Reset the scale3 to 0 myservo1.attach(8); // attaches the servo1 on pin 8 (PWM) to the servo object myservo2.attach(9); // attaches the servo2 on pin 9 (PWM) to the servo object //Set the servos to initial positions myservo1.write(0); myservo2.write(135); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); //pinMode (switchPin, INPUT_PULLUP); Wire.swap(1); Wire.begin(); } void loop() { Serial.println("Loop"); scale1.set_scale(calibration_factor1); //Adjust to this calibration factor1 scale2.set_scale(calibration_factor2); //Adjust to this calibration factor2 scale3.set_scale(calibration_factor3); //Adjust to this calibration factor3 Serial.print("Scale1_Reading: "); Serial.print(scale1.get_units(), 1); Serial.print("g"); Serial.println(); weight1 = scale1.get_units(); Serial.print("Scale2_Reading: "); Serial.print(scale2.get_units(), 1); Serial.print("g"); Serial.println(); weight2 = scale2.get_units(); Serial.print("Scale3_Reading: "); Serial.print(scale3.get_units(), 1); Serial.print("g"); Serial.println(); weight3 = scale3.get_units(); Serial.println(weight1); if (weight1 < 75) { // do nothing Serial.println("do nothing under 75g"); } else if (weight1 > 90) { // do nothing Serial.println("do nothing over 90g"); } else { // Servo1 if ((weight1 >= 75) && (weight1 <= 90)) { //If the weight1 is 75-90g Serial.println("masu is ON"); // Servo2 for (pos = 135; pos >= 45; pos -= 1) { // goes from 135 degrees to 45 degrees myservo2.write(pos); Serial.print(scale1.get_units(), 1); Serial.print("g"); Serial.println(); weight1 = scale1.get_units(); Serial.println("pouring Slowly"); pos_now = pos; if (weight1 <= 10) {//マスが落ちたら Serial.println("masu falls"); break; //pos = 135; //myservo2.write(pos); } if (weight1 >= (masu + sake) ) { //85 + 18=103g break; } delay(50); } // Servo2 for (pos = pos_now; pos <= 135; pos += 1) { // goes from now pos degrees to 135 degrees myservo2.write(pos); delay(50); } //mp3 Wire.beginTransmission(8) ; // Secondary address is 8 Wire.write("2", strlen("2")); // Send 2 to secondary Wire.endTransmission(); Serial.print("sent "); Serial.println("2"); delay(500); pour = pour + 1; Serial.print("pour= "); Serial.print(pour); Serial.println(" not_empty"); delay(500); if (pour >= 3) { Serial.println("empty now request to Refill"); delay(3000); Wire.beginTransmission(8) ; // Secondary address is 8 Wire.write("1", strlen("1")); // Send 1 to secondary Wire.endTransmission(); Serial.print("sent "); Serial.println("1"); delay(3000); while (1) { // loop forever Wire.requestFrom(8, 1); // request 1 bytes from secondary device #8 Serial.println("request Liquid_level"); while (Wire.available()) { Liquid_level = Wire.read(); // receive from secondary Serial.print("Liquid_level="); Serial.println(char(Liquid_level));// "1" is ASCII code 49 } if (Liquid_level == '1') {//Liquid is full break; } delay(5000); }//while(1) Serial.println("break"); pour = 0; }//if (pour >= 3) }//if }//else delay(500); //}//while //}//if //}//else delay(500); Serial.println(weight2); if (weight2 < 75) { // do nothing Serial.println("do nothing under 75g"); } else if (weight2 > 90) { // do nothing Serial.println("do nothing over 90g"); } else { // Servo1 if ((weight2 >= 75) && (weight2 <= 90)) { //If the weight1 is 75-90g Serial.println("masu is ON"); for (pos = 0; pos <= 90; pos += 1) { myservo1.write(pos); delay(20); } // Servo2 for (pos = 135; pos >= 45; pos -= 1) { // goes from 135 degrees to 45 degrees myservo2.write(pos); Serial.print(scale2.get_units(), 1); Serial.print("g"); Serial.println(); weight2 = scale2.get_units(); Serial.println("pouring Slowly"); pos_now = pos; if (weight2 <= 10) {//マスが落ちたら Serial.println("masu falls"); break; //pos = 135; //myservo2.write(pos); } if (weight2 >= (masu + sake) ) { //85 + 18=103g break; } delay(50); } // Servo2 for (pos = pos_now; pos <= 135; pos += 1) { // goes from 45 degrees to 135 degrees myservo2.write(pos); delay(50); } // Servo1 for (pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees // in steps of 1 degree myservo1.write(pos); // tell servo1 to go to position in variable 'pos' delay(20); // waits 30ms for the servo to reach the position // } //mp3 Wire.beginTransmission(8) ; // Secondary address is 8 Wire.write("3", strlen("3")); // Send 3 to secondary Wire.endTransmission(); Serial.print("sent "); Serial.println("3"); delay(500); pour = pour + 1; Serial.print("pour= "); Serial.print(pour); Serial.println(" not_empty"); delay(500); if (pour >= 3) { Serial.println("empty now request to Refill"); delay(3000); Wire.beginTransmission(8) ; // Secondary address is 8 Wire.write("1", strlen("1")); // Send 1 to secondary Wire.endTransmission(); Serial.print("sent "); Serial.println("1"); delay(3000); while (1) { // loop forever Wire.requestFrom(8, 1); // request 1 bytes from secondary device #8 Serial.println("request Liquid_level"); while (Wire.available()) { Liquid_level = Wire.read(); // receive from secondary Serial.print("Liquid_level="); Serial.println(char(Liquid_level));// "1" is ASCII code 49 } if (Liquid_level == '1') {//Liquid is full break; } delay(5000); }//while(1) Serial.println("break"); pour = 0; }//if (pour >= 3) }//if }//else delay(500); //}//while //}//if //}//else delay(500); Serial.println(weight3); if (weight3 < 75) { // do nothing Serial.println("do nothing under 75g"); } else if (weight3 > 90) { // do nothing Serial.println("do nothing over 90g"); } else { // Servo1 if ((weight3 >= 75) && (weight3 <= 90)) { //If the weight1 is 75-90g Serial.println("masu is ON"); for (pos = 0; pos <= 180; pos += 1) { myservo1.write(pos); delay(20); } // Servo2 for (pos = 135; pos >= 45; pos -= 1) { // goes from 135 degrees to 45 degrees myservo2.write(pos); Serial.print(scale3.get_units(), 1); Serial.print("g"); Serial.println(); weight3 = scale3.get_units(); Serial.println("pouring Slowly"); pos_now = pos; if (weight3 <= 10) {//マスが落ちたら Serial.println("masu falls"); break; // pos = 135; //myservo2.write(pos); } if (weight3 >= (masu + sake) ) { //85 + 18=103g break; } delay(50); } // Servo2 for (pos = pos_now; pos <= 135; pos += 1) { // goes from 45 degrees to 135 degrees myservo2.write(pos); delay(50); } // Servo1 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees // in steps of 1 degree myservo1.write(pos); // tell servo1 to go to position in variable 'pos' delay(20); // waits 30ms for the servo to reach the position // } //mp3 Wire.beginTransmission(8) ; // Secondary address is 8 Wire.write("4", strlen("4")); // Send 4 to secondary Wire.endTransmission(); Serial.print("sent "); Serial.println("4"); delay(500); pour = pour + 1; Serial.print("pour= "); Serial.print(pour); Serial.println(" not_empty"); delay(500); if (pour >= 3) { Serial.println("empty now request to Refill"); delay(3000); Wire.beginTransmission(8) ; // Secondary address is 8 Wire.write("1", strlen("1")); // Send 1 to secondary Wire.endTransmission(); Serial.print("sent "); Serial.println("1"); delay(3000); while (1) { // loop forever Wire.requestFrom(8, 1); // request 1 bytes from secondary device #8 Serial.println("request Liquid_level"); while (Wire.available()) { Liquid_level = Wire.read(); // receive from secondary Serial.print("Liquid_level="); Serial.println(char(Liquid_level));// "1" is ASCII code 49 } if (Liquid_level == '1') {//Liquid is full break; } delay(5000); }//while(1) Serial.println("break"); pour = 0; }//if (pour >= 3) }//if }//else delay(500); //}//while //}//if //}//else delay(500); if (pour >= 3) { Serial.println("empty now request to Refill"); delay(3000); } } //loop