其實在遙控器的按鍵中,有兩種模式,1.按一次發射一次2.連續發送,比方說上下左右的按鍵,通常都會連續發射,而其他的功能鍵,一般按一次只會發射一次
我們本次要利用按鍵來做RGB三個顏色的個別調光,上下來調整紅光,左右調整綠光、keystone+-兩顆按鍵調整藍光
相關接線方式:
程式範例:
========================================================
#include <SoftSerial.h>
#include <WS2811.h>
DEFINE_WS2811_FN(WS2811RGB, PORTB, 3)
//設定PIN3控制RGB LED
RGB_t rgb[8]; //1 for 1 pixel
int RGB_NUM;
uint8_t situation = 0;
uint8_t START = 0;
uint8_t x = 0;
uint8_t BIT = 0;
uint8_t Id = 0;
uint8_t Id_inv = 0;
uint8_t Data = 0;
uint8_t Data_back = 0;
uint8_t Data_inv = 0;
uint8_t Repeat = 0;
uint8_t sended = 0;
uint16_t Time_old = 0;
uint16_t Time = 0;
uint16_t TimeDelta = 0;
SoftSerial Serialport(5, 0 ); //設定Rx/Tx對應的PIN
unsigned char Serialdata[32],Serial_DATA;
int Serial_num;
void setup(void) {
attachInterrupt(0, IR_Read, FALLING);
pinMode(1,OUTPUT);
digitalWrite(1,LOW);
pinMode(3,OUTPUT); //for WS2811 RGB LED
Serialport.begin(9600);
Serialport.write("SoftSerial Test \r\n");
rgb[0].r-=15;
}
void loop(void) {
if(sended == 1) {
Serialport.write("IR data:"); //傳送字串
Char_to_Uart((unsigned char)Id);
Serialport.write(","); //傳送字串
Char_to_Uart((unsigned char)Id_inv);
Serialport.write(","); //傳送字串
Char_to_Uart((unsigned char)Data_back);
Serialport.write(","); //傳送字串
Char_to_Uart((unsigned char)Data_inv);
Serialport.write(",\r\n"); //傳送字串
if((uint8_t) Data_back==33){ rgb[0].r=0; rgb[0].b=0; rgb[0].g=0; }
if((uint8_t) Data_back==65 && rgb[0].r<100) rgb[0].r+=2;
if((uint8_t) Data_back==161 && rgb[0].r>0) rgb[0].r-=2;
if((uint8_t) Data_back==129 && rgb[0].g<100) rgb[0].g+=2;
if((uint8_t) Data_back==19 && rgb[0].g>0) rgb[0].g-=2;
if((uint8_t) Data_back==97 && rgb[0].b<100) rgb[0].b+=2;
if((uint8_t) Data_back==81 && rgb[0].b>0) rgb[0].b-=2;
sended = 0;
} else {
}
updatePixels(); //show the change
delay(20);
}
void updatePixels(){
WS2811RGB(rgb, ARRAYLEN(rgb));
}
void IR_Read(void) {
digitalWrite(1,HIGH);
Time = micros();
if (Time_old != 0) {
TimeDelta = Time - Time_old;
if ((TimeDelta > 12000)&&(TimeDelta < 14000)) {
START = 1;
x = 0;
situation = 1;
Id = 0;
Id_inv = 0;
Data = 0;
Data_inv = 0;
} else if ((TimeDelta > 10000)&&(TimeDelta < 12000)) {
situation = 2; // repeat
} else if ((TimeDelta > 1500)&&(TimeDelta < 2500)) {
situation = 3; //"1"
BIT = 1;
} else if ((TimeDelta > 1000)&&(TimeDelta < 1500)) {
situation = 3; //"0"
BIT = 0;
} else situation = 5;
if (situation == 3) {
if (x < 8) {
Id |= BIT;
if (x < 7) Id <<= 1;
x++;
} else if (x < 16) {
Id_inv |= BIT;
if (x < 15) Id_inv <<= 1;
x++;
} else if (x < 24) {
Data |= BIT;
if (x < 23) Data <<= 1;
x++;
} else if (x < 32) {
Data_inv |= BIT;
if (x < 31) {
Data_inv <<= 1;
} else {
/* DO SOMETHING HERE */
sended = 1;
Data_back = Data;
Repeat = 0;
}
x++;
}
} else if (situation == 2) {
if(Repeat == 1) {
/* DO SOMETHING HERE */
/*sended = 1;*/
} else {
Repeat = 1;
}
}
}
Time_old = Time;
digitalWrite(1,LOW);
}
void Char_to_Uart(unsigned char ChartoUart_NUM) {
unsigned char ChartoUart_TEMP;
ChartoUart_TEMP=ChartoUart_NUM/100;
Serialport.write(ChartoUart_TEMP+'0');
ChartoUart_NUM=ChartoUart_NUM-(ChartoUart_TEMP*100);
ChartoUart_TEMP=ChartoUart_NUM/10;
Serialport.write(ChartoUart_TEMP+'0');
ChartoUart_NUM=ChartoUart_NUM-(ChartoUart_TEMP*10);
ChartoUart_TEMP=ChartoUart_NUM;
Serialport.write(ChartoUart_TEMP+'0');
}
實際測試影片
沒有留言:
張貼留言