You can display messages just as well as patterns since each letter, digit or symbol is just a pattern. Luckily for us, someone has already worked out all the patterns we need and a function for displaying them!
Save your previous sketch (Interrupt) with a new name "Text". We can keep most of it.
We need a new file font.h containing the patters for the text characters. Copy it in the folder Text.
Include it in your sketch by adding the following line at the top:
#include "font.h"
You also need 2 new functions displayChar() and displayCharLower() which you can find in this fil <*>. Copy and paste them into your sketch after the end of the function loop().
The function displayChar() displays a character, and displayCharLower() does the same except the other way up and backwards. You can use it to display text the right way up along the bottom of the circle as the LEDs spin.
And now to code!
Let's may it display "Hello" on the top and "Year8!" on the bottom.
Insert 2 lines somewhere before setup() as follows:
int rows= 8; // Total LED's in a row
const int charHeight = 8;
const int charWidth = 5;
char textString_Up[] = "Hello";
char textString_Down[] = "Year8!";
(You can change the message if you like but that's about as long as you can make it.)
Now you have to change the code in loop(), replacing the 2 for loops beginning
for (int c =
with your new code. (Make sure your curly brackets still match up.)
First of all, switch on one of the colours with a digitalWrite() call. (Can you remember how to do that?) Now loop through the first string displaying the characters. This is how to do it:
for (int k=0; k<strlen(textString_Up); k++){
displayChar(textString_Up[k]);
}
Can you see how that works?
Before starting the other string we might want to change the colour.
We want the other string to start 180⁰ on from the start of the first. The code at the start of loop() calculates the time for a revolution and records the start time of the last revolution, so we just wait until half a revolution's time from the start, after first checking we haven't already gone past it!
if((micros() < (lastTime + revTime / 2))){
// wait for it . . .
while((micros() < (lastTime + revTime / 2)) && !flag)continue;
//Now display the lower string
}
You can display the lower string (replace the comment) with a loop just like before, except that you'll be displaying the characters in reverse order. Can you work out how to do that?
Don't forget to switch off the colour for the lower message if it was different from the upper one.
Continue to Lesson 5 by clicking here!
If you're stuck try reading the Help Sheet!