Serial.Read fill an array with characters typed in Serial Monitor.

Hey all! This code scrolls text specified in the variables byte numPatterns and byte patterns on an 8x10 LED matrix. The way this code is written requires you to assign the value of byte numPatters to the number of characters and spaces in the text, and requires you to type out each character of the text in byte patterns.

How can this code be adapted so that text typed in the serial monitor can be displayed on the LED Matrix?

One way I was thinking was:

  1. Use Serial.Read to count the number characters (in order to update byte numPatterns).
  2. Somehow use Serial.Read to separate each character and place each as separate elements of byte patterns.

How should this be accomplished? How could Serial.Read recognize a space typed, in order to display a space between letters on the Matrix?

All comments and input is appreciated!

-Skyler

Here is the instructables I used the code from
http://www.instructables.com/id/810-LED-Matrix-with-4017/ Instructables for LED Matrix

int DataPin = 9;
int LatchPin = 11;
int ClockPin = 10;
int clock = 13;
int Reset = 12;
//x,y for the loop
int x;
int y;


const int numPatterns = 17;//this is the number of patterns you want to display

byte patterns[numPatterns][10] = {space, M, A, D, E, space, B, Y, space, V, A, D, I, M, space}; // the patterns order


// here the definion of all the letter(big ans small) and numbers

#define A     {B00000000,B00111100,B01000010,B01000010,B01000010,B01111110,B01000010,B01000010,B01000010,B00000000}
#define B     {B00000000,B01111100,B01000010,B01000010,B01111100,B01000010,B01000010,B01000010,B01111100,B00000000}
#define C     {B00000000,B00111110,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B00111110,B00000000}
#define D     {B00000000,B01111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01111100,B00000000}
#define E     {B00000000,B01111110,B01000000,B01000000,B01111100,B01000000,B01000000,B01000000,B01111110,B00000000}
#define F     {B00000000,B01111110,B01000000,B01000000,B01111100,B01000000,B01000000,B01000000,B01000000,B00000000}
#define G     {B00000000,B00111100,B01000010,B01000010,B01000000,B01000111,B01000010,B01000010,B00111100,B00000000}
#define H     {B00000000,B01000010,B01000010,B01000010,B01111110,B01000010,B01000010,B01000010,B01000010,B00000000}
#define I     {B00000000,B00111000,B00010000,B00010000,B00010000,B00010000,B00010000,B00010000,B00111000,B00000000}
#define J     {B00000000,B00011100,B00001000,B00001000,B00001000,B00001000,B01001000,B01001000,B00110000,B00000000}
#define K     {B00000000,B01000100,B01001000,B01010000,B01100000,B01010000,B01001000,B01000100,B01000010,B00000000}
#define L     {B00000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01111110,B00000000}
#define M     {B00000000,B01000100,B10101010,B10010010,B10010010,B10000010,B10000010,B10000010,B10000010,B00000000}
#define N     {B00000000,B01000010,B01100010,B01010010,B01001010,B01001010,B01001010,B01000110,B01000010,B00000000}
#define O     {B00000000,B00111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00111100,B00000000}
#define P     {B00000000,B00111100,B01000010,B01000010,B01000010,B01111100,B01000000,B01000000,B01000000,B00000000}
#define Q     {B00000000,B00111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000110,B00111110,B00000001}
#define R     {B00000000,B00111100,B01000010,B01000010,B01000010,B01111100,B01000100,B01000010,B01000010,B00000000}
#define S     {B00000000,B00111100,B01000010,B01000000,B01000000,B00111100,B00000010,B01000010,B00111100,B00000000}
#define T     {B00000000,B11111110,B00010000,B00010000,B00010000,B00010000,B00010000,B00010000,B00010000,B00000000}
#define U     {B00000000,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00111100,B00000000}
#define V     {B00000000,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00100100,B00011000,B00000000}
#define W     {B00000000,B10000010,B10000010,B10000010,B10000010,B10010010,B10010010,B10101010,B01000100,B00000000}
#define X     {B00000000,B01000010,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B01000010,B00000000}
#define Y     {B00000000,B10000010,B01000100,B00101000,B00010000,B00010000,B00010000,B00010000,B00010000,B00000000}
#define Z     {B00000000,B01111110,B00000010,B00000100,B00001000,B00010000,B00100000,B01000000,B01111110,B00000000}

#define space {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000}

