Hi! I'm new using arduino and I have a question i cannot solve, maybe it's related on how serial port works. I want to use a simple program to print in the serial monitor "Hello world" but when I write the lines attending to a normal sequence in the serial monitor happens nothing.
Program:
well the easiest way to print only once would be to move the serial.print form loop to setup
Some microcontrollers need some time to establish the serial connection. It might be that this is the case. This would mean the code has executed the serial.print before the connection was really open. add this function to your code and test it
/* this code demonstrates how to use non-blocking timing based
* on easy to use non-blocking-timing based in on function millis()
* This helper-function reduces the nescessary code to two lines:
* declaring a variable for the timing
* a single call to the helperfunction
*
* and this code introduces how to uses functions.
*
* In this demo-code the functions are very short
* as soon as functions have more than two lines of code
* functions become effective
*/
int menu = 1;
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
// helper-function for easy to use non-blocking timing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod ){
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void doThePrint() {
Serial.print("Hola mundo");
Serial.println();
}
void printADot() {
Serial.print(".");
}
void setup() {
Serial.begin(9600);
}
void loop() {
// wait two seconds before executing the code
if ( TimePeriodIsOver(MyTestTimer,2000) ) {
// two seconds are over
if (menu == 1) {
doThePrint();
menu = 0;
}
// after executing doThePrint once
// every two seconds another dot gets printed
else {
printADot();
} // end of else related to if (menu == 1)
} // end of if ( TimePeriodIsOver...
}
That was the problem! Your code works perfectly and if I add a delay(5000) after Serial.begin(9600) every code I have used before too. Thank you so much,Stefan!!
the disadvantage of delay is the delay-ying. And it is blocking. No other code can be executed while a delay() is active. This is blocking.
IMHO function delay() should be completely taken away.
The function delay() puts beginners on the wrong track. And after getting used to function delay()
beginners have a hard time to understand non-blocking timing based on function millis()
My code demonstrates how to use non-blocking timing.
IMHO in an easier to use way than the standard blink-without-delay example
here is another code that demonstrates the use of non-blocking timing in conjunction with using user-defined functions
change baudrate in the serial monitor to 115200
unsigned long DemoTimer = 0; // variables that are used to store values of function millis()
unsigned long DemoTimerTwo = 0; // the must be of type unsigned long to work properly all the time
unsigned long DemoTimerThree = 0;
unsigned long DoDelayTimer = 0;
unsigned long myCounter = 0;
// helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod )
{
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void setup() {
Serial.begin(115200);
Serial.println("Program started activate Show timestamp in serial monitor");
Serial.println("maximise window of serial monitor");
Serial.println("to see the the messages in full.......................................................length");
}
void myDemofunction_1() {
Serial.println("once per second Huhu ! time for Action A ");
Serial.print("myCounter=");
Serial.println(myCounter);
}
void myDemoFuncB() {
Serial.println("once every 3 seconds Hi there time for Action B once every 3 seconds");
}
void my_Demo_function_3() {
Serial.print("once every 5 seconds ready now");
for ( int i = 0; i< 40; i++) {
Serial.print(".");
}
Serial.println("time for Action C once every 5 seconds");
}
void loop() {
myCounter++; // count up very fast to demonstrate the non-blocking character
if ( TimePeriodIsOver(DemoTimer, 1000) ) {
myDemofunction_1();
}
if ( TimePeriodIsOver(DemoTimerTwo, 3000) ) {
myDemoFuncB();
}
if ( TimePeriodIsOver(DemoTimerThree, 5000) ) {
my_Demo_function_3();
}
// show the effect of BLOCKING timing caused by function delay()
if ( TimePeriodIsOver(DoDelayTimer, 20000) ) {
Serial.println("every 20 seconds execute delay(5500)... to make all other timers overdue");
Serial.print("value of myCounter right before delay =");
Serial.println(myCounter);
delay(5500);
Serial.print("value of myCounter right AFTER delay =");
Serial.println(myCounter);
Serial.println("as delay(5500 has BLOCKED code-execution all three timers are overdue");
Serial.println("which means all three timers fire in the SAME microsecond one after the other ");
}
}
/*
the basic principle of non-blocking timing is to check if a defined timeinterval
has passed by.
This can be done by using the function millis()
The function millis()gives back the amount of milliseconds (hence the name millis)
that have passed by since power-up of the microcontroller.
It counts up to 2^32 which means reaching the max-value is reached after 49 days.
There is a calculation-technique that even "rollover" from max to zero is handled
automatically the right way
This non-blocking timing needs a timer-variable which is used for taking
snapshots of time as a comparison-point
The variable-type for this variable MUST be of type unsigend long
to make it work reliably all the time
unsigned long myLcdUpdateTimer;
now the following construction executes the code inside the if-condition
only once every two seconds
if ( TimePeriodIsOver(myLcdUpdateTimer,2000) ) {
// time for timed action
}
additionally the code demonstrates how you can code your own functions
and how to call/execute them.
The names of the functions are a bit lengthy to demonstrate which names
inside the code you can choose freely and to demonstrate where really a relation
between names is and where NO relation between names is
*/
Understood! I'm starting now but I have saved this example codes and I keep in mind the disadvantages of using "delay()" for my next programs and projects! Thank you,Stefan!
The real problem is the micro in this case. The micro has a native USB interface and doesn't use the standard UART on pins 0/1. This means, after starting the sketch and executing Serial.begin() the USB connection to the serial monitor has to be established. This needs some time. If you execute a Serial.print before the USB connection has been established it will fail. If you insert a delay, there is time to establish the connection and your Serial.print will work. Another way would be to check if the USB connection has been established.
void setup(){
Serial.begin(9600);
while(!Serial); // wait for USB connection.
}
The baudrate is of no meaning in this case, because there is no real UART connection involved. The Serial.print will always work with USB speed.
Things are different at a board with a separate USB-TTL converter ( e.g. like the UNO/Nano). In this case the USB connection is already active if the processor restarts and Serial.print will work without any delay. And because there is a 'classic' UART connection between the processor and the USB converter the baud rate is important.