/* 2018-01-18 Start designing The program send commands as a master to a slave (Arduino) and can receive a result. See also: FishFeederMaster.ino Use Serial.println(F("Some important text")); to keep the string in Flash memory and don not export to RAM. There is a lot of text. There is not enough RAM available. */ // sending - receiving data const int Data_Out = 2; //D2 const int Data_In = 3; //D3 // built in led const int Led_Pin = 13; //D13 built in led const int Time_Bit = 100; // time full bit length const int Time_Bit_Check = 5; // time to check if bit is available const int Time_Bit_Half = 50; // when first bit is found, delay to get middle of signal byte Byte_EF = 0; byte Byte_command = 0; byte Byte_number = 0; byte Byte_check = 0; byte Byte_check_c = 0; byte Byte_check_n = 0; void setup() { // sending - receiving data pinMode(Data_Out, OUTPUT); pinMode(Data_In, INPUT); // built in led pinMode(Led_Pin, OUTPUT); Serial.begin(115200); } void Reset(){ digitalWrite(Data_Out, LOW); digitalWrite(Led_Pin, LOW); Byte_EF = 0; Byte_command = 0; Byte_number = 0; Byte_check = 0; Byte_check_c = 0; Byte_check_n = 0; } void Send_Byte(byte Var_Send_Byte){ for (int i = 7; i>=0; i--){ if (bitRead(Var_Send_Byte, i) == HIGH){ digitalWrite(Data_Out, HIGH); digitalWrite(Led_Pin, HIGH); } else { digitalWrite(Data_Out, LOW); digitalWrite(Led_Pin, LOW); } delay(Time_Bit); } digitalWrite(Data_Out, LOW); digitalWrite(Led_Pin, LOW); } void Send_NEXT(){ // Send next step (1110 0000) Send_Byte(208); } void Send_OK(){ Send_Byte (209); // E1 OK } void Send_NOTOK(){ Send_Byte (218); // EA Not OK } void Send_EF(){ // EF, 239, Send this signal when Arduino Nano is ready Send_Byte(239); } byte Read_EF(){ // Read the EF byte byte Var_Read_Byte; while (digitalRead(Data_In) == LOW){ delay(Time_Bit_Check); } // Wait for signal delay(Time_Bit_Half); // Wait half a bit to middle of signal for (int i = 7; i>=0; i--){ if (digitalRead(Data_In) == HIGH){ bitWrite (Var_Read_Byte, i, 1); } else{ bitWrite (Var_Read_Byte, i, 0); } delay(Time_Bit); } return Var_Read_Byte; } byte Read_Byte(){ // Read the byte byte Var_Read_Byte; for (int i = 7; i>=0; i--){ if (digitalRead(Data_In) == HIGH){ bitWrite (Var_Read_Byte, i, 1); } else{ bitWrite (Var_Read_Byte, i, 0); } delay(Time_Bit); } return Var_Read_Byte; } void Stopbit(){ delay(Time_Bit); } byte Read_SerialMonitor(){; String Var_Read_String; byte Var_Read_Byte; Var_Read_String=(""); Serial.print(F("-->")); delay(5000); if(Serial.available() > 0){ Var_Read_String=Serial.readStringUntil('\n'); } Var_Read_Byte=Var_Read_String.toInt(); Serial.println(Var_Read_Byte); return Var_Read_Byte; } // Text Setup & Calibrate void Text_01() {Serial.println(F(" 1. Move casing knife valve to open sensor position to see silo knife valve."));} void Text_02() {Serial.println(F(" 2. Move silo knife valve to closed sensor position."));} void Text_03() {Serial.println(F(" 3. Move silo knife valve to closed position."));} void Text_04() {Serial.println(F(" 4. Move silo knife valve to open position."));} void Text_05() {Serial.println(F(" 5. Move silo knife valve to open sensor position."));} void Text_06() {Serial.println(F(" 6. Move casing knife valve to closed sensor position."));} void Text_07() {Serial.println(F(" 7. Move casing knife valve to closed position."));} void Text_08() {Serial.println(F(" 8. Move casing knife valve to open position."));} void Text_09() {Serial.println(F(" 9. Move casing knife valve to open sensor position."));} void Text_10() {Serial.println(F("10. Move silo knife valve and casing knife valve to closed position."));} void Text_11() {Serial.println(F("Store the variables to the EEPROM memory of Arduino."));} void Text_RE() {Serial.println(F(" READY ! "));} void Text_OK() {Serial.println(F(" OK Next step = 209"));} void Text_NO() {Serial.println(F(" NOT OK Redo step = 218"));} void Text_EF() {Serial.println(F(" EF Turn motor = 239"));} void Setup_11(){ // 11. Print the variables of Arduino. byte byte01; byte byte02; byte byte03; byte byte04; byte byte05; byte byte06; byte byte07; byte byte08; byte byte09; byte byte10; Send_EF(); Stopbit(); byte01 = Read_Byte(); byte02 = Read_Byte(); byte03 = Read_Byte(); byte04 = Read_Byte(); byte05 = Read_Byte(); byte06 = Read_Byte(); byte07 = Read_Byte(); byte08 = Read_Byte(); byte09 = Read_Byte(); byte10 = Read_Byte(); Serial.println(byte01); Serial.println(byte02); Serial.println(byte03); Serial.println(byte04); Serial.println(byte05); Serial.println(byte06); Serial.println(byte07); Serial.println(byte08); Serial.println(byte09); Serial.println(byte10); } void Setup(){ // Determine critical positions of valves and store valus in EEPROM. Max 250 steps 0.1mm = 25mm. int i = 1; while (i<=11){ Byte_command = 0; Read_EF(); if (i == 1) {Text_01();} if (i == 2) {Text_02();} if (i == 3) {Text_03();} if (i == 4) {Text_04();} if (i == 5) {Text_05();} if (i == 6) {Text_06();} if (i == 7) {Text_07();} if (i == 8) {Text_08();} if (i == 9) {Text_09();} if (i == 10) {Text_10();} if (i == 11) {Text_11();} if (i == 12) {Text_RE();} // Ready Read_EF(); if ( i==3 || i==4 || i==7 || i==8 ){ Text_EF(); Text_OK(); while (Byte_command != 209) { Byte_command = Read_SerialMonitor(); if ( Byte_command == 209 || Byte_command == 239 ) {Send_Byte (Byte_command);} } } if ( i==11 ){Setup_11();} i++; Stopbit(); // Wait before scanning for new } } void Calibrate(){ // Determine critical positions of valves and store valus in EEPROM. Max 250 steps 0.1mm = 25mm. int i = 1; while (i<=11){ Byte_command = 0; Read_EF(); if (i == 1) {Text_01();} if (i == 2) {Text_02();} if (i == 3) {Text_03();} if (i == 4) {Text_04();} if (i == 5) {Text_05();} if (i == 6) {Text_06();} if (i == 7) {Text_07();} if (i == 8) {Text_08();} if (i == 9) {Text_09();} if (i == 10) {Text_10();} if (i == 11) {Text_11();} if (i == 12) {Text_RE();} // Ready Read_EF(); if ( i==3 || i==4 || i==7 || i==8 ){ Text_EF(); Text_OK(); Text_NO(); while (Byte_command != 209) { Byte_command = Read_SerialMonitor(); if ( Byte_command == 209 || Byte_command == 218 || Byte_command == 239 ) {Send_Byte (Byte_command);} } } if ( i==11 ){Setup_11();} i++; } } void Test_switch(){ // Determine if the sensors are funtional. Read_EF(); Serial.println(F("Press the sensors 10x (be gentle)")); Read_EF(); Text_RE(); // Ready } void Test_photodiode(){ // Determine if the photodiode is functional. int Photo_Value = 0; Read_EF(); Serial.println(F("Reading light on sensor...")); for (int i = 0; i<=9; i++){ Read_EF(); Photo_Value = Read_Byte() * 4; // Read the value of the photodiode Serial.println(Photo_Value, DEC); } Read_EF(); Text_RE(); // Ready } void Feed(){ Read_EF(); Serial.println(F(" 1. Move silo knife valve to closed position.")); Read_EF(); Serial.println(F(" 2. Move silo knife valve to beginning of loading.")); Read_EF(); Serial.println(F(" 3. Load the food.")); Read_EF(); Serial.println(F(" 4. Testing photodiode.")); Read_EF(); Serial.println(F(" 5. Move silo knife valve to closed sensor position.")); Read_EF(); Serial.println(F(" 6. Move silo knife valve to closed position.")); Read_EF(); Serial.println(F(" 7. Move casing knife valve to open sensor position.")); Read_EF(); Serial.println(F(" 8. Move casing knife valve to closed sensor position.")); Read_EF(); Serial.println(F(" 9. Move casing knife valve to closed position.")); Read_EF(); Text_RE(); // Ready Read_EF(); Byte_number=Read_Byte(); Serial.println(Byte_number, DEC); } void loop() { Reset(); Byte_EF = Read_EF(); // EF, Wait for ready Slave Serial.print(Byte_EF, DEC); // print byte if (Byte_EF == 239){ // It is a EF signal Serial.println(" OK"); } else{ Serial.println(" Not OK"); } Serial.println(" "); Serial.println(F(" EF Begin of transmision = 239")); Serial.println(" "); Serial.println(F(" A1 Command 1, Setup = 161")); Serial.println(F(" A2 Command 2, Calibrate = 162")); Serial.println(F(" A3 Command 3, Test switch 10x = 163")); Serial.println(F(" A4 Command 4, Test photodiode = 164")); Serial.println(" "); Serial.println(F(" A9 Command 9, Feed fish = 169")); Serial.println(F(" 00 - 99 amount of fish food = 0-99")); Serial.println(" "); Serial.println(F(" B1 Turn motor 1 0.1mm right = 177")); Serial.println(F(" B2 Turn motor 1 0.1mm left = 178")); Serial.println(F(" B3 Turn motor 2 0.1mm right = 179")); Serial.println(F(" B4 Turn motor 2 0.1mm left = 180")); Serial.println(" "); Serial.println(F(" C1 Turn motor 1 1mm right = 193")); Serial.println(F(" C2 Turn motor 1 1mm left = 194")); Serial.println(F(" C3 Turn motor 2 1mm right = 195")); Serial.println(F(" C4 Turn motor 2 1mm left = 196")); Serial.println(" "); Text_OK; Text_NO; Serial.println(" "); Serial.println(F(" Run all options without fish food in silo, only option A9 is suitable with fish food")); Serial.println(" "); Serial.println(F("Enter Command Byte ")); Byte_command = Read_SerialMonitor(); Serial.println(F("Enter Number Byte ")); Byte_number = Read_SerialMonitor(); Send_EF(); // EF, Send this signal when Master is ready. Send_Byte (Byte_command); // Send command byte. Send_Byte (Byte_number); // Send number byte. Stopbit(); // Stopbit after sending before reading. Byte_check_c = Read_Byte(); // Read command byte to check. Byte_check_n = Read_Byte(); // Read number byte to check. if ((Byte_command == Byte_check_c) && (Byte_number == Byte_check_n)) { Send_OK(); // E1 OK } else { Send_NOTOK(); // EA Not OK } Serial.println(Byte_EF, DEC); Serial.println(Byte_command, DEC); Serial.println(Byte_number, DEC); Serial.println(Byte_check_c, DEC); Serial.println(Byte_check_n, DEC); if (Byte_command == 161) {Setup();} // A1, Command 1, Setup. if (Byte_command == 162) {Calibrate();} // A2, Command 2, Calibrate. if (Byte_command == 163) {Test_switch();} // A3, Command 3, Test switch 10x. if (Byte_command == 164) {Test_photodiode();} // A4, Command 4, Test photodiode. if (Byte_command == 169) {Feed();} // A9, Command 9, Feed the fish. }