.Alan. 发表于 2018-2-19 09:23:52

arduino 多个变量后会边卡 求帮助

这个有两个infrared sensor,一个LM35DT,一个servo,一个LCD,一个GROVE temperature sensor

作用:
利用2个infrared sensor来控制servo的不同转向
lm35DT是用来检测水温度
GROVE是用来检测环境温度
LCD显示35和GROVE的温度度数

问题:
lm35DT在LCD显示的温度一直在10多和30多之间跳,正常温度显示是20度左右

已用失败方案:
1.components用额外电源供电
lm35差点烧了。。很烫
servo转的比平时还快,然后就失灵了

2.用中断
尝试用中断来减少arduino的工作量,但是还是没什么用


求大神给个解决方案,感激不尽,再不行只有用2个arduino分开搞了。。。

以下是我的代码
通熟易懂的

#include <Wire.h>
#define PIR_MOTION_SENSOR2 2         //Use pin 2 to receive the signal from the module
#define PIR_MOTION_SENSOR3 3
#include <Servo.h>
Servo myservo;
#include <math.h>
const int A = 4275;                // B value of the termistor
const int R0 = 100000;             // R0 = 100K
const int pinTempSensor1 = A0;   // Grove - Temperature Sensor connect to A0
#include "rgb_lcd.h"
rgb_lcd lcd;
const int colorR = 100;             //define LCD screen color
const int colorG = 100;
const int colorB = 100;                  
int pos = 0;                     //define servo intialization of angle
int tempPin = 1;
void setup()
{
lcd.begin(16, 2);                     // define screen row and column
lcd.setRGB(colorR, colorG, colorB);   //define LCD color
delay(100);
Serial.begin(9600);
pinsInit();
myservo.attach(9);            // attaches the servo on pin 9 to the servo object
}
void loop()
{
{
if(isPeopleDetected2())         //if it detects the moving people?
{
turnonlcd();                   //lcd and servo turn on
}
}
{
if(isPeopleDetected3())

{
turnofflcd();
}
}
waterTemp();
}
void pinsInit()                //define PIR sensor input
{
pinMode(PIR_MOTION_SENSOR2, INPUT);
pinMode(PIR_MOTION_SENSOR3, INPUT);
}
void turnonlcd()                           //**lcd and servo turn on function
{
pos = 0;
myservo.write(pos);
int a = analogRead(pinTempSensor1);
float Rs1 = 1023.0/a-1.0;
Rs1 = R0*Rs1;
float temp1 = 1.0/(log(Rs1/R0)/A+1/298.15)-273.15;       // convert to temperature via datasheet
lcd.setCursor(11,0);
lcd.print("P:OUT");                           // show first row imformation
lcd.setCursor(0,1);
lcd.print("ET=");
lcd.print(temp1);                               // show second row temp
delay(10);
}
void turnofflcd()                        // **lcd and servo turn off function
{
pos = 179;
myservo.write(pos);
int a = analogRead(pinTempSensor1);
float Rs1 = 1023.0/a-1.0;
Rs1 = R0*Rs1;
float temp1 = 1.0/(log(Rs1/R0)/A+1/298.15)-273.15;       // convert to temperature via datasheet
lcd.setCursor(11,0);
lcd.print("P:IN ");                            // show first row imformation
lcd.setCursor(0,1);
lcd.print("ET=");
lcd.print(temp1);                                          // show second row temp
delay(10);

}
void waterTemp()
{
int temp;
int tempC = analogRead(tempPin);
temp = tempC * 0.48828125;
lcd.setCursor(0, 0);
lcd.print("WT=");
lcd.print(temp);
delay(100);
}

boolean isPeopleDetected2()             // **Return-boolean funtion:Detect whether people moves in it's detecting range
{
int sensor2Value = digitalRead(PIR_MOTION_SENSOR2);
int sensor3Value = digitalRead(PIR_MOTION_SENSOR3);
if(sensor2Value == HIGH&&sensor3Value == LOW)//if the sensor value is HIGH?
{
return 1;                     
}
else
{
return 0;
}
}

