本帖最后由 玉皇大帝 于 2015-6-25 13:16 编辑
源代码在此,求大神看看哪里有问题
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_NeoPixel.h>
#define STRIP_PIN 8
#define HOUR_PIN 3
#define MINUTE_PIN 4
#define BTN_PIN 5
#define SET_PIN 6
#define DEBOUNCE_DELAY 10
#define MAX_BUTTONS_INPUT 20
#define MAX_MODES 3
#define MAX_PALETTES 4
#define CLOCK_PIXELS 5
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, STRIP_PIN, NEO_RGB + NEO_KHZ800);
byte bits[CLOCK_PIXELS];
uint32_t colors[MAX_PALETTES][4] =
{
{
strip.Color(255,255,255), // off
strip.Color(255,10,10), // hours
strip.Color(10,255,10), // minutes
strip.Color(10,10,255) // both;
},
{
strip.Color(255,255,255), // off
strip.Color(255,10,10), // hours
strip.Color(248,202,0), // minutes
strip.Color(10,10,255) // both;
},
{
strip.Color(255,255,255), // off
strip.Color(228,101,43), // hours
strip.Color(158,168,38), // minutes
strip.Color(28,224,208) // both;
},
{
strip.Color(255,255,255), // off
strip.Color(203,36,2), // hours
strip.Color(184,220,60), // minutes
strip.Color(53,35,93) // both;
}
};
RTC_DS1307 rtc;
byte oldHours = 0;
byte oldMinutes = 0;
int lastButtonValue[MAX_BUTTONS_INPUT];
int currentButtonValue[MAX_BUTTONS_INPUT];
int mode = 0;
int palette = 0;
void setup()
{
// Initialize the strip and set all pixels to 'off'
strip.begin();
strip.show();
// Open serial comm with the PC
Serial.begin(9600);
Wire.begin();
rtc.begin();
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// Make the random() function return unpredictable results
randomSeed(rtc.now().unixtime());
pinMode(HOUR_PIN, INPUT);
pinMode(MINUTE_PIN, INPUT);
pinMode(BTN_PIN, INPUT);
pinMode(SET_PIN, INPUT);
pinMode(13, OUTPUT);
for(int i=0;i<2;i++)
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
}
void loop()
{
// 9:25
// setPixel(0, strip.Color(255,255,255));
// setPixel(1, strip.Color(255,10,10));
// setPixel(2, strip.Color(10,255,10));
// setPixel(3, strip.Color(10,10,255));
// setPixel(4, strip.Color(255,10,10));
// strip.show();
// return;
// Read buttons
int set_button = debounce(SET_PIN);
int hour_button = debounce(HOUR_PIN);
int minute_button = debounce(MINUTE_PIN);
int button = debounce(BTN_PIN);
if( set_button && hour_button && hasChanged(HOUR_PIN))
{
DateTime fixTime = rtc.now();
rtc.adjust( DateTime(fixTime.unixtime() + 3600) );
displayCurrentTime();
}
else if( set_button && minute_button && hasChanged(MINUTE_PIN))
{
DateTime fixTime = rtc.now();
rtc.adjust( DateTime(
fixTime.year(),
fixTime.month(),
fixTime.day(),
fixTime.hour(),
(fixTime.minute()+5)%60,
fixTime.second()) );
displayCurrentTime();
}
else if( hour_button && hasChanged(HOUR_PIN))
{
palette = (palette+1)%MAX_PALETTES;
oldHours = 99;
}
else if( button && hasChanged(BTN_PIN))
{
mode = (mode+1)%MAX_MODES;
}
// Store buttons new values
resetButtonValues();
Serial.println(mode, DEC);
switch(mode)
{
case 0:
displayCurrentTime();
break;
case 1:
oldHours = 99;
rainbowCycle(20);
break;
case 2:
oldHours = 99;
rainbow(20);
break;
}
}
int debounce(int pin)
{
int val = digitalRead(pin);
if( val == lastButtonValue[pin] )
{
currentButtonValue[pin] = val;
return val;
}
delay(DEBOUNCE_DELAY);
val = digitalRead(pin);
if( val != lastButtonValue[pin] )
{
currentButtonValue[pin] = val;
return val;
}
currentButtonValue[pin] = lastButtonValue[pin];
return lastButtonValue[pin];
}
boolean hasChanged(int pin)
{
return lastButtonValue[pin] != currentButtonValue[pin];
}
void resetButtonValues()
{
for(int i=0; i<MAX_BUTTONS_INPUT; i++)
lastButtonValue = currentButtonValue;
}
void displayCurrentTime()
{
DateTime now = rtc.now();
setTime(now.hour()%12, now.minute());
}
void setTime(byte hours, byte minutes)
{
if(oldHours == hours && oldMinutes/5 == minutes/5)
return;
oldHours = hours;
oldMinutes = minutes;
for(int i=0; i<CLOCK_PIXELS; i++)
bits |