Are pins 0 and 1 seperate from the USB bus? If I print to serial does it go to both?
I dont know where to search for an example of arduino to arduino serial communication, so can someone post a link? Thanks!
Are pins 0 and 1 seperate from the USB bus? If I print to serial does it go to both?
I dont know where to search for an example of arduino to arduino serial communication, so can someone post a link? Thanks!
They are the same pins, so yes, it goes to both.
vulture2600:
I dont know where to search for an example of arduino to arduino serial communication, so can someone post a link? Thanks!
You can put the below code on both arduinos, then connect the tx of the master arduino to the rx of the slave arduino, and connect the grounds between the two arduinos. Strings sent to the master via its serial monitor will be displayed on the serial monitor of the slave arduino.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}
Thank you fkeel, and thank you again Zoomkat for awesome code examples!
vulture2600:
I dont know where to search for an example of arduino to arduino serial communication, so can someone post a link? Thanks!
Go to the Reference page (click on Reference in navigation area at the top of this page), then click on Libraries just below the navigation bar. When there, look for SoftwareSerial, or do a search for AltSoftSerial, which is an improved version.
If you use SoftwareSerial on one Arduino, and Serial on the other, you can still use the Serial attached to the USB .
Good to know, thanks!
They are the same pins, so yes, it goes to both.
Not on the Leonardo.
Do those pins need pullup or pulldown resistors? I cant remember where I saw any of this info and its really hard to search for!
Do those pins need pullup or pulldown resistors?
Nope.
Explain what your problem is, and maybe we can help.
No problem yet. I'm experimenting with the concept of a web server on one arduino that sends serial commands to another to get or receive variable values and then serve them on a web page. The only reason I'm doing it that way is because the slave arduino has a very large sketch and not enough memory to serve a web page as well.
How long can serialEvent() be without affecting the sketch too much?
Along the lines of :
Master gets incoming web request and sends a "*" to slave.
Slave sees this as a request to send back a string of variables:
void serialEvent() {
char c = Serial.read();
switch (c) {
case '*': //all values requested.
Serial.print(tempHold);
Serial.print(F("h"));
Serial.print(tempAvg);
Serial.print(F("a"));
Serial.print(hour);
Serial.print(F("j"));
Serial.print(eveningHour);
Serial.print(F("e"));
Serial.print(morningHour);
Serial.print(F("m"));
Serial.print(tempReduction);
Serial.print(F("t"));
Serial.println(F("*"));
break;
case 'h':
tempHold += 2;
break;
case 'n':
tempHold -= 2;
break;
case 'e':
eveningHour ++;
if (eveningHour > 24) {
eveningHour = 24;
}
break;
case 'd':
eveningHour --;
if (eveningHour < 16) {
eveningHour = 16;
}
break;
case 'm':
morningHour ++;
if (morningHour > 11) {
morningHour = 11;
}
break;
case 'j':
morningHour --;
if (morningHour < 3) {
morningHour = 3;
}
break;
case 't':
tempReduction += 2;
if (tempReduction > 8) {
tempReduction = 8;
}
break;
case 'g':
tempReduction -= 2;
if (tempReduction < 0) {
tempReduction = 0;
}
break;
}
}
Master sees "*" as end of transmission and parses the data and saves them to global values to serve into the web page:
void serialEvent() {
String readString;
while (Serial.available()) {
char c = Serial.read();
if (c == '*') { //recieved end of packet.
return;
}
readString += c;
}
uint8_t holdEnd = readString.indexOf('h');
uint8_t avgEnd = readString.indexOf('a');
uint8_t hourEnd = readString.indexOf('j');
uint8_t eveningEnd = readString.indexOf('e');
uint8_t morningEnd = readString.indexOf('m');
uint8_t reductionEnd = readString.indexOf('t');
String hold = readString.substring(0, holdEnd);
tempHold = hold.toInt();
String avg = readString.substring(holdEnd + 1, avgEnd);
tempAvg = avg.toInt();
String hour1 = readString.substring(avgEnd + 1, hourEnd);
hour = hour1.toInt();
String evening = readString.substring(hourEnd + 1, eveningEnd);
eveningHour = evening.toInt();
String morning = readString.substring(eveningEnd + 1, morningEnd);
morningHour = morning.toInt();
String reduction = readString.substring(morningEnd + 1, reductionEnd);
tempReduction = reduction.toInt();
} //end serialEvent().
I have not tested any of this yet, just dicking around with it. Thoughts? Comments?
Thoughts?
Yes.
String readString;
while (Serial.available()) {
char c = Serial.read();
if (c == '*') { //recieved end of packet.
return;
}
readString += c;
}
This assumes that all of the serial data arrives at once. That is nowhere near a realistic assumption.
You know what the end of packet marker is. Read and store data until that marker arrives. Then use it, and reset.
Thanks.
Should I just leave out the check to see if "*" was recieved and just keep adding on to readString no matter what?
Should I just leave out the check to see if "*" was recieved and just keep adding on to readString no matter what?
No. When the '*' arrives, you need to parse the collected data, and reset readString to an empty string.
Youre right, it will return before it ever gets there. Thanks!