简体   繁体   English

MBed 程序卡住了

[英]MBed program gets stuck

I have written some code with the help of Mbed framework, which is either supposed to take user input and then display sensor values or display the value after 15mins.我在 Mbed 框架的帮助下编写了一些代码,该代码要么接受用户输入然后显示传感器值,要么在 15 分钟后显示该值。 When I try to execute this code, it is getting stuck at line 21 (display.printf("Inside loop\n");).当我尝试执行这段代码时,它卡在了第 21 行 (display.printf("Inside loop\n");)。

I am not able to understand why is it so and what is the fix for this problem so that the switch block gets executed.我不明白为什么会这样,以及解决这个问题的方法是什么,以便执行 switch 块。 How to I solve this?我该如何解决这个问题? FYI, although not important, the microcontroller I am using is STM32 bluepill (STM32F103C8T6).仅供参考,虽然不重要,但我使用的微控制器是 STM32 bluepill (STM32F103C8T6)。

#include "mbed.h"
#include "Sensor_input.h"
#include "Ticker.h"
#include "Dht11.h"

//#include "USBSerial.h"
Serial display(PA_2, PA_3, 9600);
char* a;
Dht11 DhtSensor(PA_4);
Ticker t;
Sensor_input Soil(PB_7, PB_6, 8);
float *SensorData;
void getSensorData();
int main ( void ){

    uint8_t choice = 0;

    display.printf("Enter 1 or 2:\n1.Greenhouse stats\n2.Return Control to System");
    choice = display.putc(display.getc());
    while(1){
        display.printf("Inside loop\n");
        wait_ms(15000);
        switch(choice)
        {
            
            case 1:
                display.printf("Inside case 1");
                a = Soil.readTemp();
                display.printf("Temperature: %f\n",DhtSensor.getCelsius());
                display.printf("Humidity: %f\n",DhtSensor.getHumidity());
                display.printf("Soil water content: %c\n ",*a);
                break;
            case 2:
                /*<GreenHouse object>*/
                /*Might have to proceed with timer*/
                display.printf("Inside case 2");
                t.attach(&getSensorData,4500);
                display.printf("Temperature: %f\n",a[0]);
                display.printf("Humidity: %f\n",a[1]);
                display.printf("Soil water content: %c\n ",a[2]);
                break;
            default:
                break;
            
        }
    }
}
void getSensorData(){
    static float a[3];
    a[0]=DhtSensor.getCelsius();
    a[1]=DhtSensor.getHumidity();
    a[2]=(int)Soil.readTemp();   

}

Your switch statement is probably being executed, but always in the 'default' case.您的 switch 语句可能正在执行,但始终处于“默认”情况。 You can test this out by putting a print statement in the default.您可以通过将打印语句放入默认值来对此进行测试。

When you request a char from the display, it will return the input as an ASCII-character.当您从显示器请求一个字符时,它将以 ASCII 字符的形式返回输入。 This means, if you enter '1' on the display, it will give you (as the ASCII table says) 0x31 (decimal 49) and not the value of 1. So you have to change your case to "case '1':" or "case 0x31:" and the equivalent for the second case.这意味着,如果您在显示屏上输入“1”,它会给您(如 ASCII 表所示)0x31(十进制 49)而不是 1 的值。因此您必须将大小写更改为“case '1': " 或 "case 0x31:" 以及第二种情况的等价物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM