I've wanted to make a little cool effect on my 16x2 character display for a little music project I've been planning. So I made this little sketch and I wanted to share.
I tried to make the code comprehensible but please tell me if not.
Each character is two "pixels".
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
float w=0;
int xWidth = 16;
int yWidth = 4;
//y first, x second
byte dis[4][16] = {
{B1,B0,B0,B0,B1,B0,B1,B0,B0,B0,B0,B0,B0,B0,B0,B0 },
{B0,B0,B1,B0,B1,B0,B1,B0,B0,B0,B0,B0,B0,B0,B0,B0 },
{B0,B0,B1,B0,B0,B0,B1,B0,B0,B0,B0,B0,B0,B0,B0,B0 },
{B1,B0,B0,B0,B1,B0,B1,B0,B0,B0,B0,B0,B0,B0,B0,B0 },
};
static byte top[8] = {B11111,B11111,B11111,B11111,B00000,B00000,B00000,B00000};
static byte bot[8] = {B00000,B00000,B00000,B00000,B11111,B11111,B11111,B11111};
static byte ful[8] = {B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
void setup() {
lcd.createChar(0,top);
lcd.createChar(1,bot);
lcd.createChar(2,ful);
lcd.begin(xWidth,yWidth/2);
updateDisplay();
}
void loop() {
fillDisplay(0);
for(int x=0;x<xWidth;x++) {
paint(x,2+2*cos(radians(w*(x+1))),1);
}
updateDisplay();
w+=1;
}
void fillDisplay(int c) {//0=white, 1=black, 2=invert
for (int x=0;x<=xWidth;x++) {
for (int y=0;y<=yWidth;y++) {
if(c==2){dis[y][x]^=B1;}else{dis[y][x]=(c==1)?B1:B0;}
}
}
}
void paint(int x,int y,int c) {
x=round(x);
y=round(y);
if (x<17&&x>-1&&y>-1&&y<5) {
if(c==2){dis[y][x]^=B1;}else{dis[y][x]=(c==0)?B0:B1;}
}
}
void updateDisplay() {
lcd.setCursor(0,0);
int r;
for (int i=0;i<=xWidth;i++) {
r=0;
r+=(dis[0][i]==B1)?1:0;
r+=(dis[1][i]==B1)?2:0;
lcd.write((r==0)?254:r-1);
}
lcd.setCursor(0,1);
for (int i=0;i<=xWidth;i++) {
r=0;
r+=(dis[2][i]==B1)?1:0;
r+=(dis[3][i]==B1)?2:0;
lcd.write((r==0)?254:r-1);
}
}
Haven't tested the code on other displays but should hopefully work.
Have a nice new year!