Guest User

Untitled

a guest
May 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. //This code was written to be easy to understand.
  2. //Code efficiency was not considered.
  3. //Modify this code as you see fit.
  4. //This code will output data to the Arduino serial monitor.
  5. //Type commands into the Arduino serial monitor to control the EC circuit.
  6. //This code was written in the Arduino 1.6.5 IDE
  7. //An Arduino UNO was used to test this code.
  8.  
  9.  
  10. #include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it
  11. #define rx 2                                          //define what pin rx is going to be
  12. #define tx 3                                          //define what pin tx is going to be
  13.  
  14. SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work
  15.  
  16.  
  17. String inputstring = "";                              //a string to hold incoming data from the PC
  18. String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
  19. boolean input_string_complete = false;                //have we received all the data from the PC
  20. boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
  21.  
  22.  
  23.  
  24.  
  25. void setup() {                                        //set up the hardware
  26.   Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
  27.   myserial.begin(9600);                               //set baud rate for the software serial port to 9600
  28.   inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  29.   sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
  30. }
  31.  
  32.  
  33. void serialEvent() {                                  //if the hardware serial port_0 receives a char
  34.   inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  35.   input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
  36. }
  37.  
  38.  
  39. void loop() {                                         //here we go...
  40.  
  41.   if (input_string_complete) {                        //if a string from the PC has been received in its entirety
  42.     myserial.print(inputstring);                      //send that string to the Atlas Scientific product
  43.     myserial.print('\r');                             //add a <CR> to the end of the string
  44.     inputstring = "";                                 //clear the string
  45.     input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  46.   }
  47.  
  48.   if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
  49.     char inchar = (char)myserial.read();              //get the char we just received
  50.     sensorstring += inchar;                           //add the char to the var called sensorstring
  51.     if (inchar == '\r') {                             //if the incoming character is a <CR>
  52.       sensor_string_complete = true;                  //set the flag
  53.     }
  54.   }
  55.  
  56.  
  57.   if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
  58.     if (isdigit(sensorstring[0]) == false) {          //if the first character in the string is a digit
  59.       Serial.println(sensorstring);                   //send that string to the PC's serial monitor
  60.     }
  61.     else                                              //if the first character in the string is NOT a digit
  62.     {
  63.       print_EC_data();                                //then call this function
  64.     }
  65.     sensorstring = "";                                //clear the string
  66.     sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  67.   }
  68. }
  69.  
  70.  
  71.  
  72. void print_EC_data(void) {                            //this function will pars the string  
  73.  
  74.   char sensorstring_array[30];                        //we make a char array
  75.   char *EC;                                           //char pointer used in string parsing
  76.   char *TDS;                                          //char pointer used in string parsing
  77.   char *SAL;                                          //char pointer used in string parsing
  78.   char *GRAV;                                         //char pointer used in string parsing
  79.   float f_ec;                                         //used to hold a floating point number that is the EC
  80.  
  81.   sensorstring.toCharArray(sensorstring_array, 30);   //convert the string to a char array
  82.   EC = strtok(sensorstring_array, ",");               //let's pars the array at each comma
  83.   TDS = strtok(NULL, ",");                            //let's pars the array at each comma
  84.   SAL = strtok(NULL, ",");                            //let's pars the array at each comma
  85.   GRAV = strtok(NULL, ",");                           //let's pars the array at each comma
  86.  
  87.   Serial.print("EC:");                                //we now print each value we parsed separately
  88.   Serial.println(EC);                                 //this is the EC value
  89.  
  90.   Serial.print("TDS:");                               //we now print each value we parsed separately
  91.   Serial.println(TDS);                                //this is the TDS value
  92.  
  93.   Serial.print("SAL:");                               //we now print each value we parsed separately
  94.   Serial.println(SAL);                                //this is the salinity value
  95.  
  96.   Serial.print("GRAV:");                              //we now print each value we parsed separately
  97.   Serial.println(GRAV);                               //this is the specific gravity
  98.   Serial.println();                                   //this just makes the output easier to read
  99.  
  100. //f_ec= atof(EC);                                     //uncomment this line to convert the char to a float
  101. }
Add Comment
Please, Sign In to add comment