极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9821|回复: 1

arduino编程问题

[复制链接]
发表于 2013-4-24 13:00:57 | 显示全部楼层 |阅读模式
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>

  3. // Data wire is plugged into port 2 on the Arduino
  4. #define ONE_WIRE_BUS 12
  5. int jia=6; //设定控制LED的数字IO脚
  6. int temPin=13;
  7. int huan=7;//设置控制按键的数字IO脚
  8. int shi=3;
  9. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  10. OneWire oneWire(ONE_WIRE_BUS);
  11. DallasTemperature sensors(&oneWire);
  12. int i;//wen
  13. int j;//guang
  14. int t;
  15. char val;
  16. void setup( )
  17. {
  18.   Serial.begin(9600);
  19.   sensors.begin();
  20.    pinMode(jia,OUTPUT);//设置数字IO引脚为输出模式
  21.     pinMode(huan,OUTPUT);//设定数字IO口的模式,OUTPUT 为输出
  22.   // start serial port
  23.   pinMode(temPin,OUTPUT);
  24. }
  25. void loop()
  26. {
  27. int t=26;
  28. int i=0;
  29. int j=0;

  30.    val=Serial.read();
  31.             if(val=='A')  
  32.         {
  33.              digitalWrite(shi,HIGH);
  34.               delay(5000);  
  35.               digitalWrite(shi,LOW);  
  36.         }
  37.          else if(val=='B')  
  38.         {
  39.              digitalWrite(huan,HIGH);
  40.               delay(60000);  
  41.               digitalWrite(huan,LOW);  
  42.         }
  43.             else if(val=='C')  
  44.         {
  45.              digitalWrite(jia,HIGH);
  46.               delay(5000);  
  47.               digitalWrite(jia,LOW);  
  48.         }
  49.             
  50.              else if(val=='D')  
  51.         {
  52.               sensors.requestTemperatures();
  53.               Serial.print("Temperature for the device 1 (index 0) is: ");
  54.               Serial.println(sensors.getTempCByIndex(0));   
  55.         }
  56.            else if(val=='T')  
  57.         {

  58.               Serial.print("SET Temperature  is: ");
  59.               Serial.println("t");   
  60.         }
  61.         else if(val=='1')
  62.        {
  63.        t=21;
  64.        }
  65.          else if(val=='2')
  66.        {
  67.        t=22;
  68.        }
  69.          else if(val=='3')
  70.        {
  71.        t=23;
  72.        }
  73.          else if(val=='4')
  74.        {
  75.        t=24;
  76.        }
  77.          else if(val=='5')
  78.        {
  79.        t=25;
  80.        }
  81.          else if(val=='6')
  82.        {
  83.        t=26;
  84.        }
  85.          else if(val=='7')
  86.        {
  87.        t=27;
  88.        }
  89.          else if(val=='8')
  90.        {
  91.        t=28;
  92.        }
  93.          else if(val=='9')
  94.        {
  95.        t=29;
  96.        }
  97.          else if(val=='10')
  98.        {
  99.        t=30;
  100.        }
  101.                if(i>100)
  102.              {
  103.                digitalWrite(jia,HIGH);  
  104.               delay(5000);
  105.                digitalWrite(jia,LOW);  
  106.              }
  107.            if(j>100)
  108.                {
  109.                digitalWrite(huan,HIGH);  
  110.               delay(60000);
  111.                digitalWrite(huan,LOW);  
  112.              }
  113.   
  114.   if(analogRead(12)>150)
  115.   {i=i+1;}
  116.   sensors.requestTemperatures();
  117.   if(sensors.getTempCByIndex(0)<t)
  118.   j=j+1;
  119.    delay(5);
  120. }
复制代码

这是自己写的一个蓝牙控制程序,很粗糙,拷到arduino上后很不稳定,怎样优化一下呢
回复

使用道具 举报

发表于 2013-4-24 16:44:10 | 显示全部楼层
1.判断那里不要用太多if,可以用switch来代替
2.val的字符类型,可以把val转成int类型再判断,那么12345678910那里直接就可以用一句代替掉了

转类型方法有两种
要不就是int a = val - '0'
要不就用atoi()函数
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-5 20:13 , Processed in 0.039450 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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