2016年4月28日 星期四

遙控變色燈

相信很多人去逛IKEA都對這蠟燭台很有印象,感覺很好看,但又不知道買了要做啥.
顏色還很多種可以挑選呢.燭台.感覺有點危險,

或許拿來當營燈也不錯,網路上有很多達人都改裝成不同的LED.
要是跟大家都一樣,創意算是抄襲的.
所以囉.來點不一樣的.我的版本是遙控變色LED燈


材料如下:
IKEA蠟燭台,
Arduino Nano控制板,
3.7V手機電池,
5V升壓板,
AT89S52 315M遙控模組,
NeoPixel LED Ring,
泡棉一塊....


預計規劃的功能如下
當按下遙控器按鈕A 開啟紅燈,再次按下則關閉紅燈.
當按下遙控器按鈕B 開啟藍燈,再次按下則關閉藍燈.
當按下遙控器按鈕C 開啟綠燈,再次按下則關閉綠燈.
藉由ABC三個按鈕的交叉組合可以出現以下七種顏色變化
如按鈕D啟動, 則呈現DEMO展示,呈現彩虹色環的旋轉變化
程式碼的部分直接使用Adafruit_NeoPixel的DEMO code.小改一下很快就完成了.

剩下就是買零件組合.
這回要利用廢棄的手機電池當作電源的來源,需要是3.7V轉5V的電源供應機制.
如果不想用手機電池,其實可以用三顆1.2V的充電電池搭配電池盒與升壓板組合.
但考慮到只有狹小的燭台頂部空間可以收納這些零件.還是使用手機電池好了.

以下是直接修改DEMO code的文件 加入AT89S52 315M的控制於PIN8/9/10/11.
NeoPixel的控制接在PIN6,總計目前使用五個訊號端.
不過.目前手上還有一個G Sensor.可以考慮一下放進去.如果燈發生搖晃時.發生閃爍嗎?不過Arduino的I2C我還不懂.這可以當作下回的練習.



#include 

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino 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(16, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// defines pins numbers
const int PinD0 = 8; // D8 link to AT89S52 315M AVR D0
const int PinD1 = 9; // D9 link to AT89S52 315M AVR D1
const int PinD2 = 10; // D10 link to AT89S52 315M AVR D2
const int PinD3 = 11; // D11 link to AT89S52 315M AVR D3
const int Pulses = 200; //Define delay time 1000 = 1 sec
int BTN_A = 0;
int BTN_B = 0;
int BTN_C = 0;
int BTN_D = 0;
int VAL_R = 127;
int VAL_B = 127;
int VAL_G = 127;
int Color_R;
int Color_B;
int Color_G;

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  Serial.begin(9600);
  Serial.println("Hello! I'm Arduino LED Ring.\n");
  // Sets the 4 pins as Input from AT89S52 315M
  pinMode(PinD0, INPUT);
  pinMode(PinD1, INPUT);
  pinMode(PinD2, INPUT);
  pinMode(PinD3, INPUT);
}

void loop() {
  BTN_A = digitalRead(PinD0);
  BTN_B = digitalRead(PinD1);
  BTN_C = digitalRead(PinD2);
  BTN_D = digitalRead(PinD3);
  // Some example procedures showing how to display to the pixels:

  if (BTN_A == 1) { // Red enable
    Color_R = VAL_R;
  }
  else {
    Color_R = 0;
  }
  if (BTN_B == 1) { // Blue enable
    Color_B = VAL_B;
  }
  else {
    Color_B = 0;
  }
  if (BTN_C == 1) { // Green enable
    Color_G = VAL_G;
  }
  else {
    Color_G = 0;
  }
  if (BTN_D == 1) { // DEMO mode
    rainbow(20);
    rainbowCycle(20);
    theaterChaseRainbow(50);
  }
  else { // Normal mode
    colorWipe(strip.Color(Color_R, Color_G, Color_B), 20)
    theaterChase(strip.Color(Color_R, Color_G, Color_B), 50);
  }

  //  colorWipe(strip.Color(255, 0, 0), 50); // Red
  //  colorWipe(strip.Color(0, 255, 0), 50); // Green
  //  colorWipe(strip.Color(0, 0, 255), 50); // Blue
  //  // Send a theater pixel chase in...
  //  theaterChase(strip.Color(127, 127, 127), 50); // White
  //  theaterChase(strip.Color(127,   0,   0), 50); // Red
  //  theaterChase(strip.Color(  0,   0, 127), 50); // Blue

}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, c);  //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

沒有留言:

張貼留言

2024東京迪士尼,愛與夢想的樂園行

疫情的關係, 很多年沒有出國旅遊了.這回計畫出遊日本.歷經磨難的挑選.最後勝出的行程是五福旅行社的"銀色雪東京五日-戲雪,和服體驗,迪士尼"這標題簡單的標註出此行的目的. 上回去東京是20年前了.可以參考一下過去 2004年的Blog紀錄 心中的願景是能看到前...