|
#include <IRremote.h>
//#include <IRremoteInt.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define PIN 11
#define LED_number 16
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_number, PIN, NEO_GRB + NEO_KHZ800);
#define WIFI_TX 9
#define WIFI_RX 8
SoftwareSerial wifi(WIFI_RX, WIFI_TX); //RX, TX
String _comdata_wifi = ""; //for incoming wifi serial data
int BlueColor=000;
int GreenColor=000;
int RedColor=000;
int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
irrecv.enableIRIn();
Serial.begin(9600);
wifi.begin(115200);
strip.begin();
strip.setBrightness(50);
Serial.println("system is ready!");
wifi.println("AT+CWMODE=3\r\n");
delay(500);
wifi.println("AT+CIPMUX=1\r\n");
delay(500);
wifi.println("AT+CIPSERVER=1,5000\r\n");
delay(500);
}
void loop() {
if(Serial.available()){
String order = "";
while (Serial.available()){
char cc = (char)Serial.read();
order += cc;
delay(2);
}
order.trim();
wifi.println(order);
}
String wifidata=getWifiSerialData();
if(wifidata!=""){
Serial.println(wifidata);
Serial.println(wifidata.substring(12,15));
if(wifidata.substring(12,15)=="RGB")
{
RedColor=wifidata.substring(15,18).toInt();
GreenColor=wifidata.substring(18,21).toInt();
BlueColor=wifidata.substring(21,24).toInt();
//colorRGB(RedColor,GreenColor,BlueColor);
colorRGB1(RedColor,GreenColor,BlueColor);
Serial.print(GreenColor);
Serial.print(RedColor);
Serial.print(BlueColor);
Serial.print("\r\n");
delay(20);
}
}
wifidata="";
if (irrecv.decode(&results)) {
switch(results.value)
{
case 0xFF6897:colorRGB1(0,0,0);break;//0
case 0xFF30CF:colorRGB1(255,0,0);break;//1
case 0xFF18E7:colorRGB1(255,128,0);break;//2
case 0xFF7A85:colorRGB1(255,255,0);break; //3
case 0xFF10EF:colorRGB1(0,255,0);break; //4
case 0xFF38C7:colorRGB1(0,255,255);break;//5
case 0xFF5AA5:colorRGB1(0,0,255);break;//6
case 0xFF42BD:colorRGB1(128,0,255);break;//7
case 0xFF4AB5:colorRGB1(255,255,255);break;//8
}
irrecv.resume(); // 接收下一个值
}
}
//void colorRGB(int red, int green, int blue){
// analogWrite(RedPin,constrain(red,0,255));
// analogWrite(GreenPin,constrain(green,0,255));
// analogWrite(BluePin,constrain(blue,0,255));
//}
void colorRGB1(int red, int green, int blue){
for(uint8_t j=0;j<16;j++)
{
strip.setPixelColor(j,constrain(red,0,255),constrain(green,0,255),constrain(blue,0,255));
strip.show();
delay(2);
}
delay(200);
}
String readTtl() {
String comdata = "";
while (Serial.available())
{
comdata += char(Serial.read());
delay(2);
}
return comdata;
}
String getWifiSerialData(){
String _comdata_wifi = ""; //for incoming wifi serial data
while (wifi.available()>0){
_comdata_wifi += char(wifi.read()); //get wifi data
}
return _comdata_wifi;
}
我是想做一个可以同时红外和wifi控制的led彩灯,wifi可以单独控制颜色,红外也可以单独控制颜色,但两个代码合在一起就不行了。
只能红外遥控器控制,wifi控制不能。
就是代码中的 irrecv.enableIRIn(); 这个红外启动函数导致了wifi乱码。
原本wifi单独接收,控制颜色没有问题
setup()加了 irrecv.enableIRIn(); 后就乱码,就wifi控制不了颜色,但wifi还是有接收到数据的
请问有什么好的解决方法,试过改波特率,但是好像不行,因为硬件模块都是固定的。
|
|