Stap 3: Programmering/Code
Download het bestand aan het einde van de instructable of kopieer de volgende code in jouw Arduino-Intel IDE (Integrated Development Environment). Upload de code naar de Edison-bestuur.
//This is a library of functions designed to simplify the task of setting up scrolling code on the lcd screen//There are several modes of text scrolling: //Continuous scrolling - characters will scroll across the lcd screen as they are added. //Looped scrolling - Update a String that scrolls on loop, choose the row to scroll.
#include #include
rgb_lcd lcd;
const int lcdRows = 2; const int lcdCols = 16;
int milisPerChar = 125; long prevMilis = 0;
int scrollPos[lcdRows]; String prevStrings[lcdRows];
//EXAMPLE STRINGS: String scrollingText = "This text will scroll at the specified rate on loop from right to left.";
void initLCD(){ lcd.begin(lcdCols, lcdRows); for(int i = 0; i < lcdRows; i++){ scrollPos[i] = 0; prevStrings[i] = " "; for(int j = 0; j < lcdCols - 1; j++){ prevStrings[i] += " "; } } lcd.setRGB(255,255,255); //White by default, can be customized }
void scrollLCD(int rowNum, String text){ text = text + " "; if(millis() - prevMilis > milisPerChar){ scrollPos[rowNum] += (millis()-prevMilis)/milisPerChar; scrollPos[rowNum] = scrollPos[rowNum] % text.length(); prevMilis = millis(); }
lcd.setCursor(0,rowNum); for(int i = 0; i < lcdCols; i++){ lcd.print(" "); } lcd.setCursor(0,rowNum); if(text.length() < lcdCols){ lcd.print(text); lcd.setRGB(255,0,0); } else { for(int i = 0; i < lcdCols; i++){ lcd.print(text[(scrollPos[rowNum] + i)%text.length()]); } } }
void appendLCD(int rowNum, String textToAppend){ prevStrings[rowNum] = prevStrings[rowNum] + textToAppend; prevStrings[rowNum] = prevStrings[rowNum].substring(prevStrings[rowNum].length()-1-lcdCols, prevStrings[rowNum].length()); lcd.setCursor(0, rowNum); lcd.print(prevStrings[rowNum]); }
void setup() { initLCD();
//Perform Other Initialization Steps Here: }
void loop() { scrollLCD(0, scrollingText); if(random(4)==0){ String alphabet = "ABCDEFGHIGJKLMNOPQRSTUVWXYZ"; int ranVal = random(alphabet.length()); appendLCD(1, alphabet.substring(ranVal, ranVal+1)); } delay(100);//Unless you are doing a lot of processing, this is necessary to prevent screen flickering. }