boolean isPeopleDetected3()
{
int sensor2Value = digitalRead(PIR_MOTION_SENSOR2);
int sensor3Value = digitalRead(PIR_MOTION_SENSOR3);
if(sensor3Value == HIGH&&sensor2Value == LOW)
{
return 1;
}
else
{
return 0;
}
}

t3486784401 发表于 2018-2-19 16:50:45

本帖最后由 t3486784401 于 2018-2-19 16:52 编辑

这么多 float 还有 log 函数,当 AVR 是神仙啊。

单说语法只会用 int 么,这一句有没有试试 sizeof(int)==2......
const int R0 = 100000;             // R0 = 100K

这么点任务 AVR 是可以完成的,但这个代码的优化程度,估计算错了都不知道

简单一试便知上边的毛病:
void setup()
{
        // put your setup code here, to run once:
        Serial.begin(9600);
        Serial.println(sizeof(int));
}

.Alan. 发表于 2018-2-21 19:39:34

本帖最后由 .Alan. 于 2018-2-21 19:41 编辑

。。。。

.Alan. 发表于 2018-2-21 19:42:11

t3486784401 发表于 2018-2-19 16:50
这么多 float 还有 log 函数,当 AVR 是神仙啊。

单说语法只会用 int 么,这一句有没有试试 sizeof(int)== ...

只有2bype和4.。。。

.Alan. 发表于 2018-2-21 20:45:33

t3486784401 发表于 2018-2-19 16:50
这么多 float 还有 log 函数,当 AVR 是神仙啊。

单说语法只会用 int 么,这一句有没有试试 sizeof(int)== ...

我觉得是我用太多除法运算导致运行变慢

和floatLog关系不太大

wangyu_2011 发表于 2018-2-22 00:25:08

换成stm32的

.Alan. 发表于 2018-2-22 08:50:08

wangyu_2011 发表于 2018-2-22 00:25
换成stm32的

能换我早换啦,还不如上Pi更好

.Alan. 发表于 2018-2-22 09:28:38

这个失败的原因是觉得应该是LM358和LM35dt两个互不相容

我用2个一样的LM358就可以正常工作,所以我觉得和floatlog没什么太大关系

t3486784401 发表于 2018-2-22 14:24:52

.Alan. 发表于 2018-2-21 19:42
只有2bype和4.。。。

R0已经溢出,其他的自己找吧

t3486784401 发表于 2018-2-22 14:46:02

本帖最后由 t3486784401 于 2018-2-22 14:48 编辑

.Alan. 发表于 2018-2-21 20:45
我觉得是我用太多除法运算导致运行变慢

和floatLog关系不太大

我用 ICCAVR+AvrStudio 测试了下运算性能(标于每行注释),你可以大致看下:

#include "math.h"

void main(void)
{
    charv8;
    short v16;
    longv32;
    float vfl;
   
    v8= 1;          // 2T
    v8= v8+v8;      // 1T
    v8= v8*5;       // 5T/43T
    v8= v8/3;       // 173T
   
    v16= 10000;   // 3T
    v16= v16+v16;   // 2T
    v16= v16*5;   // 37T/62T
    v16= v16/3;   // 247T
   
    v32= 100000000; // 12T
    v32= v32+v32;   // 22T
    v32= v32*5;   // 179T/211T
    v32= v32/3;   // 928T
   
    vfl= 3.14159;   // 54T
    vfl= vfl+vfl;   // 348T
    vfl= vfl*5.0;   // 390T/682T
    vfl= vfl/3.0;   // 1025T
    vfl= log(vfl);// 8973T
   
    while(1);
}

注释标的是测得的机器周期数,对于 16M 主频的 Arduino 而言,1600T=1ms.
标注两个周期的,后一个是禁用了硬件乘法器的编译结果。

想减轻负担,还是得从减少浮点、MATH库着手。
页: [1]
查看完整版本: arduino 多个变量后会边卡 求帮助