极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

蓝牙控制LED灯的亮度问题

[复制链接]
 楼主| 发表于 2014-1-22 16:41:09 | 显示全部楼层
上位机程式。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-22 16:42:54 | 显示全部楼层
暗风帮我看看程式
回复 支持 反对

使用道具 举报

发表于 2014-1-22 17:07:01 | 显示全部楼层
本帖最后由 pww999 于 2014-1-22 17:16 编辑

int val

......


void loop(){
  //read the signal from bt

   if(mySerial.available()>0){
       val=mySerial.read();
   delay(200);
   }
Serial.println(val);
analogWrite(pinLED,val);
}

要放在外面吧?
如果上位机自动发送复位0 时,  接收端就要加变量保存大于0的当前值??
回复 支持 反对

使用道具 举报

发表于 2014-1-22 22:56:39 | 显示全部楼层
Serial.read()读1byte,上位送2bytes
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-23 09:11:08 | 显示全部楼层
1 byte最大127, 2 byte才能发送255大小的数据.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-23 09:31:28 | 显示全部楼层
Belive是对的, 太感谢了! 我修改了上位机程序, 现在正常了. 下位机需要调整一下delay时间为10,以减少滞后,改为10调光比较平顺。谢谢大家的帮助!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-23 09:34:18 | 显示全部楼层
本帖最后由 活着就是幸福 于 2014-1-23 09:45 编辑

调整后的程序
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial mySerial(2,3);//Define RX,TX
  3. int pinLED=11;
  4. int val=0;
  5. void setup(){
  6.    Serial.begin(9600); //define the bounds rate
  7.    mySerial.begin(9600); //define bt bounds rate
  8.    pinMode(pinLED,OUTPUT);
  9. }

  10. void loop(){
  11.   //read the signal from bt
  12.   while(mySerial.available()>0){
  13.         val=mySerial.read();//arduino received 1 byte each time.
  14.         Serial.println(val);
  15.         analogWrite(pinLED,val);
  16.    delay(10);
  17.    }
  18. }
复制代码


上位机程序:




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-23 09:45:57 | 显示全部楼层
有兴趣的哥们可以测试一下。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-23 12:08:08 | 显示全部楼层
另外,拓展一下,怎样控制多盏LED呢?我试了一下,但是有个问题,Serial.read()接收不了大于255的数值, 如何识别来自不同slider的信号? 通过不同数值范围看来是行不通了。哪位大虾解答一下?
  1. /* Android control several LEDs through BT*/

  2. #include <SoftwareSerial.h>
  3. SoftwareSerial mySerial(2,3);//Define RX,TX
  4. int pinLED1=9;
  5. int pinLED2=10;
  6. int pinLED3=11;
  7. int val=0;
  8. void setup(){
  9.    Serial.begin(9600); //define the bounds rate
  10.    mySerial.begin(9600); //define bt bounds rate
  11.    pinMode(pinLED1,OUTPUT);
  12.    pinMode(pinLED2,OUTPUT);
  13.    pinMode(pinLED3,OUTPUT);
  14. }

  15. void loop(){
  16.   //read the signal from bt
  17.   while(mySerial.available()>0){
  18.         val=mySerial.read();//arduino received 1 byte each time.
  19.         if (0<=val<256){ //the LED1 will be controlled as the val between 0-255;
  20.            Serial.println(val);
  21.            analogWrite(pinLED1,val);        
  22.         }else if(256<=val<512){ //LED2 will be controlled as the val between 255-510;
  23.            Serial.println(val);
  24.            analogWrite(pinLED2,val-255);
  25.         }else if(512<=val<767){
  26.            Serial.println(val);
  27.            analogWrite(pinLED3,val-512);
  28.         }
  29.    delay(10);
  30.    }
  31. }

复制代码


实际上val 不可能获取大于255的数值。所以以上方案不可行。有其它解决办法吗?
回复 支持 反对

使用道具 举报

发表于 2014-1-23 14:22:41 | 显示全部楼层
活着就是幸福 发表于 2014-1-23 12:08
另外,拓展一下,怎样控制多盏LED呢?我试了一下,但是有个问题,Serial.read()接收不了大于255的数值, 如 ...

发送的数据加协议进去,比如下面这个链接的
http://www.geek-workshop.com/for ... thread&tid=8415
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-23 15:25:21 | 显示全部楼层
没看明白, 我需要int格式数据.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-27 13:17:01 | 显示全部楼层
没人解答, 只好自力更生, 尝试了几种方案, 终于可以了, 但是还是有个bug, 就是当slider快速滑动时, 输出的数据不正常, 不知道为什么. 估计是上位机的原因. 以下是控制三盏灯的程序:
  1. #include <SoftwareSerial.h>

  2. /*
  3. this sample tells us how to get the specific format demand from the Serial.
  4. Letter+intNum+@ different letter leads to different command, and how to pick up
  5. the intNum data from the string and then send it to the PWM pin.

  6. initiated by Jerry Zhong Jan-27-14
  7. */
  8. SoftwareSerial mySerial(2,3);
  9. String comdata = "";
  10. int numdata=0;
  11. int pinLED1=12;
  12. int pinLED2=11;
  13. int pinLED3=10;
  14. void setup(){
  15.    Serial.begin(9600);
  16.    mySerial.begin(9600);
  17.    pinMode(pinLED1,OUTPUT);
  18. }

  19. void loop(){
  20.   int i=0,j=1;
  21.   while(mySerial.available()>0){
  22.       comdata+=char(mySerial.read());
  23.       delay(2);
  24.       i++;
  25.       //Serial.println(comdata);
  26.       switch(comdata[0]){
  27.       case 'a'://check if the first letter is 'a', if yes, get the PWM data
  28.       if(comdata[i-1]=='@'){//to check if the string command is over
  29.          //Serial.println(comdata);
  30.          for (j=1;j<(comdata.length()-1);j++){
  31.             numdata=numdata*10+(comdata[j]-'0');
  32.          }//transfer the character into integer number which could be recognized by analogWrite();
  33.         // Serial.println(j);
  34.          Serial.println(numdata); //print out the data to check if it's correct
  35.          analogWrite(pinLED1,numdata);//write the PWM data to the output pinLED.
  36.          comdata="";//clean out the varial
  37.          numdata=0;//clean out the numdata vairal
  38.          }
  39.          break;
  40.      case 'b':  
  41.      if(comdata[i-1]=='@'){
  42.          Serial.println(comdata);
  43.          for(j=1;j<(comdata.length()-1);j++){
  44.              numdata=numdata*10+(comdata[j]-'0');
  45.          }
  46.          Serial.println(numdata);
  47.          analogWrite(pinLED2,numdata);
  48.          comdata="";
  49.          numdata=0;
  50.         }
  51.         break;
  52.       case 'c':  
  53.       if(comdata[i-1]=='@'){
  54.          Serial.println(comdata);
  55.          for(j=1;j<(comdata.length()-1);j++){
  56.             numdata=numdata*10+(comdata[j]-'0');
  57.          }
  58.          Serial.println(numdata);
  59.          analogWrite(pinLED3,numdata);
  60.          comdata="";
  61.          numdata=0;
  62.       }
  63.       break;
  64.       
  65.       //default:
  66.       //Serial.println(comdata);
  67.     }
  68.   }
  69. }
复制代码



点按slider运行正常, 输出的数据也是正常的. 快速滑动有时会出现非正常数据.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-27 13:23:43 | 显示全部楼层
修订后的上位机程序: 希望有兴趣的帮忙测试一下, 发送数据格式是 '字母'+0~255+'@', 第一个字母代表不同的LED, '@' 表示字符串结束, 中间部分是调节亮度的值, 按照TEXT方式发送.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-27 13:59:39 | 显示全部楼层
快速滑动输出的数据出现超出0-255范围

222
-9462
128
125
120
-25965
105
-10238
出现超出范围的数据时, 无法继续调节
回复 支持 反对

使用道具 举报

发表于 2014-1-29 07:52:02 来自手机 | 显示全部楼层
过年在家,没办法帮你了,等回去给你验证一下,顺便给你简化一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-16 00:34 , Processed in 0.126131 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表