I plan on using 2 different serial devices from Atlas Scientific. One is a pH probe, the other is a dissolved oxygen probe. I dont have any code written for these as of yet but was wondering. They both will communicate with a Mega through serial connections. One would be Serial3 and the other Serial2. These probes from time to time need to be calibrated. I know with the both the manufacturer says to end your calibration strings on the serial monitor with "/r" so to calibrate the pH probe with a solution of ph: 7.00 you would enter CAL,mid,7.00/r a solution of 4.00 and 10.00 would be CAL,low,4.00/r and CAL,high,10.00/r.
here is the code they have for the DO circuit:
String inputstring = "";
String sensorstring = "";
boolean input_stringcomplete = false;
boolean sensor_stringcomplete = false;
//a string to hold incoming data from the PC
//a string to hold the data from the Atlas Scientific product
//have we received all the data from the PC
//have we received all the data from the Atlas Scientific
//product
void setup(){
Serial.begin(9600);
Serial3.begin(9600);
inputstring.reserve(5);
sensorstring.reserve(30);
}
//set up the hardware
//set baud rate for the hardware serial port_0 to 9600
//set baud rate for software serial port_3 to 9600
//set aside some bytes for receiving data from the PC
//set aside some bytes for receiving data from Atlas Scientific
//product
void serialEvent() {
char inchar = (char)Serial.read();
inputstring += inchar;
if(inchar == '\r') {input_stringcomplete = true;}
}
//if the hardware serial port_0 receives
//a char
//get the char we just received
//add it to the inputString
//if the incoming character is a <CR>,
//set the flag
void serialEvent3(){
char inchar = (char)Serial3.read();
sensorstring += inchar;
if(inchar == '\r') {sensor_stringcomplete = true;}
}
//if the hardware serial port_3 receives
//a char
//get the char we just received
//add it to the inputString
//if the incoming character is a <CR>,
//set the flag
void loop(){
if (input_stringcomplete){
Serial3.print(inputstring);
inputstring = "";
input_stringcomplete = false;
}
//here we go...
//if a string from the PC has been received in its entierty
//send that string to the Atlas Scientific product
//clear the string:
//reset the flag used to tell if we have received a completed
//string from the PC
if (sensor_stringcomplete){
Serial.println(sensorstring);
sensorstring = "";
sensor_stringcomplete = false;
}
}
//if a string from the Atlas Scientific product has been
//received in its entierty
//send that string to to the PC's serial monitor
//clear the string:
//reset the flag used to tell if we have received a
//completed string from the Atlas Scientific product
My question is for the DO probe could I just simply change the string names to inputstring1 and sensorstring1, change the Serial3 on this code to Serial2, and then change the inchar part from /r to say /c and effectively change this to enable me to Send "CAL,mid,7.00/r" to calibrate the pH probe and then "CAL/c" (the command to calibrate the DO probe to atmospheric Oxygen levels)?
However /r is the code for a carraige-return character. I presume you would also use that with the other serial device. I have no idea what /c is.
You may run into problems using Strings (capital S) as they don't work well in the small memory of an Arduino - they can cause strange problems when a program is running for a time because they corrupt memory. It is better to use strings (small s) which are char arrays terminated by 0.
"/r" is a string that contains a slash character and an 'r'.
"\r" is a string that contains a single carriage-return character.
Know the difference. It just might save your life.
(It just amazes me how many times people confuse slash with backslash, or vice versa. You don't see as much confusion between 'p' and 'q', or between '^' and 'v', or between '(' and ')'. But between a slash and a backslash? Major confusion!)
Umm.. I know the difference. I was simply asking if there was a way, through the Serial Monitor, to send a command to each circuit separately. the /r /c thing was asking if I ended the command with a different character would there be a way to only talk to one or the other. And from what zoomkat says, Im learning that may not be the case. But, the 4 port UART from Atlas Scientific is looking like a solution... unless someone else can help me come up with a viable option.
I plan to use the mega. How do I use the serial monitor to send commands to only say Serial3 to calibrate that device, and then to Serial2 to calibrate THAT device. I guess that is what I am trying to figure out. I think I can get the information FROM these devices just by changing the names of of the Strings, strings, char(s) or whatever gets used at that time. The issue I foresee is when I try to calibrate these devices independently. which Is why this guy is looking like a viable option. It seems that I could tell it "1:CAL,mid,7.00\r" to calibrate the pH probe, and then "2:CAL\r" to calibrate the DO probe. Unless you know a better way that doesn't require me to purchase additional hardware.
You can send command like "1:CAL,mid,7.00\r" from the Arduino IDE's Serial Monitor (or other serial monitor software), have the Arduino parse the command as X:Y, where X is the serial port and Y the command to send that serial port. Then have a simple if statement like..
if ( X == 0 )
Serial3.print( Y );
else if ( X == 1 )
Serial2.print( Y );
adamlwvdc36:
Umm.. I know the difference. I was simply asking if there was a way, through the Serial Monitor, to send a command to each circuit separately.
I had not understood that from your earlier post.
It would be easy to include something in the message from the Serial Monitor that the Arduino would use to identify which device it was to pass the message to. For example you could preface a message with (say) 'A' or 'B' and the Arduino would strip those off before passing on the rest of the message.
...R
PS I just noticed that I am pretty-much repeating @guix's advice. The Thread serial input basics includes a parse example.
The below code uses the concept of using an identifier in the data sent to identify what component the data is being used for. You might also be able to use a multiplex chip to multiplex the TTL commands to the different components.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}