Fun with Arduino 08 User Interface Serial.print()

When I worked with a large tech company, we had this cartoon on the wall to remind us of the importance of good communication between all parties involved in a project:


If a project starts with unambiguous specifications, properly reviewed and agreed upon by all parties, costly modifications in the later phases of a project can be avoided.

When our ‘virtual customer’, the Tech Company, specified that the day / night cycle time should be ‘easy configurable’, they described the ‘what’ … but nothing was said about the ‘how’. Sometimes that is good … we can often leave the ‘how’ to the specialists, we expect them to know what they are doing. Sometimes it is wise though to also specify the ‘how’ as there often are many possible solutions.

We placed the cycle time parameter at the top of the code. It is indeed easy to change, but for every change the user first has to tinker with the code and then upload it again. The Tech Company is looking for a solution that does not require to modify the code. There should be a User Interface to change the cycle time ‘from the outside’ and ‘on the fly’.

When the Tech Company held a brainstorm session, the Hardware group came up with this:


The software group had something completely different in mind:


Isn’t it amazing how solutions for the same question can go in all kinds of directions if it is not unambiguously specified? The Tech Company called in our help again: they asked if we could make two prototypes for a user interface, one more software oriented, the other more hardware oriented.

Of course we said yes, we can! We proposed two simple solutions:

  1. a rotating knob
  2. data input via the PC keyboad

Both with a PC screen display that shows the current cycle time value and the status of the cycle: on, off, day, night.


They agreed with our specifications and gave us the ‘go’ to build these 2 prototypes. Let’s work out how to display text first. Read on below the video …



Luckily writing to the PC screen is very simple. The Arduino has a USB serial interface and there is a Serial.print() function with which we can print text or numbers:

Serial.print("This is text"); // text goes between quotes
Serial.print(173); // numbers don't use quotes

We can also print variables. We haven’t discussed variables yet, but suppose we have declared a variable with the name cycle_time and at some point we want to print the value that is currently stored in it, then, without quotes, we write:


Suppose the cycle_time variable contains the value 180, then if we would write …

Serial.print("Cycle time: ");
Serial.println(" seconds");

… the following will be printed on screen:

Cycle time: 180 seconds

Note that even though the print statements are written on three lines in the code, the printout is on one line. A print statement starts printing where the last one left off. To enforce a new line after the last printed text or number we can use the Serial.println() statement.

Before we can print, we need to enable the serial interface and tell it what speed (the so called Baudrate) we want it to work at. We do this in the setup section, with:


OK, now that we know all this, let’s create a cycle_time variable and add some print statements to the code we had at the end of video 6 and see how that starts to look.  The lines in red are the ones that were added / changed:

#define DAY_NIGHT_CYCLE_ON_OFF_PIN 2 // input for toggle switch
#define LIGHTS_PIN 8                 // output to FET or relay module
#define LED_PIN A5                   // output to LED on switch panel
int cycle_time = 180;                // [s] 180 = 3 minutes

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.println("Day/Night cycle system is ready");
  Serial.println(); // This prints an empty new line
  Serial.print("Cycle time: ");
  Serial.println(" seconds");

void loop() {
  if (digitalRead(DAY_NIGHT_CYCLE_ON_OFF_PIN) == LOW) {
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(LIGHTS_PIN, HIGH);
    delay(cycle_time * 1000); 
    digitalWrite(LIGHTS_PIN, LOW);
    delay(cycle_time * 1000);
  else {
    digitalWrite(LED_PIN, LOW);

Upload this code, then click on the Tools menu and then Serial Monitor to open the communication window. Check bottom right that it is set to 9600 Baud, and see what happens. Yes … this works.

What is this ‘int’ with the declaration of the cycle_time you may wonder? More on that in the next video, where we will have a closer look at variables and where we are going to add the print commands to show the status the day / night cycle is in when it is running.

— 0 —




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s