Arduino LED Matrix EEPROM

Discussion in 'Embedded Systems and Microcontrollers' started by skusku, Apr 21, 2015.

  1. skusku

    Thread Starter Active Member

    Aug 9, 2009
    63
    1
    Hello All

    I am having some trouble with a LED MATRIX.
    Everything works except to display the last data that has been received when turning the device off and back on again.
    If I go into the arduino serial event manager and type with brackets (Hello), the word "Hello" scrolls through the LED MATRIX. But I need the last data to display again when turning the device off and back on again without being at a computer.

    Originally from this site: http://embedded-lab.com/blog/?p=9186

    The current data saves into the EEPROM bit by bit. Turning it off and on, displays it into the serial manager. But I think the problem is that it uses hardware interrupt to actually check for the data and then also that if I use Serial.read() command it doesnt receive it from a pc but from itself and therefore doesnt pick up and display the data.
    In an attempt I have tried to use the virtual serial but I am not having any luck with that either.
    The LED Matrix remains blank until I go into the serial manager on the pc and type anything in brackets.
    Can anybody please help?

    Code (C):
    1. /*
    2. Project: Scrolling Message Display using Easy Matrix modules in cascade
    3. Description: This is demo project for Easy Matrix modules cascaded together to make a scrolling
    4. message display. It uses Adafruit's GFX and Mark Ruys' Max72xxPanel libraries both.
    5. The project receives display data and commands through UART. You can send a character stream through Serial Monitor
    6. tool from the Arduino IDE. You also need a 1K resistor that is used to connect Arduino Pin 0 (Rx) and Pin 2 (External Interrupt).
    7. This interrupts the Arduino every time a new command or character stream is received through UART so that it could respond to
    8. that.
    9. Here are the commands Arduino recognizes.
    10. 1. Any character stream must be sent enclosed inside parenthesis.
    11.     Example: Suppose if you want to display a message "Hello! How are you doing?"
    12.     You should send it like this: (Hello! How are you doing?)
    13.     When the Arduino receives the first character as '(', it understands that the upcoming characters are display data until ')'
    14.     is received. The message is displayed as scrolling from right to left.
    15.  
    16. 2. Display intensity can be incremented or decremented by sending /+ or /- respectively. Every time the Arduino receives '/',
    17.     it understands it is a command. It supports 16 brightness levels.
    18.  
    19. 3. Similarly, scrolling speed can be increased or decreased by sending /< or /> respectively.
    20. 4. /p is used to pause the scrolling message. Sending this command again resumes the scroll.
    21. 5. /e is used to erase the buffer and blank the display
    22. Connections:
    23. Easy Matrix              Arduino
    24. VCC                      +5V
    25. DIN                      MOSI (Pin 11)
    26. LOAD                     pinCS defined in the program belowe
    27. CLK                      SCK (Pin 13)
    28. GND                      Gnd
    29. The UART communication happens at 9600 baud.
    30. Written by: Rajendra Bhatt
    31. Contact: admin@embedded-lab.com
    32. Easy Matrix product link: http://embedded-lab.com/blog/?p=9186
    33. */
    34.  
    35. #include <SPI.h>
    36. #include <Adafruit_GFX.h>
    37. #include <Max72xxPanel.h>
    38. //#include <avr/pgmspace.h>
    39. #include <EEPROM.h>
    40. #include <SoftwareSerial.h>
    41.  
    42. int pinCS = 8; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
    43. int numberOfHorizontalDisplays = 8;
    44. int numberOfVerticalDisplays = 1;
    45.  
    46. Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
    47. int scrollspeed = 200; // Default scrollspeed (milliseconds)
    48.  
    49. int spacer = 1;  // Space between two characters
    50. int width = 5 + spacer; // The font width is 5 pixels
    51. boolean inChar = false, NewData = true, pause = false;
    52. boolean dataAvailable = true;
    53. char inputString[512];
    54. int count = 0, BTvalue = 5;
    55. int address = 0;
    56. byte value;
    57. int prevcountLB;
    58. int prevcountHB;
    59. int test;
    60. byte byteRead;
    61.  
    62. SoftwareSerial mySerial(10, 12);
    63.  
    64. void setup() {
    65.  
    66.   matrix.setIntensity(15); // Use a value between 0 and 15 for brightness
    67.  
    68. // Adjust to your own needs
    69.   matrix.setPosition(0, 7, 0); // The first display is at <0, 7>
    70.   matrix.setPosition(1, 6, 0); // The second display is at <1, 0>
    71.   matrix.setPosition(2, 5, 0); // The third display is at <2, 0>
    72.   matrix.setPosition(3, 4, 0); // And the last display is at <3, 0>
    73.   matrix.setPosition(4, 3, 0); // The first display is at <0, 0>
    74.   matrix.setPosition(5, 2, 0); // The second display is at <1, 0>
    75.   matrix.setPosition(6, 1, 0); // The third display is at <2, 0>
    76.   matrix.setPosition(7, 0, 0); // And the last display is at <3, 0>
    77. //  ...
    78. //  matrix.setRotation(0, 2);    // The first display is position upside down
    79. //  matrix.setRotation(3, 2);    // The same hold for the last display
    80.   matrix.fillScreen(0);
    81.   matrix.write();
    82.   Serial.begin(9600); // serial communication initialize
    83.  
    84.   // Using interrupt 0 on digital pin 2.
    85.   pinMode(2, INPUT);
    86.   pinMode(7, INPUT);
    87.   digitalWrite(2, LOW);
    88.   attachInterrupt(0, serialInterrupt, CHANGE);
    89.  
    90.  
    91.   prevcountLB = EEPROM.read(501);  // read previous amount value in count and use this to read the exact amount of characters
    92.   Serial.write("(");
    93.    Serial.read();
    94.    delay(5);
    95.   for(int i=0; i<prevcountLB ; i++){
    96.   char ch = EEPROM.read(address);
    97.   Serial.write(ch);
    98.    Serial.read();
    99.       delay(5);
    100.   address = address + 1;
    101.   }
    102.   if (address == 50){
    103.     address = 0;
    104.   }
    105.   Serial.write(")");
    106.  
    107.   mySerial.begin(9600);
    108. mySerial.write("(Hello)");
    109. Serial.read();
    110. //Serial.write(mySerial.read());
    111.  
    112.  
    113.  
    114. }
    115.  
    116.  
    117. void loop(){
    118. if(dataAvailable){
    119.   display_data();
    120. }
    121. }
    122.  
    123. void display_data(){
    124.   for ( int i = 0 ; i < width * count + matrix.width() - 1 - spacer; i++ ) {
    125.  
    126.     matrix.fillScreen(0);
    127.  
    128.     int letter = i / width;
    129.     int x = (matrix.width() - 1) - i % width;
    130.     int y = (matrix.height() - 8) / 2; // center the text vertically
    131.  
    132.     while ( x + width - spacer >= 0 && letter >= 0 ) {
    133.       if ( letter < count ) {
    134.         matrix.drawChar(x, y, inputString[letter], HIGH, LOW, 1);
    135.       }
    136.  
    137.       letter--;
    138.       x -= width;
    139.     }
    140.     matrix.write(); // Send bitmap to display
    141.     if(!dataAvailable){
    142.       i = width * count + matrix.width() - 1 - spacer;
    143.       matrix.fillScreen(LOW);
    144.       matrix.write();
    145.     }
    146.     if(NewData){
    147.       i = 0;
    148.       matrix.fillScreen(LOW);
    149.       matrix.write();
    150.       NewData = false;
    151.     }
    152.     while(pause == true){
    153.       delay(0);
    154.     } // Pause here if pause command is received
    155.  
    156.     delay(scrollspeed);
    157.   }
    158. }
    159.  
    160. // Volatile, since it is modified in an ISR.
    161. volatile boolean inService = false;
    162.  
    163. void serialInterrupt(){
    164.  
    165.   if (inService) return;
    166.  
    167.   // You was not in service. Now you are.
    168.   inService = true;
    169.  
    170.   // Reenable interrupts, to allow Serial to work. We do this only if inService is false.
    171.   interrupts();
    172.     address=0; //return address back to 0 for when writing to EEPROM
    173.   while(!Serial.available());
    174.   while (Serial.available()) {
    175.      char ch = Serial.read();
    176.      if(ch =='('){
    177.        count = 0;
    178.        inChar = true;
    179.      
    180.        while(inChar){
    181.          if (Serial.available()) {
    182.            ch = Serial.read();
    183.            if(ch == ')'){
    184.              inChar = false;
    185.              dataAvailable = true;
    186.             } else{
    187.              inputString[count] = ch;
    188.              EEPROM.write(address, ch);
    189.               address = address + 1;
    190.               if (address == 50)
    191.               address = 0;
    192.               delay(50);
    193.  
    194.  
    195.              count++;
    196.            
    197.          
    198.            }
    199.            if(count > 0) {
    200.              ;
    201.              NewData = true;
    202.            
    203.            }
    204.          }
    205.        }
    206.  
    207.        Serial.println(count);
    208.            if(count>=10 && count<20){
    209.           EEPROM.write(500,1);            //Higher Nibble
    210.            }
    211.            else if (count>=20 && count<29){
    212.           EEPROM.write(500,2);
    213.            }
    214.            else if (count>=30 && count<40){
    215.           EEPROM.write(500,3);
    216.            }
    217.             else if (count>=40 && count<50){
    218.           EEPROM.write(500,4);
    219.            }
    220.  
    221.               EEPROM.write(501, count);  //Write Count Value into addr 501 Lower Nibble
    222.      }
    223.    
    224.      if(ch =='/'){   // Command mode
    225.        inChar = true;
    226.        while(inChar){
    227.          if (Serial.available()) {
    228.            ch = Serial.read();
    229.            // '/+' is for brightness increment
    230.            if(ch == '+'){
    231.              if(BTvalue < 15) {
    232.                BTvalue ++;
    233.                matrix.setIntensity(BTvalue);
    234.               }
    235.              break;
    236.            }
    237.            // '/-' is for brightness decrement        
    238.            if(ch == '-'){
    239.             if(BTvalue > 0){
    240.              BTvalue --;
    241.              matrix.setIntensity(BTvalue);
    242.             }
    243.             break;
    244.            }  
    245.  
    246.            // '/>' is to slow down scroll
    247.            if(ch == '>'){
    248.              if(scrollspeed < 500) {
    249.                scrollspeed = scrollspeed + 50;
    250.              }
    251.              break;
    252.            }
    253.            // '/<' is to make scroll faster        
    254.            if(ch == '<'){
    255.             if(scrollspeed > 50){
    256.              scrollspeed=scrollspeed-50;
    257.             }
    258.             break;
    259.            }
    260.  
    261.            // '/e' is to erase display        
    262.            if(ch == 'e'){
    263.             dataAvailable = false;
    264.              break;
    265.            }
    266.          
    267.            // '/p' is to pause display        
    268.            if(ch == 'p'){
    269.              if(pause == false){
    270.                pause = true;
    271.              }
    272.              else {
    273.                pause = false;
    274.              }
    275.              break;
    276.            }
    277.          
    278.            else {
    279.             break;  // Unrecognized command
    280.            }
    281.          
    282.          
    283.          }
    284.        }
    285.      }  // if(ch == '!')
    286.  
    287.  
    288.   }  // while(serial.available)
    289.   inService = false;
    290. }
    291.  
    292.  
    293.  
    294.  
    295.  
    296.  
    297.  
    298.  
    299.  
    300.  
    301.  
    302.  
    303.  
    304.  
     
    Last edited by a moderator: Nov 23, 2015
  2. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    Hello,
    I have done project with scrolling text display.
    The data i stored in flash using pic micro,and EEPROM is to slow for picking data!
     
Loading...