Hi,
I am trying to get one arduino uno (1) to send key words to another arduino uno (2). Robin2 has already pointed me to his topic "serial Input Basics" (Serial Input Basics - Programming Questions - Arduino Forum). this forum has been helpful however i have been unable to call functions.
here is some code i have written to express my issue:
char c = NULL; // input from recievedChars
String s = ""; // declare string (to convet c )
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
digitalWrite(13,LOW); // led starts off
}
void loop() {
a(); // get input
if (s = 'on') { //decide if days on or off and change the led state if it does.
digitalWrite(13,HIGH);
} else if (s == 'off') {
digitalWrite(13, LOW);
}
}
void a() { // get input through serial
recvWithStartEndMarkers();
showNewData();
c=receivedChars;
s = String(c);
// Serial.println(s);
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println(receivedChars);
newData = false;
}
}
if it possible that i am simply using the wrong input type i.e. string or int when it is coming in as a char.
even if i try to use char in the if statements i am unable to change the led on or off.
void a() { // get input through serial
recvWithStartEndMarkers();
showNewData();
c=receivedChars;
s = String(c);
// Serial.println(s);
}
For one thing, c is defined as a char and receivedChars[] is defined as an array and you CANNOT but an array into a single char.
Even if you could, what would be point of converting it to the String class since the whole purpose of my example code is to avoid using the String class. It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
And, if that were not enough, you are trying to do something with receivedChars[] when you have no idea whether it contains the complete incoming message. Look at how showNewData() deals with that and emulate it.
This part also suffers from the problem I mentioned in the previous paragraph
if (s = 'on') { //decide if days on or off and change the led state if it does.
digitalWrite(13,HIGH);
} else if (s == 'off') {
digitalWrite(13, LOW);
}
Finally, if all you want to do is control ON and OFF why not just send the single character 'N' for "on" and 'F' for "off"
The code in that example is simply trying to help me explain my issue - there are some errors. The reason I am not using single characters is because I would like the functions I am calling to be human readable. (unlike my stupidly named functions).
@BulldogLowell you hit the nail on the head. Thank you for your suggestion. However i am unable to use newline in serial monitor as I intend to have one uno controlling the other.
The reason I am not using single characters is because I would like the functions I am calling to be human readable
So type in "Turn on LED 1" in the first Arduino, have it translate it to say "x", transmit the single character, receive it on the second Arduino, decode it and have it run function turnOnLed(1); to turn on the LED
UKHeliBob:
So type in "Turn on LED 1" in the first Arduino, have it translate it to say "x", transmit the single character, receive it on the second Arduino, decode it and have it run function turnOnLed(1); to turn on the LED