我正在编程一个传感器微尘(XM1000),我正在使用Contiki操作系统来编程这个设备(我正在使用终端来查看输出,使用GEdit来编写我的“C”代码。这个传感器微尘有一个温度,光和湿度传感器以及3个LED灯。
下面我有两组代码。第一组代码的作用是给出温度、光和湿度的传感器读数。
第二组代码函数。它会打开/关闭传感器节点XM1000上的发光二极管指示灯并使其有规律地闪烁,它会计算发光二极管闪烁的次数,并将计数输出到控制台。
我遇到的问题是创建一个if语句来满足以下条件,我正在努力将这两个代码组合在一起。所以这就是我想要实现的:
·如果温度超过26度,则打开LED灯1 5秒,否则,如果温度等于或低于26度,则关闭LED灯1。
·如果湿度超过40%,则打开LED灯2 5秒,否则,如果湿度等于或低于40%,则关闭LED灯2。
·如果光强度超过510纳米,则打开LED灯3 5秒,否则,如果光强度等于或小于510纳米,则关闭LED灯3。
第一组代码:它测量温度、光照和湿度,并在终端窗口上输出结果。
#include "contiki.h" //Contiki Header File
#include "dev/light-sensor.h" //Light Sensor Header File
#include "dev/sht11-sensor.h" //Temperature and Humidity Header File
#include <stdio.h> /* for printf() */ // standard input/output library needed to write to the standard output
static struct etimer timer; //Process Requires a Timer
int light=0, temp=0, humid=0;
//To Start
/*___________________________________________________*/
PROCESS(sensor_reading_process, "Sensor Reading Process");
AUTOSTART_PROCESSES(&sensor_reading_process);
/*___________________________________________________*/
//PROCESS BEGINS
PROCESS_THREAD(sensor_reading_process, ev, data)
{
PROCESS_BEGIN();
SENSORS_ACTIVATE(light_sensor); //Activate the Light Sensor
SENSORS_ACTIVATE(sht11_sensor);//Activate Temp/Humidity Sensor
etimer_set(&timer, CLOCK_CONF_SECOND);//Configuring Timer 1SEC
while(1) { //Start of While Loop
PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER);//Wait4Time
// This is how we get the Sensor Values for light, temp, hum
light = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
temp = sht11_sensor.value(SHT11_SENSOR_TEMP);
humid = sht11_sensor.value(SHT11_SENSOR_HUMIDITY);
printf("Light=%d, Temp=%d, Humid=%d\n", light, temp, humid);
//Above Line if Print Plus Values
etimer_reset(&timer); //Reset the Timer
}
PROCESS_END(); //End of Process
}第二个代码:
#include "contiki.h"
#include "leds.h" // LED HEADER FILE
#include <stdio.h> /* for printf() */
static struct etimer timer;
/*____________________________________________________*/
PROCESS(led_blinking_process, "LED Blinking Process");
PROCESS(LED_process, "LED process");
AUTOSTART_PROCESSES(&LED_process);
/*____________________________________________________*/
PROCESS_THREAD(LED_process, ev, data)
{
static int count = 0;
PROCESS_BEGIN();
etimer_set(&timer, CLOCK_CONF_SECOND/2); // 0.5S timer
leds_init(); // initialise the LEDs
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER); // wait for timer event
count++; // count the blinking times
process_start(&led_blinking_process, NULL); // to blink the BLUE Led
printf("Count: %d\n", count); // output the counter number to console
etimer_reset(&timer); // reset the timer
}
PROCESS_END();
}
CODE FOR LED LIGHTS
/*____________________________________________________*/
PROCESS_THREAD(led_blinking_process, ev, data)
{
PROCESS_BEGIN();
leds_toggle(LEDS_BLUE); // Blinking the Blue LED
PROCESS_END();
}请注意,当我运行它们时,这两个代码都可以工作,我只是试图将它们组合在一起,使用if语句,这样我就可以满足我上面提到的功能。
提前感谢您的帮助或贡献!
发布于 2016-04-25 05:53:04
这是我认为它会是什么样子,或多或少一些黑客。我已经将代码组织成一个单循环,仅以5秒的间隔轮询所有三个传感器。因此,每隔5秒,所有三个灯的开/关状态可能会改变。我认为对于给草坪浇水来说,你并不真的需要瞬间的精确度来知道什么时候变热了,什么时候黑了,或者其他什么。
根据您在OP的注释部分中的回复,可能需要对传感器读数的返回值进行一些计算。我认为在编译时进行计算可能更容易,所以代码是有偏差的。
#include <stdio.h> /* for printf() */
#include "contiki.h"
#include "leds.h"
#include "dev/light-sensor.h"
#include "dev/sht11-sensor.h"
PROCESS(Led_management_process, "LED Management Process");
AUTOSTART_PROCESSES(&Led_management_process);
static struct etimer Timer;
/* Per the online docs at http://www.advanticsys.com/wiki/index.php?title=TestCM5000
T = -39.6 + 0.01 × SO(T)
So, solving for SOt gives:
*/
#define degreesC(n) (n)
#define TEMP_D1 degreesC(-39.6)
#define TEMP_D2 degreesC(0.01)
#define TEMP_THRESHOLD(tempC) (int)(((((double)(tempC))-(TEMP_D1))/(TEMP_D2)))
#define TARGET_TEMP_READING TEMP_THRESHOLD(degreesC(26))
#define LIGHT_LED LEDS_GREEN
#define HUM_LED LEDS_BLUE
#define TEMP_LED LEDS_RED
PROCESS_THREAD(Led_management_process, ev, data)
{
static int humidity_led_on = 0;
static int light_led_on = 0;
static int temp_led_on = 0;
PROCESS_BEGIN();
SENSORS_ACTIVATE(light_sensor);
SENSORS_ACTIVATE(sht11_sensor);
leds_init();
unsigned int cycle = 0;
while (1) {
printf("%8u:", cycle);
/* This part is questionable, since I don't know if the
sensors are guaranteed to return ints (vs. floats or
something) and I don't know what units they use. */
int temp = sht11_sensor.value(SHT11_SENSOR_TEMP);
if (temp > TARGET_TEMP_READING) {
printf(" TEMP warm- ");
if (!temp_led_on) {
printf("toggle LED");
temp_led_on = 1;
leds_toggle(TEMP_LED);
}
else {
printf("LED is ok ");
}
}
else {
printf(" TEMP cold- ");
if (temp_led_on) {
printf("toggle LED");
temp_led_on = 0;
leds_toggle(TEMP_LED);
}
else {
printf("LED is ok ");
}
}
/* Code for humidity */
/* Code for light */
printf("\n");
/* Sleep 5 seconds */
etimer_set(&Timer, CLOCK_CONF_SECOND * 5);
PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER);
etimer_reset(&Timer);
++cycle;
}
PROCESS_END();
} https://stackoverflow.com/questions/36828908
复制相似问题