My code now:
/*
* Wink: POV Display test
* Wire LEDs according to the pins below
* The mercury switch needs to be on the left, so it closes while waving LEFT!
*/
#include <avr/pgmspace.h>
#define LEFT 1
#define RIGHT -1
#define MERCURYSW 4
#define DEBUG 3
int dir = 1;
int time = 0;
int atime = 0;
int pos = 0;
int val = 0;
int myPins[] = {
6,7,8,9,10,11,12,13};
// + HALLO +
prog_uchar PROGMEM BitMap[] = {
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00010000,
B00010000,
B01111100,
B00010000,
B00010000,
B11111111,
B00010000,
B00010000,
B11111111,
B00000000,
B11111111,
B00010001,
B00010001,
B11111111,
B00000000,
B11111111,
B10000000,
B10000000,
B10000000,
B00000000,
B11111111,
B10000000,
B10000000,
B10000000,
B00000000,
B11111111,
B10000001,
B10000001,
B11111111,
B00000000,
B00000000,
B00000000,
B00010000,
B00010000,
B01111110,
B00010000,
B00010000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
};
int len = sizeof(BitMap);
void setup() // run once, when the sketch starts
{
for (int i=0; i <= 7; i++){
pinMode(myPins[i], OUTPUT); // sets the digital pin as output
}
pinMode(MERCURYSW, INPUT); // sets the digital pin as input (mercury switch)
pinMode(DEBUG, OUTPUT); // sets the digital pin DEBUG as output
}
void setLEDs(prog_uchar pattern)
{
for (byte i=0; i <= 7; i++){
digitalWrite(myPins[i],pattern & (1<<i)); // Bit shifting magic
}
}
void loop() // run over and over again
{
time++;
if (digitalRead(MERCURYSW)==HIGH && dir==LEFT) // checks mercury switch
{
dir = RIGHT;
atime = time;
time = 0;
pos=0; //sync begin of text to start of waving
}
else if (digitalRead(MERCURYSW)==LOW && dir==RIGHT)
{
dir = LEFT;
atime = time;
time = 0;
pos=len; //sync text end to start of waving
}
if (dir==1){
digitalWrite(DEBUG,HIGH); // Debug LED used here to indicate the mercury switch state
pos = (pos + dir) % len ;
}
else
{
digitalWrite(DEBUG,LOW); // Debug LED
pos = (pos + dir);
if (pos<0)
{
pos = len;
}
}
// Try to sync to waving speed (still far from perfect!)
val = atime+analogRead(2); // read the value from the pot to change the frequency
setLEDs(pgm_read_byte_near(&BitMap[pos])); // switch on the LEDs
delayMicroseconds(val); // waits
setLEDs(0); // switch off all LEDs
delayMicroseconds(val); // waits
}