วันพฤหัสบดีที่ 25 มิถุนายน พ.ศ. 2558

วันที่28 ที่Fibo

-นำ conductivity กับ PH sensor และmotor PID ม่รวมกันในบอร์ด arduino mega 2560 และcralibrate conductivity กับ PH sensorได้ แต่ยังควบคุม RPMยังไม่ได้เพราะยังไม่ลองทำ
-การส่งcralibrate
cal,low,12000
cal,mid
cal,high,80000

-โค้ด ที่ช่วยในการศึกษาหาความรู้ 

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


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                       //set baud rate for the hardware serial port_0 to 9600
     Serial3.begin(38400);                                                      //set baud rate for software serial port_3 to 9600
     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
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port_0 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 serialEvent2(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_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 received in its entirety 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flag used to tell if we have received a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been received in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      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
      }
 }


-โค้ดต่อมาคือโค้ดที่ทำงานได้จริง



String inputstring = "";                                                       //a string to hold incoming data from the PC
boolean input_stringcomplete = false;                                          //have we received all the data from the PC

String sensorstringPH = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean sensor_stringcompletePH = false;                                         //have we received all the data from the Atlas Scientific product

String sensorstringEC = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean sensor_stringcompleteEC = false;                                         //have we received all the data from the Atlas Scientific product

float time1 = 0.0;
float time2 = 0.0;
float s=0.0;
float counter = 0.0;
float j = 0.0;
float k=0.0;
float rpm=0.0;
float rpm_set = 0.0;
float err = 0;
float sum_err = 0;
float old_err = 0;
float Kp = 1;           //ค่าคงที่ kp
float Ki = 0.5;         //ค่าคงที่ki
float Kd = 1;           //ค่าคงที่kd
float u = 0;
float pwm_set = 0;

unsigned long Cur_time=0;
unsigned long Prev_time=0;

int latter1 = 0;                                                                //set pwm out 0-50 
int pwm = 9;                                                                    // PWM connected to digital pin 9

  void setup()
  {                                                                             //set up the hardware
     Serial.begin(38400);                                                       //set baud rate for the hardware serial port_0 to 38400
     Serial2.begin(38400);                                                      //set baud rate for software serial port_2 to 38400
     Serial3.begin(38400);                                                      //set baud rate for software serial port_3 to 38400
     
     inputstring.reserve(5);                                                    //set aside some bytes for receiving data from the PC
     sensorstringPH.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
                                                    
     sensorstringEC.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     
     attachInterrupt(0,Millis,RISING);
      //LOW to trigger the interrupt whenever the pin is low,
      //CHANGE to trigger the interrupt whenever the pin changes value
      //RISING to trigger when the pin goes from low to high,
      //FALLING for when the pin goes from high to low.
      pinMode(pwm,OUTPUT);                                                          //PWM
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
  }
 
 
 
   void serialEvent() 
   {                                                                              //if the hardware serial port_0 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 serialEvent2()
  {                                                                              //if the hardware serial port_3 receives a char 
              char incharEC = (char)Serial2.read();                              //get the char we just received
              sensorstringEC += incharEC;                                        //add it to the inputString
              if(incharEC == '\r') {sensor_stringcompleteEC = true;}               //if the incoming character is a <CR>, set the flag 
  }
  void serialEvent3()
  {                                                                              //if the hardware serial port_3 receives a char 
              char incharPH = (char)Serial3.read();                              //get the char we just received
              sensorstringPH += incharPH;                                        //add it to the inputString
              if(incharPH == '\r') {sensor_stringcompletePH = true;}               //if the incoming character is a <CR>, set the flag 
  }
  
  void Millis()
{
  counter++;
}


 void loop(){                                                            
   if (input_stringcomplete)
      {                                                                          //if a string from the PC has been received in its entirety 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flag used to tell if we have received a completed string from the PC
      }

 if (sensor_stringcompletePH)
      {                                                                         //if a string from the Atlas Scientific product has been received in its entierty 
      Serial.print( "PH:");
      Serial.print(sensorstringPH);                                            //send that string to to the PC's serial monitor
      sensorstringPH = "";                                                       //clear the string:
      Serial.print("\t");  
      sensor_stringcompletePH = false;                                           //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }

 if (sensor_stringcompleteEC)
      {                                                                         //if a string from the Atlas Scientific product has been received in its entierty 
      Serial.print( "EC:");
      Serial.print(sensorstringEC);                                            //send that string to to the PC's serial monitor
      sensorstringEC = "";                                                       //clear the string:
      Serial.println("\t");  
      sensor_stringcompleteEC = false;                                           //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
 }









ไม่มีความคิดเห็น:

แสดงความคิดเห็น