ledprojector/src/main.cpp

111 lines
1.9 KiB
C++

#include <Arduino.h>
/*
for cheap Chinese 64 dots LED-Projector
Board has unmarked chip in socket - pin layout fits to ATTiny 2313 an 4313
the LEDS are connected to 8 pcs 74HC595
--u--
RST 1 20 VCC
NC (0) 2 19 (16) IN SCK
NC (1) 3 18 (15) IN MISO
XTAL 4 17 (14) IN MOSI
XTAL 5 16 (13) IN
OUT (4) 6 15 (12) IN
OUT (5) 7 14 (11) IN
OUT (6) 8 13 (10) IN
NC (7) 9 12 (9) MIC
GND 10 11 (8) IN
-----
() = Arduino Pins
RST = Reset - 10K to GND, 10uF to VCC
XTAL = 16 MHz Crystal
IN = switches
MIC = Input from microphone amplifier (LM358)
*/
#define LATCHPIN 5
#define DATAPIN 4
#define CLOCKPIN 6
byte ledsData[8];
byte currentled = 0;
void clear() {
for (int i=0; i < 8; i++) {
// low is led on
ledsData[i] = 255;
}
}
void writebuffer(byte ledNr) {
byte chipNr = ledNr / 8;
byte pinNr = ledNr % 8;
if(chipNr < 8) {
bitWrite(ledsData[chipNr], pinNr, LOW);
}
}
void writexy(byte x, byte y) {
byte lednr = x*8 + y;
writebuffer(lednr);
}
byte rowmask[8] = {5,3,1,0,2,4,6,7};
void pushout() {
digitalWrite(LATCHPIN, LOW);
for(int i = 8-1; i>=0; i--) {
byte row = rowmask[i];
shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, ledsData[row]);
}
digitalWrite(LATCHPIN, HIGH);
}
void setup() {
pinMode(LATCHPIN, OUTPUT);
pinMode(DATAPIN, OUTPUT);
pinMode(CLOCKPIN, OUTPUT);
clear();
pushout();
}
byte currentcol=1;
bool direction = true;
void loop() {
clear();
// x-line
for(byte i=0; i<8; i++) {
writexy(i,currentcol-1);
}
// y line
for(byte i=0; i<8; i++) {
writexy(currentcol-1,i);
}
pushout();
if(direction) {
currentcol++;
} else {
currentcol--;
}
if (currentcol >8) {
currentcol = 7;
direction = false;
}
if (currentcol <=0) {
currentcol=2;
direction = true;
}
delay(100);
}