Problem Regarding: Automatically input command into the serial monitor

Hello Guys,

I would like some helps with the EC Kit from Atlas Scientific. ( https://www.atlas-scientific.com/product_pages/kits/ec-kit.html )
My problem is that for the EC Kit to start working and measuring the electrical conductivity value, I have to connect it to a PC and input the command “c” everytime.

This doesn’t follow my intention as I want it to automatically input the command “c” and start measuring right away after I power up my arduino without having to put in the command via PC. I’ve tried messing around with my code to get over this problem but I still can’t find myself a solution.

Could you please help me fix this problem? Thank you :slight_smile:

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product
int valve = 13;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     pinMode(valve, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("HYDROPONICS FMU");
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port receives a char
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  
 

 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }
 

  while (myserial.available()) {                                               //while a char is holding in the serial buffer
         char inchar = (char)myserial.read();                                  //get the new char
         sensorstring += inchar;                                               //add it to the sensorString
         if (inchar == '\r') {sensor_stringcomplete = true;}                   //if the incoming character is a <CR>, set the flag
         }


   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.print(sensorstring);                                             //use the hardware serial port to send that data to the PC
       lcd.setCursor(0, 1);
       lcd.print("EC:");
       lcd.print(sensorstring.substring(0,3));
       lcd.print(" uS/cm");
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
   
}

To make a program, the real world has to be implemented into program functionality. Looking at the code I would have done it in a different way and used different variable names. They have added many comments, so that is good.

To do something just once at startup, you have to do that in the setup() function.
They use a String class to build a string for the EC kit, but you don't have to do that. A normal string will also work.

I have added a delay of half a second, perhaps it is not needed, perhaps a few seconds are needed.

void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     pinMode(valve, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("HYDROPONICS FMU");

     // Added section to start the EC kit
     delay (500); // a delay to be sure that the EC kit has started.
     myserial.print ("c\r");  // send a 'c' and a '\r' CarriageReturn
     }

Hi Caltoa!

Really thank you for your help! It works!
Thanks for the suggestions. That really helps me.

I really appreciate it. :slight_smile: