蓝牙控制LED灯带arduino

时间:2015-02-05 23:47:23

标签: android c++ arduino led

我目前正在开展一个手机控制RGB LED灯条的项目。我相信我的机器人方面工作得体,足以让一切都运转起来。我遇到的问题是,当我按下手机上的单一颜色时,条带工作得很好,但是当我点击闪光灯或淡入淡出按钮时,LED条带工作但仅适用于那一个功能,然后它断开或不起作用。

我的目标是点击任何按钮,无论它是纯色还是闪光灯,并希望它能够正常工作。任何帮助将不胜感激。

#include <SoftwareSerial.h>

int bluetoothTx = 2;
int bluetoothRx = 3;

int RedPin = 10;
int GreenPin = 5;
int BluePin = 6;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
String data;
char inData;
int FADESPEED = 5;

void setup()
{
 Serial.begin(9600); 

 bluetooth.begin(115200); 
 delay(100);
 //bluetooth.println("U,9600,N");
 bluetooth.begin(9600);
}


void loop()
{ 
  if(bluetooth.available() > 0)
  {
    inData = (char)bluetooth.read();
    if(inData != '\n')
    {
      data += inData;
    }
  }
  else
  {
    data = "";
  }

//**********************************************************  
  if (data == "Red")
  {//if value from bluetooth serial is n
    analogWrite(RedPin, 255);
    analogWrite(GreenPin, 0);
    analogWrite(BluePin, 0);    //switch on LED
  }
//********************************************************** 
  else if (data == "Blue")
  {//if value from bluetooth serial is n
    analogWrite(RedPin, 0);
    analogWrite(GreenPin, 0);
    analogWrite(BluePin, 255);   //turn off LED
  }  
//********************************************************** 
  else if (data == "Green")
  {
    analogWrite(RedPin, 0);
    analogWrite(GreenPin, 255);
    analogWrite(BluePin, 0);
  }
//********************************************************** 
  else if (data == "R1")
  {
    analogWrite(RedPin, 255);
    analogWrite(GreenPin, 0);
    analogWrite(BluePin, 125);
  }
//********************************************************** 
  else if (data == "B1")
  {
    analogWrite(RedPin, 0);
    analogWrite(GreenPin, 125);
    analogWrite(BluePin, 255);
  }
//********************************************************** 
  else if (data == "G1")
  {
    analogWrite(RedPin, 125);
    analogWrite(GreenPin, 255);
    analogWrite(BluePin, 0);
  }
//********************************************************** 
  else if (data == "R2")
  {
    analogWrite(RedPin, 255);
    analogWrite(GreenPin, 0);
    analogWrite(BluePin, 255);
  }
//********************************************************** 
  else if (data == "B2")
  {
    analogWrite(RedPin, 0);
    analogWrite(GreenPin, 255);
    analogWrite(BluePin, 255);
  }
//********************************************************** 
  else if (data == "G2")
  {
    analogWrite(RedPin, 255);
    analogWrite(GreenPin, 255);
    analogWrite(BluePin, 0);
  }
//********************************************************** 
  else if (data == "R3")
  {
    analogWrite(RedPin, 125);
    analogWrite(GreenPin, 0);
    analogWrite(BluePin, 255);
  }
//********************************************************** 
  else if (data == "B3")
  {
    analogWrite(RedPin, 0);
    analogWrite(GreenPin, 255);
    analogWrite(BluePin, 125);
  }
//********************************************************** 
  else if (data == "G3")
  {
    analogWrite(RedPin, 255);
    analogWrite(GreenPin, 125);
    analogWrite(BluePin, 0);
  }
//********************************************************** 
  while(data == "Strobe" && bluetooth.available() == 0)
    {
       analogWrite(RedPin, 255);
       analogWrite(GreenPin, 255);
       analogWrite(BluePin, 255);
       delay(40);
       analogWrite(RedPin, 0);
       analogWrite(GreenPin, 0);
       analogWrite(BluePin, 0);
       delay(40);
    }
//********************************************************** 
  while(data == "Smooth" && bluetooth.available() == 0)
  {
    {
      int r, g, b;
      // fade from blue to violet
      for (r = 0; r < 256; r+5) 
      { 
        analogWrite(RedPin, r);
        delay(FADESPEED);
      } 
      // fade from violet to red
      for (b = 255; b > 0; b-5) 
      { 
        analogWrite(BluePin, b);
        delay(FADESPEED);
      } 
      // fade from red to yellow
      for (g = 0; g < 256; g+5) 
      { 
        analogWrite(GreenPin, g);
        delay(FADESPEED);
      } 
      // fade from yellow to green
      for (r = 255; r > 0; r-5) 
      { 
        analogWrite(RedPin, r);
        delay(FADESPEED);
      } 
      // fade from green to teal
      for (b = 0; b < 256; b+5) 
      { 
        analogWrite(BluePin, b);
        delay(FADESPEED);
      } 
      // fade from teal to blue
      for (g = 255; g > 0; g-5) 
      { 
        analogWrite(GreenPin, g);
        delay(FADESPEED);
      }
    }
  }
//**********************************************************
}

0 个答案:

没有答案