การส่งserialไปควบคุมมีดั่งนี้
-M...EF จะสามมารถควบคุม PWM
-Pcal,...,...EF ส่งไปcalibrate ค่าPH
-cal,low,4
-cal,mid,7
-cal,high,10
-E..cal,...,...EF ส่งไปcalibrate ค่าEC
-cal,low,12000
-cal,high,80000
พี่ไนท์สอนการส่งserial ไปทำงานโดยจากการเลือกการส่งไปตอนพี่ไนท์ช่วยแก้โค้ดติดตรงที่ตัวแปร String กับ char โดยพยายามเปลี่ยนString เป็นchar
ตัวอย่างโค้ดที่ทำงาน
String inputstring = ""; //a string to hold incoming data from the PC
int inputstringLength=0;
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
String latter ="";
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 = 1; //set pwm out 0-50
int pwm = 9; // PWM connected to digital pin 9
int inputstring1 = 0;
void setup()
{ //set up the hardware
Serial.begin(115200); //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(30); //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_2 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(Serial.available()>0)
{
while(Serial.available()>0)
{
inputstring += char(Serial.read());
inputstringLength += 1;
delay(1);
}
}
for(int i=1;i<=inputstringLength;i++)
{
if((inputstring[i]=='F')&&(inputstring[i-1]=='E'))
{
Serial.println(inputstring);
if(inputstring[0]=='M')
{
String temp="";
temp.reserve(30);
for(int i=1;i<(inputstringLength-2);i++)
{
temp += inputstring[i];
}
Serial.println(temp);
}
if(inputstring[0]=='P')
{
String temp="";
temp.reserve(30);
for(int i=1;i<(inputstringLength-2);i++)
{
temp += inputstring[i];
}
Serial.println(temp);
}
if(inputstring[0]=='E')
{
String temp="";
temp.reserve(30);
for(int i=1;i<(inputstringLength-2);i++)
{
temp += inputstring[i];
}
Serial.println(temp);
}
inputstringLength=0;
inputstring="";
}
}
//
// 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
// }
//
//// if(inputstring1 <=50 ) //send to serial over 50
//// {
// Cur_time = millis(); //set Cur_time is millis
// s = Cur_time-Prev_time;
// if(s >= 10)
// {
// j = (counter*10.0)/360.0;
// k = j/(s*0.001);
// rpm = k*60.0; //rpm
//
// err = inputstring1 - rpm/100;
// sum_err += err;
// u = Kp*err + Ki*sum_err + Kd*(err-old_err);
// old_err = err;
// pwm_set = u*1.2;
// analogWrite(pwm,pwm_set);
//
// pwm_set+=0;
// counter=0;
// Prev_time = Cur_time;
// pwm_set=0;
// }
//// }
}

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