Hello
I am sending data between two arduino. How can i use serial.write() to print the data on the same line. I want to use write instead of print because i send characters too.
Trasmitter code:
#include <SoftwareSerial.h>
#define rx 7
#define tx 8
SoftwareSerial ss(7, 8);
int month,day,year;
void setup() {
ss.begin(9600);
day=0;
month=0;
year=2000;
}
void loop() {
ss.print(day);
ss.print("/");
ss.print(month);
ss.print("/");
ss.print(year);
delay(100);
}
Receiver code:
//Receiver Code
#include <SoftwareSerial.h>
#define rx 9
#define tx 6
SoftwareSerial ss(9, 6);
int var,number;
void setup() {
Serial.begin(9600);
ss.begin(9600);
}
void loop() {
while (ss.available() > 0) { //read the two speeds
number= ss.parseInt();
var = ss.read();
Serial.write(var);
//Serial.write(number);
//Serial.print(var);
//Serial.println(number);
}
delay(100);
}
Receiver Serial monitor:
/0
⸮2000
/0
/0
/20000
/0
⸮2000
/0
/0
⸮2000
/0
/0
⸮2000
/0
/0
blh64
May 18, 2020, 3:29pm
2
Read Robin2's tutorial Serial Input Basics or use the SerialTransfer library to do all the work.
sirick
May 18, 2020, 11:29pm
3
blh64:
Read Robin2's tutorial Serial Input Basics or use the SerialTransfer library to do all the work.
Thanks dude Robin2's tutorial helped me alot!
Here is my final code working with serial.write using <,> as start and end byte
//Receiver Code
#include <SoftwareSerial.h>
#define rx 9
#define tx 6
SoftwareSerial ss(9, 6);
int i=0;
char var[32];
bool received=false;
void setup() {
Serial.begin(9600);
Serial.flush();
ss.begin(9600);
ss.flush();
}
void loop() {
static bool reading = false;
while (ss.available() > 0 && received == false) {
var[i] = ss.read();
if (var[i] == 60) {
reading=true;
}
if(reading==true){
if (var[i]!= 13 && var[i] != 60 && var[i] != 62) {
Serial.write(var[i]);
}
if (var[i] == 62) {
received = true;
reading=false;
break;
}
i++;
}
}
if (received) {
Serial.println();
received = false;
i=0;
}
delay(100);
}
blh64
May 18, 2020, 11:35pm
4
I personally find it more understandable to use the char constants rather than the ascii values
// if (var[i] == 60) {
if (var[i] == '<') {
reading=true;
}
//...