Here is my sketch I am trying to write to the serial port with no success.
#include "HardwareSerial.h"
long previousMillis = 0;
long interval = 2000;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
String test = SendInstruction(Serial,"Test");
}
}
This is the code in the function
SendInstruction(HardwareSerial serial,String command)
{
int i;
int commandLength = command.length();
for(i=0; i < commandLength ;i++)
{
serial.print(command.charAt(i));
}
serial.print("#");
}
Now when I monitor the serial port all I get every 2 seconds is
"Te"
When I use Serial.print then it is fine but I need to get it working with HardwareSerial
unsigned long previousMillis = 0;
unsigned long interval = 2000;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
SendInstruction(Serial, "Test");
}
}
void SendInstruction(Stream &serial, String command) // <<<<<<<<<<<
{
int i;
int commandLength = command.length();
for (i = 0; i < commandLength ; i++)
{
serial.print(command.charAt(i));
}
serial.print("#");
}
no need to include HWSerial,
used the Stream baseclass (so you can also pass SWserial as param)
and most important used a reference &