Display of the Zumo IR and Light Sensors on the Serial Monitor

In this post, we are going to measure both the downward facing light sensors and the proximity sensors, and display their data.  The previous several posts discussed the reading of the light and proximity sensors, so we will not spend any time discussing those aspects of the task.  However, the small LCD display does not have enough characters available to display the 7 values of interest.  Note that there is a very good example program in the Zumo Examples that covers how to do this using custom characters to create a very nice bar chart on the LCD.  I found that trying to explain this to new programmers was more difficult that it was worth.  Fundamentally, we are just interested in reading the values so we can set some thresholds within a larger program to use these sensors as tools, and we are not too concerned with making nice bar charts on the display.

In order to display all of the data, we are going to use the Arduino Serial Monitor.  In the upper right corner of the Arduino IDE, there is an icon that looks like a magnifying glass.  This is the serial monitor.  It is an interface that allows us to receive data from the Arduino directly to our computer over a USB connection.  Note that for this to work, we need to keep the USB cable plugged in to receive the data.

In order to use the Serial Monitor, you first need to set up the interface.  Serial simply means that the information is transmitted bit-by-bit over the USB.  In Arduino, we need to specify how fast the bits are coming to the computer.  You would do that with the following command placed in the Setup() function as this command is run only once.

Serial.begin(115200); 

What this command does is that it simply initialized the serial interface, and says that we want to transmit the data from the Arduino at a rate of 115,200 bits per second.  Each character is approximately 8 bits (1 byte), so the characters and data is flying by pretty fast.  Once the program runs, the Arduino doesn’t normally wait for the user to open the Serial Monitor.  Personally, I like to have the monitor open prior to the robot sending me information so that I don’t miss anything that is important.  The way to force the program to pause and wait for the Serial Monitor to be opened is with the following command:

while(!Serial); 

This is short for saying, “do nothing while there is no serial communication.”  When sending information to the Serial Monitor, it can be done with the Serial.print() function.  This is very similar to the lcd.print() functions that we have been using to write to the LCD.  Like with the LCD, the Serial.print() does not send a carriage return at the end to specify that we are starting a new line.  To force a print with a new line, you would use the Serial.println() function.  println means “Print Line” which does force a carriage return at the end.  If we want the data to line up in a nice set of columns, we also want to use “\t” which is the format code for Tab.

The complete program is below:

#include <Zumo32U4.h>
Zumo32U4LineSensors lineSensors;
Zumo32U4ProximitySensors proxSensors;
Zumo32U4LCD lcd;
Zumo32U4ButtonA buttonA;
Zumo32U4Buzzer buzzer;

unsigned int lineSensorValues[3];

void setup() {
  lineSensors.initThreeSensors();
  proxSensors.initThreeSensors();
  calLightSensors(5);
  Serial.begin(115200);
  lcd.clear();
  lcd.gotoXY(0,0);
  lcd.print("Connect");
  lcd.gotoXY(0,1);
  lcd.print("Serial");
  // Wait Until Serial Connection Open
  while(!Serial);
  lcd.clear();
  Serial.println("Begin Serial Communication");
  delay(500);
  lcd.clear();
  lcd.gotoXY(0,0);
  lcd.print("Taking");
  lcd.gotoXY(0,1);
  lcd.print("Data");
}

void loop() {
  lineSensors.readCalibrated(lineSensorValues);
  proxSensors.read();
  displaySerial();
  delay(50);
}

void calLightSensors(int calTime) {
  lcd.clear();
  lcd.gotoXY(0,0);
  lcd.print("Press A");
  lcd.gotoXY(0,1);
  lcd.print("to Cal");
  buttonA.waitForButton();
  buzzer.playFrequency(440, 200, 15);
  lcd.clear();
  lcd.print("Cal\'ing");
  for (int i = 20*calTime; i > 0; i--)
  {
    lcd.gotoXY(0, 1);
    if(i%20==0) {
      lcd.print(i/20);
      lcd.print(" ");
      buzzer.playFrequency(440, 50, 15);
    }
    lineSensors.calibrate();
    delay(20);
  }
buzzer.playFrequency(600, 200, 15);
lcd.clear();
lcd.print("Cal Done");
delay(1000);
}

void displaySerial() {
  Serial.print("ProxSensors:\t");
  Serial.print(proxSensors.countsLeftWithLeftLeds() );
  Serial.print("\t");
  Serial.print(proxSensors.countsFrontWithLeftLeds() );
  Serial.print("\t");
  Serial.print(proxSensors.countsFrontWithRightLeds() );
  Serial.print("\t");
  Serial.print(proxSensors.countsRightWithRightLeds() );
  Serial.print("\tLineSensors:\t");
  Serial.print(lineSensorValues[0]);
  Serial.print("\t");
  Serial.print(lineSensorValues[1]);
  Serial.print("\t");
  Serial.println(lineSensorValues[2]);
} 

Like with so many things, there are shorter ways to display the data on the serial monitoring. However, this requires a better understanding of formatted string output. If I get time, I’ll have a post on this later.

This entry was posted in Robotics and tagged , , . Bookmark the permalink.