windcharm

wiring diagram in jpg attachment.
- This code seems to upload (first attempt I had to manually ground the RESET pin) and work fine, including the sleep and hardware interrupt, apart from the fact that the tilt sensor/switch has to be shorted. i.e. pin 2 at 0V.
- When the tilt sensor is open pin 2 is set to 4.1V (about 90%Vcc) all the LEDs flicker then, when the pin is grounded again, the program re-starts. After a few times the program fails (either LEDs all off (pin2=0V) or all flicker (pin2=4V), but starts again OK after a manual restart or switching the supply off and on. Vcc doesn't appear to change significantly when the tilt switch opens and closes.

added to boards.txt

##############################################################

atmega48.name=stripped down with w/ ATmega48 (8 MHz internal clock)*****

atmega48.upload.protocol=stk500
atmega48.upload.maximum_size=4094
atmega48.upload.speed=4800

atmega48.bootloader.low_fuses=0xE2
atmega48.bootloader.high_fuses=0xDF
atmega48.bootloader.extended_fuses=0xFF
atmega48.bootloader.path=atmega
atmega48.bootloader.file=ATmegaBOOT_168_pro_8MHz.hex
atmega48.bootloader.unlock_bits=0x3F
atmega48.bootloader.lock_bits=0x0F

atmega48.build.mcu=atmega48
atmega48.build.f_cpu=1000000L
atmega48.build.core=arduino:arduino

the sketch

#include <WProgram.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>

const int pin0 = 3;
const int pinIn = 11;
const int tick = 3;
const int pLen = 100;
const int lLen = 100; //spinning version whole phrase on one line!
const int tPc = tick*8; //millis per char
const int tPl = tPc*lLen; //millis per line
const unsigned long sleepTime = 120000;
const unsigned long refreshTime = 10000;

char charData[] PROGMEM ={  //array for char bytes for dot matrix
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, //32 ' '
B00000000, B00000000, B00000000, B00000000, B00000000, B11111101, B00000000, B00000000,
B00000000, B00000000, B00000000, B00000000, B11100000, B00000000, B11100000, B00000000,
B00000000, B00000000, B00000000, B00101010, B01111111, B00101010, B01111111, B00101010,
B00000000, B00000000, B00000000, B00100001, B01010001, B11111111, B10001010, B10000100,
B00000000, B00000000, B00000000, B11000001, B11000110, B00011000, B01100011, B10000011,
B00000000, B00000000, B00000000, B00110110, B01001001, B10010110, B11100110, B00001001,
B00000000, B00000000, B00000000, B00000000, B00000000, B11100000, B00000000, B00000000,
B00000000, B00000000, B00000000, B00000000, B00111100, B01000010, B10000001, B00000000,
B00000000, B00000000, B00000000, B00000000, B10000001, B01000010, B00111100, B00000000,
B00000000, B00000000, B00000000, B10101000, B01110000, B11111000, B01110000, B10101000,
B00000000, B00000000, B00000000, B00001000, B00001000, B00111110, B00001000, B00001000,
B00000000, B00000000, B00000000, B00000000, B00000001, B00000110, B00000100, B00000000,
B00000000, B00000000, B00000000, B00000000, B00001000, B00001000, B00001000, B00000000,
B00000000, B00000000, B00000000, B00000000, B00000000, B00000001, B00000000, B00000000,
B00000000, B00000000, B00000000, B00000001, B00000110, B00011000, B01100000, B10000000,
B00000000, B00000000, B00000000, B01111110, B10000011, B10001001, B10100001, B01111110, //48 '0'
B00000000, B00000000, B00000000, B00100001, B01000001, B11111111, B00000001, B00000001,
B00000000, B00000000, B00000000, B01100011, B10000101, B10001001, B10010001, B01100001,
B00000000, B00000000, B00000000, B01000010, B10010001, B10010001, B10010001, B01101110,
B00000000, B00000000, B00000000, B11111000, B00001000, B00111111, B00001000, B00001000,
B00000000, B00000000, B00000000, B11110001, B10010001, B10010001, B10010001, B10001110, //53 '5'
B00000000, B00000000, B00000000, B00111110, B01010001, B10010001, B10010001, B10001110,
B00000000, B00000000, B00000000, B10000001, B10000010, B10000100, B10001000, B11110000,
B00000000, B00000000, B00000000, B01101110, B10010001, B10010001, B10010001, B01101110,
B00000000, B00000000, B00000000, B01100000, B10010000, B10010000, B10010000, B01111111, //57 '9'
B00000000, B00000000, B00000000, B00000000, B00000000, B01100110, B00000000, B00000000,
B00000000, B00000000, B00000000, B00000000, B00000001, B01100110, B00000000, B00000000,
B00000000, B00000000, B00000000, B00000000, B00011000, B00100100, B01000010, B10000001,
B00000000, B00000000, B00000000, B00010100, B00010100, B00010100, B00010100, B00000000,
B00000000, B00000000, B00000000, B10000001, B01000010, B00100100, B00011000, B00000000,
B00000000, B00000000, B00000000, B01100000, B10000000, B10001101, B10010000, B01100000,
B00000000, B00000000, B00000000, B01111111, B10000001, B10011001, B10000001, B01111110,
B00000000, B00000000, B00000000, B01111111, B10001000, B10001000, B10001000, B01111111, //65 'A'
B00000000, B00000000, B00000000, B11111111, B10010001, B10010001, B10010001, B01101110,
B00000000, B00000000, B00000000, B00111100, B01000010, B10000001, B10000001, B10000001,
B00000000, B00000000, B00000000, B11111111, B10000001, B10000001, B01000010, B00111100,
B00000000, B00000000, B00000000, B11111111, B10010001, B10010001, B10010001, B10000001,
B00000000, B00000000, B00000000, B11111111, B10010000, B10010000, B10010000, B10000000,
B00000000, B00000000, B00000000, B01111110, B10000001, B10001001, B10001001, B01001110,
B00000000, B00000000, B00000000, B11111111, B00010000, B00010000, B00010000, B11111111,
B00000000, B00000000, B00000000, B10000001, B10000001, B11111111, B10000001, B10000001,
B00000000, B00000000, B00000000, B10000010, B10000001, B11111110, B10000000, B10000000,
B00000000, B00000000, B00000000, B11111111, B00010000, B00101000, B01000100, B10000011, //75 'K'
B00000000, B00000000, B00000000, B11111111, B00000001, B00000001, B00000001, B00000001,
B00000000, B00000000, B00000000, B11111111, B01100000, B00110000, B01100000, B11111111,
B00000000, B00000000, B00000000, B11111111, B01100000, B00011000, B00000110, B11111111,
B00000000, B00000000, B00000000, B01111110, B10000001, B10000001, B10000001, B01111110,
B00000000, B00000000, B00000000, B11111111, B10010000, B10010000, B10010000, B01100000,
B00000000, B00000000, B00000000, B01111110, B10000001, B10000101, B10000011, B01111111,
B00000000, B00000000, B00000000, B11111111, B10010000, B10011000, B10010100, B01100011,
B00000000, B00000000, B00000000, B01100010, B10010001, B10001001, B10001001, B01000110,
B00000000, B00000000, B00000000, B10000000, B10000000, B11111111, B10000000, B10000000,
B00000000, B00000000, B00000000, B11111110, B00000001, B00000001, B00000001, B11111110, //85 'U'
B00000000, B00000000, B00000000, B11111100, B00000010, B00000001, B00000010, B11111100,
B00000000, B00000000, B00000000, B11111110, B00000001, B00000110, B00000001, B11111110,
B00000000, B00000000, B00000000, B11000011, B00100100, B00011000, B00100100, B11000011,
B00000000, B00000000, B00000000, B11000000, B00100000, B00011111, B00100000, B11000000,
B00000000, B00000000, B00000000, B10000111, B10001001, B10010001, B10100001, B11000001, //90 'Z'
B00111100, B01000010, B10100101, B10001001, B10001001, B10100101, B01000010, B00111100, //91 '[' prints as saddy
B00000000, B00000000, B00000000, B10000000, B01100000, B00011000, B00000110, B00000001,
B00111100, B01000010, B10101001, B01000001, B01000001, B10101001, B01000010, B00111100, //93 ']' prints as smiley
B00111000, B01000100, B10000010, B00100000, B01000000, B10000010, B01000100, B00111000, //94 '^' prints as heart
B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 //95 '_' prints as ' '
};
  //string of vocabs
char vocab[] PROGMEM =       "L^VE LOVE PARTY PARTY_TIME DANCE PEACE HOPE ] ^_LOVE_^ START_AGAIN_[ MUSIC FOOD ]_[_] ENERGY MOVE HI WHAT_ARE_YOU_ON IN_LOVE THE_FIRST_TIME "
                          "^ JE_T'AIME CHILL SUN_RAIN SUN RAIN SNOW!! MOVE_OVER FREE COOL COOL_IT HI_FIVE RESPECT ROCK HIP HIPPY FREE FREEDOM "
                          "^ ] BRO SISTER FREEDOM ]DJ] ROBOT ROCKET_SURGEON PROTEST GIVE DON'T_TAKE ELDWICK.ORG.UK "; // party
unsigned long vocabLen;

unsigned long last, first, refresh_first, very_first; //last is the time for start of this tick (line of LEDs), first is the time for start of this line
int cnum, bnum, wStart, wEnd;
char thisC, thisB;


/*********************************
*
*********************************/
void setup() {
  vocabLen = strlen_P(vocab);
  for (int i=0; i<8; i++) pinMode(pin0+i, OUTPUT);   //8 for letters
  pinMode(pinIn, INPUT); //button pin not used in this version
  pinMode(2, INPUT); //interrupt pin for sleep mode
  last = millis();
  first = last;
  very_first = last;//used for shutdown to sleep mode
  refresh_first = last;
  wStart = 0; wEnd = 3;
//  wStart = 0; wEnd = 14;
  //re-seed random numbers
  randomSeed(analogRead(0));
  digitalWrite(pin0 + (analogRead(0))%8, HIGH);
  delay(500);
}

/*********************************
*
*********************************/
void loop() {
  unsigned long now = millis();
  //check input pin. If pressed do routine to generate a new phrase and justify
  int buttonState = digitalRead(pinIn);//not used
  if (now > (refresh_first + refreshTime)) {
    refresh_first = now;
    chooseWord();
  }
/////////////////////// same issue with pin 2 input +ve even with this section commented out ////////////////
  if ((now - very_first) > sleepTime) {
    for (int i = 0; i<8; i++) digitalWrite(pin0 + i, LOW);
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
    sleep_enable();          // enables the sleep bit in the mcucr register so sleep is possible. just a safety pin wakeupNow when pin 2 changes
    attachInterrupt(0,wakeupNow, CHANGE); // use interrupt 0 (pin 2) and run function
    sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
    sleep_disable();         // first thing after waking from sleep: disable sleep...
    detachInterrupt(0);      // disables interrupt 0 on pin 2 so the wakeupNow code will not be executed during normal running time.
  }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //check now against start time.
  if ((now - last) > tick) {
    last += tick;
    now = last;
    unsigned long adjTm = now - first;
    if (adjTm > ((wEnd - wStart + 5) * tPc)) { //end of phrase
      first = now;
      adjTm = 0;
    }
    cnum = adjTm/tPc;
    bnum = adjTm/tick - cnum*8;
    thisC = pgm_read_byte(vocab + wStart + cnum);
    if (cnum > (wEnd - wStart)) thisC = ' ';
    if(thisC < 32 || thisC > 126) thisC = ' ';
    thisB = pgm_read_byte(charData + (thisC - 32)*8 + bnum);
    for (int i = 0; i<8; i++) {
//      if ((thisB & 1) == 1) digitalWrite(pin0 + (14 - i)%8, HIGH); else digitalWrite(pin0 + (14 - i)%8, LOW);
      if ((thisB & 1) == 1) digitalWrite(pin0 + i, HIGH); else digitalWrite(pin0 + i, LOW);
      thisB = thisB >> 1;
    }
  } 
}

/*********************************
* addWord. by default creates one word lines
*********************************/
void chooseWord() {
  wStart = random(vocabLen);
  while ((pgm_read_byte(vocab + wStart) != ' ') && (wStart >= 0)) wStart--; //go backwards to find start of word
  wStart++;
  wEnd = wStart;
  while ((pgm_read_byte(vocab + wEnd) != ' ') && (wEnd < vocabLen)) wEnd++; //go forwards to find end of word
}

/**************
*
**************/
void wakeupNow() {
  very_first = millis();
  refresh_first = very_first; 
//  randomSeed(analogRead(0));
}