gpt4 book ai didi

c - 使用 C 编程语言对 XM1000 传感器设备进行编程 需要 if 语句

转载 作者:行者123 更新时间:2023-11-30 17:01:46 25 4
gpt4 key购买 nike

我正在对传感器节点 (XM1000) 进行编程,我正在使用 Contiki 操作系统对该设备进行编程(我正在使用终端查看输出,并使用 GEdit 写入我的“C”代码。该传感器节点具有温度、光照板载湿度传感器以及 3 个 LED 灯。

下面我有两组代码。第一组代码起作用并为我提供温度、光照和湿度值的传感器读数。

第二组代码函数。它打开/关闭 LED 灯并使其在传感器节点 XM1000 上定期闪烁,计算 LED 闪烁的次数并将计数输出到控制台。

我遇到的问题是创建一个 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 语句,这样我就可以满足我上面提到的功能。

预先感谢您的任何帮助或贡献!

最佳答案

这就是我认为它会是什么样子,给予或采取一点黑客攻击。我将代码构建为单个循环,仅以 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();
}

关于c - 使用 C 编程语言对 XM1000 传感器设备进行编程 需要 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36828908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com