`夏天出生的孩子,天气温度湿热,还是要在空调屋中。
功能:使用温度模块检测室内的温度,当温度太高时,使用遥控器打开空调。
在LCD上显示温度值。
没有红外模块,关键是还要遥控器的协议。不过可以还用wifi控制,但也没有协议啊。
只能先用这个模块先凑合了。
与控制器的按法如下,当温度高时舵机转20度,按下空调的开关键,由于空调会记录上次设置的温度,所以再次打开时,就会按上次的配置工作。1秒后再复位舵机:
程序如下 ,没有删除其他的模块的程序,一点点完善这个项目:
- #include
- #include
- #include
- #include "grove.hpp"
- #include
- #include
- #include "mic.hpp"
- #include
- #include
- #include time.h>
- #include "buzzer.hpp"
- #include "es08a.hpp"
- int is_running = 0;
- uint16_t buffer [128];
- upm::Microphone *sensor = NULL;
- int
- main(int argc, char **argv)
- {
- int thresh;
- int flag;
- upm::ES08A *servo = new upm::ES08A(5);
- thresholdContext ctx;
- ctx.averageReading = 0;
- ctx.runningAverage = 0;
- ctx.averagedOver = 2;
- upm::GroveTemp* temp = new upm::GroveTemp(0);
- upm::GroveLed* led = new upm::GroveLed(2);
- upm::Buzzer* sound = new upm::Buzzer(3);
- sensor = new upm::Microphone(1);
- std::cout << temp->name() << std::endl;
- char voic[8];
- char temperature[15];
- upm::Jhd1313m1 *lcd = new upm::Jhd1313m1(0, 0x3E, 0x62);
- lcd->setColor(40,150,150);//浅蓝
- lcd->setCursor(0,0);
- lcd->write("Voice:");
- lcd->setCursor(1,0);
- sound->stopSound();
- sound->stopSound();
- //temperature
- lcd->setCursor(1,0);
- lcd->write("temperature:");
- lcd->setCursor(1,0);
- for (;;) {
- int len = sensor->getSampledWindow (2, 128, buffer);
- thresh = sensor->findThreshold (&ctx, 30, buffer, len);
- int celsius = temp->value();
- int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0);
- if((ctx.averageReading>200)&&(flag==0)){
- sound->playSound(3800, 1000000);
- led->on();
- flag=1;
- }
- else if((ctx.averageReading<200)){
- flag=0;
- sound->stopSound();
- led->off();
- }
- //int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0);
- sprintf(voic,"%d",(int)ctx.averageReading);
- lcd->setCursor(0,7);
- lcd->write(voic);
- printf("%d degrees Celsius, or %d degrees Fahrenheit
- ",
- celsius, fahrenheit);
- sprintf(temperature,"%d",(int)celsius);
- lcd->setCursor(1,12);
- lcd->write(temperature);
- if(celsius>=31){
- servo->setAngle (20);
- sleep(1);
- servo->setAngle (0);
- }
- sleep(1);
- //sound->stopSound();
- //sound->stopSound();
- //led->off();
- //sleep(1);
- }
- delete temp;
- delete lcd;
- return 0;
- }
复制代码
当温度显示如下:
舵机转动后,空调就开始工作了。
`
|