User Manual

172
/
223
while (! Serial); // Wait until Serial is ready - Leonardo
Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}
Firstly, we have the command 'Serial.begin(9600)'. This starts serial communication,
so that the MEGA2560 can send out commands through the USB connection. The
value 9600 is called the 'baud rate' of the connection. This is how fast the data is to
be sent. You can change this to a higher value, but you will also have to change the
Arduino Serial monitor to the same value. We will discuss this later; for now, leave
it at 9600.
The line beginning with 'while' ensures that there is something at the other end of
the USB connection for the Arduino to talk to before it starts sending messages.
Otherwise, the message might be sent, but not displayed. This line is actually only
necessary if you are using an Arduino Leonardo because the Arduino MEGA2560
automatically resets the Arduino board when you open the Serial Monitor, whereas
this does not happen with the Leonardo.
The last of the new lines in 'setup' sends out the message that we see at the top of
the Serial Monitor.
The 'loop' function is where all the action happens:
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch >= '0' && ch <= '7')
{
int led = ch - '0';
bitSet(leds, led);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
if (ch == 'x')
{
leds = 0;
updateShiftRegister();