Hello,
I am working on a sketch trying to use the serial monitor to enter some values using for loops. I want these values to be stored for comparison until they are changed again by entering new values into the serial monitor. The sketch starts up and prompts for values to be entered. The idea is these values could be compared to the String called inputString coming in from the software serial port. An example would be if the first 5 values were 100,200,300,400,500 and the inputString was “100 Test String”, then a comparison could be done using the int “header” and “ value[]”. Everything works well except the part
if ((header == values[0])||(header == values[1])||(header == values[2])
||(header == values[3])||(header == values[4]));
{
Serial.print ("header and value match");
This part of sketch always prints regardless of what inputString sends
I would appreciate help with this and also any suggestions on my code. Code is attached.
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 2, RX = digital pin 3
SoftwareSerial portOne(2, 3);
const int NUMBER_OF_FIELDS = 5;
int fieldIndex =0;
int values [NUMBER_OF_FIELDS];
const int NUMBER_OF_FIELDS2 = 5;
int fieldIndex2 =0;
int values2 [NUMBER_OF_FIELDS2];
char inByte;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int header;
String stringOne, stringTwo;
void setup() {
// initialize serial:
Serial.begin(9600);
portOne.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
Serial.println ("To begin entering numbers for values 1-5");
Serial.println ("Press the A key then enter room numbers.");
Serial.println ("Numbers must be separated with a comma. Ex. A,100,200,300,400,500");
Serial.println ("Press the Enter key when finished");
Serial.println ('\r');
}
void loop() {
while (Serial.available() > 0)
{
char inByte = Serial.read();
if (inByte == 'A')
{
for (fieldIndex = 0; fieldIndex < 5; fieldIndex ++)
{
values[fieldIndex] = Serial.parseInt();
}
Serial.print ('\n');
Serial.print(fieldIndex);
Serial.println(" fields recieved:");
// for(int i=0; i < fieldIndex; i++)
{
Serial.print(values[0]);
Serial.print(',');
Serial.print(values[1]);
Serial.print(',');
Serial.print(values[2]);
Serial.print(',');
Serial.print(values[3]);
Serial.print(',');
Serial.print(values[4]);
Serial.print ('\n');
Serial.print ('\n');
Serial.println ("To begin entering numbers for values 6-10");
Serial.println ("Press the B key then enter room numbers.");
Serial.println ("Numbers must be separated with a comma. Ex. B,100,200,300,400,500");
Serial.println ("Press the Enter key when finished");
}
fieldIndex = 0;
}
if (inByte == 'B')
{
for (fieldIndex2 = 0; fieldIndex2 < 5; fieldIndex2 ++)
{
values2[fieldIndex2] = Serial.parseInt();
}
Serial.print ('\n');
Serial.print(fieldIndex2);
Serial.println(" fields2 recieved:");
// for(int i=0; i < fieldIndex; i++)
{
Serial.print(values2[0]);
Serial.print(',');
Serial.print(values2[1]);
Serial.print(',');
Serial.print(values2[2]);
Serial.print(',');
Serial.print(values2[3]);
Serial.print(',');
Serial.print(values2[4]);
Serial.print ('\n');
}
fieldIndex2 = 0;
}
}
// print the string when a newline arrives:
if (stringComplete){
Serial.println(inputString);
{
char temp[5];
inputString.substring(0,3).toCharArray(temp,sizeof(temp));
header = atoi(temp);
Serial.print ("header = ");
Serial.println(header);
}
if ((header == values[0])||(header == values[1])||(header == values[2])
||(header == values[3])||(header == values[4])||(header == values[5]));
{
Serial.print ("header and value match");// This part of sketch always prints regardless
// of what inputString sends
}
}
// clear the string:
inputString = "";
stringComplete = false;
checkInput();
}
/*
SerialEvent occurs whenever new data comes in the
software serial RX. This routine is run each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void checkInput() {
while (portOne.available()) {
// get the new byte:
char inChar = (char)portOne.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a return, set a flag
// so the main loop can do something about it:
if (inChar == '\r') {
stringComplete = true;
}
}
}