#define num_0 {B00000000,B00111100,B01000110,B01001010,B01001010,B01001010,B01010010,B01100010,B00111100,B00000000}
#define num_1 {B00000000,B00001000,B00011000,B00001000,B00001000,B00001000,B00001000,B00001000,B00011100,B00000000}
#define num_2 {B00000000,B00111100,B01000010,B00000100,B00001000,B00010000,B00100000,B01000000,B01111110,B00000000}
#define num_3 {B00000000,B01111110,B00000010,B00000010,B00011100,B00000010,B00000010,B01000010,B00111100,B00000000}
#define num_4 {B00000000,B00000100,B00001100,B00010100,B00100100,B01000100,B01111110,B00000100,B00000100,B00000000}
#define num_5 {B00000000,B01111110,B01000000,B01000000,B00111100,B00000010,B00000010,B00000010,B01111100,B00000000}
#define num_6 {B00000000,B00111100,B01000000,B01000000,B01111100,B01000010,B01000010,B01000010,B00111100,B00000000}
#define num_7 {B00000000,B01111110,B00000010,B00000100,B00001000,B00010000,B00010000,B00010000,B00010000,B00000000}
#define num_8 {B00000000,B00111100,B01000010,B01000010,B00111100,B01000010,B01000010,B01000010,B00111100,B00000000}
#define num_9 {B00000000,B00111100,B01000010,B01000010,B01000010,B00111110,B00000010,B00000010,B00111100,B00000000}
#define times {B00000000,B00000000,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B00000000,B00000000}


void setup() {
  pinMode(DataPin, OUTPUT);
  pinMode(ClockPin, OUTPUT);
  pinMode(LatchPin, OUTPUT);
  //simple stuff here
  pinMode(clock, OUTPUT);
  pinMode(Reset, OUTPUT);
  //reseting the 4017 IC, you have to do this
  digitalWrite(Reset, HIGH);
  delayMicroseconds(5);
  digitalWrite(Reset, LOW);
}

void display_pattern(int loops)//int loop acts like a delay, it take 8 mSecands to scan all of the rows so int loops = 15 is a good time for it
{
  for (x = 0; x < numPatterns - 1; x++) { // loop over the patterns
    for (int z = 0; z < 8; z++) { //scrolls one bit at a time
      for (int t = 0; t < loops; t++) { // the delay we get with loops
        for (y = 0; y < 10; y++) { // loops over the array of bytes
          byte temp = patterns[x][y];
          byte temp_2 = patterns[x + 1][y];
          digitalWrite(LatchPin, 0);
          shiftOut(DataPin, ClockPin, MSBFIRST, ((temp << z) + (temp_2 >> 7 - z))); //writes digital outputs, Z is for how much bits it need to scroll
          digitalWrite(LatchPin, 1); //data is only showed on the output after latch toggles from 0 to 1
          delayMicroseconds(800);
          digitalWrite(LatchPin, 0);
          shiftOut(DataPin, ClockPin, MSBFIRST, 0);
          digitalWrite(LatchPin, 1);
          digitalWrite(clock, HIGH);
          digitalWrite(clock, LOW);
        }
      }
    }
  }
}

void loop() {
  display_pattern(15);// calls for the display_pattern function and says that int loop = 15(if you do more loop the pattern whould scrrol slower).
}

Lustrousfiddle4:
How should this be accomplished? How could Serial.Read recognize a space typed, in order to display a space between letters on the Matrix?

if (characterThatWasRead == ' ')
{ do stuff }

Instread of making an array with a message, make an array with all the patterns. Or maybe two: one for letters and one for numbers.

then

if(Serial.available()) {
  char ch = Serial.read();
  byte pattern; 

  if(ch >= 'A' && ch <= 'Z') {
    pattern = letters_array[ch-'A'];
  }
  else if(ch >= '0' && ch <= '9') {
    pattern = numbers_array[ch-'0'];
  }
  else if(ch == ':') {
    pattern = COLON; // for instance
  }
  else {
    pattern = I_DONT_KNOW_HOW_TO_DISPLAY_THIS_PATTERN; // unhappy face
  }

  // and push the pattern out to the LCD

}

The 2nd and 3rd examples in Serial Input Basics store the incoming data in an array.

...R

What would I write to use Serial.read to return an integer value representing the number of chars typed in the serial monitor?

What would I write to use Serial.read to return an integer value representing the number of chars typed in the serial monitor?

Count how many times you call Serial.read() when there is data to be read. Then you'll know how many characters were entered.

PaulS:
Count how many times you call Serial.read() when there is data to be read. Then you'll know how many characters were entered.

Thanks! But specifically, what could be written to count the times Serial.read reads a char?

-Skyler

Try something like this:

if (Serial.available() >0){
dataArray[arrayIndex] = Serial.read(); // save next character
arrayIndex = arrayIndex+1; // increment index for next character
  if (dataArray[arrayIndex] == endChar){ // where endChar is carriage return, or line feed, or whatever your end
                                                                // character is - see the block at the lower right of the serial monitor
  arrayCount = arrayIndex ; // Save for whatever you need the count for
  arrayIndex = 0; // reset for next message
  }
}