|
楼主 |
发表于 2017-4-3 10:02
|
显示全部楼层
变色兔子代码:
[mw_shl_code=bash,true]#include <OneWire.h>
#include <Adafruit_NeoPixel.h>
#define MAX_T 34.20
#define N 12
#define DS18S20_Pin 11
#define PIN A3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
#define DEBUG Serial.print
#define DEBUGLN Serial.println
//#define DEBUG
//#define DEBUGLN
unsigned char colors[N][3] = {
0, 30, 200,
0, 100, 200,
0, 200, 200,
0, 200, 200,
0, 200, 120,
0, 200, 0,
100, 200, 0,
200, 200, 0,
200, 100, 0,
200, 100, 0,
200, 50, 0,
200, 0, 0,
};
OneWire ds(DS18S20_Pin);
float avg_t = 0.0;
////////////////////////////////////////////
void setup(void) {
Serial.begin(9600);
strip.begin();
pinMode(DS18S20_Pin, INPUT_PULLUP);
//pinMode(DS18S20_Pin, LED_BUILTIN);
int i = 0;
while (true) {
float t = getTemp();
DEBUGLN(t);
if ( (t < 50) && t > -50) break;
delay(30);
}
for (i = 0; i < 3; i++) {
avg_t += getTemp();
delay(300);
}
avg_t = avg_t / i;
DEBUGLN(avg_t);
}
/////////////////////////////////////////////
void loop(void) {
float t = getTemp();
DEBUGLN(t);
DEBUGLN(avg_t);
if ( (t < 50) && t > -50) {
show_led(t);
}
delay(30);
}
///////////////////////////////////////////////
void show_led(float t)
{
int r = colors[0][0], g = colors[0][1], b = colors[0][2];
float j = N * ((t - avg_t) / (MAX_T - avg_t)) ;
DEBUG("abc::");
DEBUGLN(j);
DEBUG("t::");
DEBUGLN(t);
if (j < 1)
;
else if (j >= N) {
r = colors[11][0];
g = colors[11][1];
b = colors[11][2];
}
else {
int jx = floor(j);
int js = ceil(j);
int r1 = colors[jx - 1][0];
int g1 = colors[jx - 1][1];
int b1 = colors[jx - 1][2];
int r2 = colors[jx][0];
int g2 = colors[jx][1];
int b2 = colors[jx][2];
r = r1 + (j - jx) * (r2 - r1);
g = g1 + (j - jx) * (g2 - g1);
b = b1 + (j - jx) * (b2 - b1);
}
for (int i = 0; i < 8; i++)
{
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
}
delay(50);
}
//////////////////////////////////////////////////
float getTemp() {
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1);
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for (int i = 0; i < 9; i++) {
data = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB);
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}[/mw_shl_code] |
|