Atlas Scientificプローブからいくつかの値を読み取る arduino プロジェクトを作成しています。シリアル モニタを使用して 1 つのデバイスのみと通信する方法のサンプル コードがあり、それをポートして、シリアル モニタを使用して通信するプローブを「選択」しようとしています。問題は、サンプル コードが loop() でシリアル入力を待機するだけであることです。次のようになります。
void loop(){ //here we go....
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial2.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been recived 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 flage used to tell if we have recived a completed string from the Atlas Scientific product
}
}
つまり、基本的には、コマンドを入力して Enter キーを押すと、コマンドがプローブに送信されます。プローブはその魔法を実行し、値を報告します。かなり簡単です。ケース ステートメントを使用して、通信するプローブを選択しようとしています (このプロトコルを使用しない温度センサーもあります)。case ステートメント内で while ループを使用して、周辺機器と通信しようとしています。
void loop(){
// read the selection
if (Serial.available()>0) {
int inByte = Serial.read();
switch(inByte) {
case '1':
//read pH
Serial.print("pH");
while (inputstring != "break"){
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial3.print(inputstring); //send that string to the Atlas Scientific product
Serial.print(inputstring);
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been recived 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 flage used to tell if we have recived a completed string from the Atlas Scientific product
}
}
case '2':
//read EC
Serial.print("EC");
while (inputstring != "break"){
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial2.print(inputstring); //send that string to the Atlas Scientific product
Serial.print(inputstring); //debug
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been recived 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 flage used to tell if we have recived a completed string from the Atlas Scientific product
}
}
.
.
.
}
私が言ったように、while ループを使用して、個々のセンサーとの個々の端末シリアル通信を実行し、端末に「break」と入力して、ケース ステートメントに戻って別のセンサーを選択できるようにしようとしています。while ループのアイデアはまったく機能していません。コマンドをエコーするためにデバッグ行を追加しましたが、それも行っていません。これらのネストされたループを行うより良い方法はありますか?
参考までにプログラム全体を載せておきます。
/*
This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
An Arduino MEGA 2560 board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
**Type in a command in the serial monitor and the Atlas Scientific product will respond.**
**The data from the Atlas Scientific product will come out on the serial monitor.**
Code efficacy was NOT considered, this is a demo only.
The TX3 line goes to the RX pin of your product.
The RX3 line goes to the TX pin of your product.
Make sure you also connect to power and GND pins to power and a common ground.
Open TOOLS > serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL & CR".
*/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 30 on the Arduino
#define ONE_WIRE_BUS 32
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;
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
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;
void setup(void){ //set up the hardware
Serial.begin(9600); //set baud rate for the hardware serial port_0 to 38400
Serial3.begin(38400); //set baud rate for software serial port_3 to 38400
Serial2.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
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific produc
// locate devices on the bus
Serial.print("Locating temperature devices...");
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
for(int i=0;i<numberOfDevices; i++)
{
if(sensors.getAddress(tempDeviceAddress, i))
{
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
}
}
Serial.println("Type 1 to read pH, 2 to read EC, or 3 to read temperature.");
}
//for reading pH/EC
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
input_stringcomplete = true;
}
void serialEvent3(){ //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 serialEvent2(){ //if the hardware serial port_3 receives a char
char inchar = (char)Serial2.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
}
//for reading temp
void printTemperature(DeviceAddress deviceAddress)
{
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
void loop(){ //here we go....
// read the selection
int r = 0;
if (Serial.available()>0) {
int inByte = Serial.read();
switch(inByte) {
case '1':
//read pH
Serial.print("pH");
while (inputstring != "break"){
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial3.print(inputstring); //send that string to the Atlas Scientific product
Serial.print(inputstring); //debug
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been recived 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 flage used to tell if we have recived a completed string from the Atlas Scientific product
}
}
case '2':
//read EC
Serial.print("EC");
while (inputstring != "break"){
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
Serial2.print(inputstring); //send that string to the Atlas Scientific product
Serial.print(inputstring); //debug
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been recived 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 flage used to tell if we have recived a completed string from the Atlas Scientific product
}
}
case '3':
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
break;
}
}
}
ありがとう、マイク