OK, here we go. The example is coded here: http://www.smacula.co.uk/2011/07/arduino-serial-communication.html
This first one compiles fine; everything is included in the .ino.
//Project tester.ino, whole
String command;
String temp1,temp2;
char inByte;
char carray[6];
int a,b,c;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (1)
{
if (Serial.available() > 0)
{
inByte = Serial.read();
if ((inByte >= 65 && inByte <= 90) || (inByte >=97 && inByte <=122) || (inByte >= 48 && inByte <=57) || inByte == 43 || inByte == 61 || inByte == 63)
command.concat(inByte);
}
if (inByte == 10 || inByte == 13)
{
inByte = 0;
command.concat(inByte);
}
if (command.equalsIgnoreCase("hey"))
{
Serial.println("hello there!");
}
else if(command.indexOf('+') > 0)
{
temp1 = command.substring(0,command.indexOf('+'));
temp2 = command.substring(command.indexOf('+')+1);
temp1.toCharArray(carray,6);
a = atoi(carray);
temp2.toCharArray(carray,6);
b = atoi(carray);
c = a + b;
Serial.print("The sum of ");
Serial.print(a,DEC);
Serial.print(" and ");
Serial.print(b,DEC);
Serial.print(" is ");
Serial.println(c,DEC);
}
else if (command.startsWith("mynameis"))
{
temp1 = command.substring(8);
Serial.print("Hello, ");
Serial.println(temp1);
}
else
{
if (!command.equalsIgnoreCase(""))
Serial.println("Invalid argument.");
}
command = "";
}
}
Now this is the version with the variables declared in variabls.h
//Project tester1.ino,
//#include "variabls.h" //#include commented out
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (1)
{
if (Serial.available() > 0)
{
inByte = Serial.read();
if ((inByte >= 65 && inByte <= 90) || (inByte >=97 && inByte <=122) || (inByte >= 48 && inByte <=57) || inByte == 43 || inByte == 61 || inByte == 63)
command.concat(inByte);
}
if (inByte == 10 || inByte == 13)
{
inByte = 0;
command.concat(inByte);
}
if (command.equalsIgnoreCase("hey"))
{
Serial.println("hello there!");
}
else if(command.indexOf('+') > 0)
{
temp1 = command.substring(0,command.indexOf('+'));
temp2 = command.substring(command.indexOf('+')+1);
temp1.toCharArray(carray,6);
a = atoi(carray);
temp2.toCharArray(carray,6);
b = atoi(carray);
c = a + b;
Serial.print("The sum of ");
Serial.print(a,DEC);
Serial.print(" and ");
Serial.print(b,DEC);
Serial.print(" is ");
Serial.println(c,DEC);
}
else if (command.startsWith("mynameis"))
{
temp1 = command.substring(8);
Serial.print("Hello, ");
Serial.println(temp1);
}
else
{
if (!command.equalsIgnoreCase(""))
Serial.println("Invalid argument.");
}
command = "";
}
}
and variabls.h:
String command;
String temp1,temp2;
char inByte;
char carray[6];
int a,b,c;
This constellation stops compiling in tester1 with the indicator at the line
inByte = Serial.read();
Now, the compiler apparently does not allow copy and paste of the error messages, but this listing starts with:
tester1.cpp: In function 'void loop()':
tester1:15: error: 'command' was not declared in this scope
and then it continues mentioning all occurrences of the variables.
When now the #include is decommented and thus activated, the compiler gives a different result; it stops in variabls.h at the first line and the first three lines of the error messages are:
In file included from tester1.cpp:1:
variabls.h:1: 'String' does not name a type
variabls.h:2: 'String' does not name a type
And then the complete list of the previous faulty compilation, beginning again with "tester1.cpp: In function 'void loop():'" follows.
If I put an Arduino command in variabls.h, for example Serial.begin(9600); as the first line, then I get an additional error:
variabls.h:1: error: expected constructor, destructor, or type conversion before '.' token
All this is following the normal procedure with other compilers and does not give any problems there.