-การใช้ String
Examples
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
แหล่งที่มา https://www.arduino.cc/en/Reference/String-ส่วนการทดลองดูได้ในคริป
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(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_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]=='Y')&&(inputstring[i-1]=='T'))
{
Serial.println(inputstring);
if(inputstring[0]=='P')
{
String temp="";
char(temp.reserve(30));
for(int i=1;i<(inputstringLength-2);i++)
{
temp += inputstring[i];
}
Serial.print("temp:");
Serial.println(temp);
input_stringcomplete = true;
if (input_stringcomplete)
{
Serial3.print(inputstring); //send that string to the Atlas Scientific product
inputstring="";
input_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the PC
}
}
inputstring="";
inputstringLength=0;
}
}
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.println("\t");
sensor_stringcompletePH = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
}
}

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