Looks like I'm overshooting the storage of my Adafruit Trinket:
Sketch uses 5,600 bytes (105%) of program storage space. Maximum is 5,310 bytes.
Global variables use 109 bytes of dynamic memory.
Any tips for reducing the footprint of my code?
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <RTClib.h>
#define LEDPIN 1
#define TONE 4
#define BTNPIN 3
RTC_DS1307 rtc;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, LEDPIN, NEO_RGB);
long fadeTime = 1 * 10000L; // X minutes
int colorStops = 256;
int delaySpeed = fadeTime / colorStops;
int notes[] = {262,294,330,349};
int timeHour = 14;
int timeMinute = 43;
uint32_t alarmLength = 5 * 60000L; // 5 minutes
void setup() {
rtc.begin();
if(!rtc.isrunning()) {
rtc.adjust(DateTime(__DATE__, __TIME__));
}
pinMode(TONE,OUTPUT);
pinMode(BTNPIN, INPUT);
digitalWrite(BTNPIN, HIGH);
strip.begin();
strip.show();
}
void loop() {
DateTime now = rtc.now();
if (now.hour() == timeHour && now.minute() == timeMinute && now.second() == 0){
// Fade in light
for(int i = 1; i<colorStops; i++){
if (kill() == true){ break; }
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(i,i,0));
}
strip.show();
delay(delaySpeed);
}
// Play tone after light fully bright
for( uint32_t tStart = millis(); (millis()-tStart) < alarmLength; ){
if (kill() == true){ break; }
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(random(100,255),random(100,255),random(100,255)));
}
strip.show();
beep(TONE, notes[random(0,3)], 50);
delay(100);
}
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(0,0,0));
}
strip.show();
}
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}
bool kill() {
if (! digitalRead(BTNPIN)) {
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(0,0,0));
}
strip.show();
return true;
}
else {
return false;
}
}
byteis [0, 255], not [-127, 127], as it is typedefed to be anuint8_t. Andintis 2 bytes, not 4. – Edgar Bonet Mar 31 '16 at 20:22beepfunction. Ultimately trying to just make the piezo generate a set of rapid beeping noises (this thing is for an alarm). – Shpigford Mar 31 '16 at 21:40I'm wondering if there's a more compact way to execute the intent behind the beep function- I wrote a small library to do that, however for the Atmega328. You should be able to adapt it. http://www.gammon.com.au/forum/?id=11504&reply=11#reply11 – Nick Gammon Mar 31 '16 at 23:05