Advertisement
learnelectronics

Arduino Scrolling Text

Feb 2nd, 2018
8,693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. //****************************************
  2. //     Arduino Scrolling Text
  3. //     learnelectronics
  4. //     1 FEB 2018
  5. //     www.youtube.com/c/learnelectronics
  6. //
  7. //     Based on sketch by Marco Colli
  8. //*****************************************
  9.  
  10.  
  11.  
  12. #include <MD_Parola.h>              //MD Parola Library
  13. #include <MD_MAX72xx.h>             //MD Maxx72XX Library
  14. #include <SPI.h>                    //SPI library for comms
  15.  
  16.  
  17. #define USE_UI_CONTROL 0            //Set to 1 to use control for speed, direction and invert
  18.  
  19. #if USE_UI_CONTROL                  //includes library if we are using the UI Control
  20. #include <MD_KeySwitch.h>
  21. #endif
  22.  
  23.  
  24. #define DEBUG 0                     //Set to 1 for Serial debug info
  25.  
  26. #if DEBUG                           //debug info sent to serial port
  27. #define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
  28. #define PRINTS(x) Serial.print(F(x))
  29. #define PRINTX(x) Serial.println(x, HEX)
  30. #else
  31. #define PRINT(s, x)
  32. #define PRINTS(x)
  33. #define PRINTX(x)
  34. #endif
  35.  
  36.  
  37. #define MAX_DEVICES 4               //number of units in the chain (max 8?)
  38. #define CLK_PIN   13                //SPI Clk on digital 13
  39. #define DATA_PIN  11                //SPI MOSI on digital 11
  40. #define CS_PIN    10                //SPI CS on digital 10
  41.  
  42.  
  43.  
  44. // Scrolling parameters
  45.  
  46.  
  47. uint8_t scrollSpeed = 25;                   // default frame delay value
  48. textEffect_t scrollEffect = PA_SCROLL_LEFT; //parameters from Parola library, sets scrolling from left
  49. textPosition_t scrollAlign = PA_LEFT;       //parameters from Parola library, sets align left
  50. uint16_t scrollPause = 2000;                // delay in milliseconds
  51.  
  52.  
  53. #define BUF_SIZE    75                        //buffer size
  54. char curMessage[BUF_SIZE];                  //sets current message buffer size
  55. char newMessage[BUF_SIZE];                  //sets new message buffer size
  56. bool newMessageAvailable = false;           //flag for if there is no new message
  57.  
  58.  
  59.  
  60. void readSerial(void)                       //serial read routine
  61. {
  62.   static char *cp = newMessage;             //declare the messsage variable as character type
  63.  
  64.   while (Serial.available())                //if data is coming from the serial port
  65.   {
  66.     *cp = (char)Serial.read();              //add the character to the character string
  67.     if ((*cp == '\n') || (cp - newMessage >= BUF_SIZE-2)) // if the message end or buffer is full
  68.     {
  69.       *cp = '\0';                           // if we get the NewLine character then end the string
  70.                                             // restart the index
  71.       cp = newMessage;                      //cp variable now equals the new message
  72.       newMessageAvailable = true;           //flag for if there is a new message  
  73.     }
  74.     else                                    // move char pointer to next position
  75.       cp++;
  76.   }
  77. }
  78.  
  79. void setup()
  80. {
  81.   Serial.begin(57600);                      //serial port begins at 57600
  82.   Serial.print("Type message, end with Newline");
  83.  
  84.  
  85.  
  86.   P.begin();                                //MD Parola parameter - begin
  87.   P.displayClear();                         //MD Parola parameter - clear the display
  88.   P.displaySuspend(false);                  //MD Parola parameter - suspend or not?
  89.  
  90.   //MD Paroloa display msg using our predefined parameters
  91.   P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
  92.  
  93.   strcpy(curMessage, "Hello! Enter new message?"); //prompt user to enter mesg
  94.   newMessage[0] = '\0';                            //first spot in array NewLine
  95. }
  96.  
  97. void loop()
  98. {
  99.  
  100.  
  101.   readSerial();                                   //read the serial port
  102.   if (P.displayAnimate())
  103.   {
  104.     if (newMessageAvailable)                      //if there is a new msg
  105.     {
  106.       strcpy(curMessage, newMessage);             //copy current message to new message
  107.       newMessageAvailable = false;                //set flag
  108.     }
  109.     P.displayReset();                             //MD Parola parameter - reset display
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement