Advertisement
AntonioVillanueva

LCD I2C 16x2 arduino nano rotation du texte

Nov 17th, 2020 (edited)
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Antonio Villanueva Rotation du texte avec réinsertion
  3.   LCD i2c test https://github.com/johnrickman/LiquidCrystal_I2C
  4.  */
  5.  
  6. //LCD config
  7. #include <Wire.h>
  8. #include <LiquidCrystal_I2C.h>
  9. #define cols 16
  10. #define lignes 2
  11. LiquidCrystal_I2C lcd(0x27,cols ,lignes);  // Address i2c 0x27 0x3f
  12. /***************************************************************************************************/
  13. /***************************************************************************************************/
  14. //variables
  15. unsigned int x(0),y(0);
  16. String message("Tony Villanueva");
  17. String message2("Segura");
  18. /***************************************************************************************************/
  19. void printMessage(String &msg,unsigned int &x , unsigned int y);//imprime un message dans une position x,y
  20. void error(String msg="E R R O R");//Message error limites afficheur
  21. /***************************************************************************************************/
  22. /***************************************************************************************************/
  23. void setup() {
  24.   //  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS );
  25.   lcd.init();
  26.   lcd.backlight();//lcd.noBacklight();
  27.   //lcd.cursor_on();
  28.   //lcd.blink_on();
  29.  
  30. }
  31. /***************************************************************************************************/
  32. void loop() {
  33.   scrollLineRight(message,x,y);
  34.   scrollLineRight(message2,x,1);  
  35.   //lcd.scrollDisplayRight();
  36.   delay (500);
  37. }
  38.  
  39.  
  40. /***************************************************************************************************/
  41. void scrollLineRight(String &msg,unsigned int &x,unsigned int y){
  42.   //Rotation une ligne
  43.   String tmp("");
  44.  
  45.   for (int i=0;i<x;i++){tmp=' ';}//Cree space au debut
  46.   tmp+=msg;//Colle le message
  47.   for (int i= 16-(tmp.length ()+x) ; i>0;i--){ tmp+=' ';}//Cree space à la fin
  48.  
  49.   msg=tmp.charAt(15);//dernier caractère à la première position
  50.   msg+=tmp.substring(0,15);
  51.  
  52.   printMessage(msg,0,y);
  53.  
  54. }
  55.  
  56.  
  57. /***************************************************************************************************/
  58. void printMessage(String msg,unsigned int x , unsigned int y){//imprime un message dans une position x,y
  59.   if (limits(x,y,msg)){
  60.     error( "x="+String(x)+" ,y="+String(y)+",txt="+String(msg.length()));
  61.     return;
  62.   }
  63.   //lcd.clear();
  64.   lcd.setCursor(x,y);
  65.   lcd.print(msg);
  66.  
  67. }
  68. /***************************************************************************************************/
  69. bool limits(unsigned int x,unsigned int y,String msg){//Test limits ecran xy
  70.  
  71.   if ( (x+msg.length())>16 || y>1){return true;}
  72.   return false;
  73. }
  74. /***************************************************************************************************/
  75. void error(String msg="E R R O R"){//Message error limites afficheur
  76.   lcd.clear();
  77.   lcd.setCursor(0,0);
  78.   lcd.print(msg);
  79. }
  80. /***************************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement