GameMale
登陆 / 注册 搜索

USERCENTER

SEARCHSITE

搜索

查看: 625|回复: 17
收起左侧

[悬赏提问] 有没有会arduino的大神帮忙看看我们的代码出了什么问题?

 关闭 [复制链接] |关注本帖

『终点站:极地』炎龙火舌『梦旅存根』百相千面-晦诺克提斯·路西斯·伽拉姆龙血指环夜魔护符大恶魔骑兽之子岛田源氏

    400金币


    如题,今年刚开始学着用arduino解决问题,然后就发现编辑完后代码虽然不报错但是跑起来没反应,即使写了相关指令按按钮也不会执行。

    我们的目的是制作一个短距离的温度探测器,工作流程是:开机后自动进入倒计时(3秒),在此期间每按压一下按钮(button 2)就会记录一次按压数(click count),倒计时结束后根据按压次数1/2/3分别进入3个预设的模式(3种触发温度40/50/60)

    模式选定后自动进入一个循环进行的工作流程。首先探测距离,危险距离外则不报警,并重复此流程;危险距离内发出提示并开始进行温度探测;探测温度低于阈值则不报警并返回距离探测;反之报警并等待一秒,然后重复执行温度探测



    这是我们的Predefined flowchart





    这是我们的circuit diagram



    这是我们的代码:
    #include "Wire.h"
    #include <Adafruit_Sensor.h>
    #include "Adafruit_TMP006.h"
    #define TCAADDR 0x70 void tcaselect(uint8_t i) {
    if (i > 7) return;  
    Wire.beginTransmission(TCAADDR);
      Wire.write(1 << i);
    Wire.endTransmission();  }
    Adafruit_TMP006 sensor1;
    uint16_t samples = TMP006_CFG_8SAMPLE;
    // I2C address of TMP006, can be 0x40-0x47
    const int speaker = A0;      
    // The CP microcontroller pin for the speaker
    const int buttonPin1 = 4;
    const int buttonPin2 = 5;
    const int devicePin1 = A4;
    const int devicePin2 = A5;
    int temperature;
    int buttonState1 = 0;
    int lastButtonState1 = 0;
    int buttonState2 = 0;
    int lastButtonState2 = 0;
    int lastDebounceTime2 = 0;
    int debounceDelay2 = 50;
    unsigned long countdownStart = 0;
    const unsigned long countdownDuration = 3000;   // 3 seconds
    bool countdownFinished = false;
    int clickCount = 0; // standard Arduino setup()
    void setup()
    {  pinMode(buttonPin1, INPUT);
    pinMode(devicePin1, OUTPUT);  
    pinMode(buttonPin2, INPUT);  
    pinMode(devicePin2, OUTPUT);
    pinMode(speaker, OUTPUT);}
    void activatePreset1()
    {  temperature = 40;}
    void activatePreset2() {
    temperature = 50;
    }
    void activatePreset3()
    {  temperature = 60;}
    void executePresetProgram()
    {  switch (clickCount)
    {   
    case 1:      activatePreset1();      break;   
    case 2:      activatePreset2();      break;   
    case 3:      activatePreset3();      break;   
    default:      break;  }// bottom set up

        while (!Serial);  
      delay(1000);     
    Wire.begin();  
      Serial.begin(115200);   
    Serial.println("\nTCAScanner ready!");   
         for (uint8_t t=0; t<8; t++) {      tcaselect(t);   
      Serial.print("TCA Port #"); Serial.println(t);     
      for (uint8_t addr = 0; addr<=127; addr++) {   
         if (addr == TCAADDR) continue;      
       Wire.beginTransmission(addr);      
      if (!Wire.endTransmission()) {        
      Serial.print("Found I2C 0x");  Serial.println(addr,HEX);      
      }   
       }  
      }  
      Serial.println("\ndone");  
    Serial.println("TMP006 Example");
    sensor1.begin(TMP006_CFG_8SAMPLE);
      /* Initialise the 1st sensor */  tcaselect(1);  if(!sensor1.begin())  
    {    /* There was a problem detecting the HMC5883 ... check your connections */   
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");  
      while(1);  }} void loop(){ int reading2 = digitalRead(buttonPin2);
    if (reading2 != lastButtonState2) {    lastButtonState2 = reading2;  
      if (reading2 == HIGH) {      countdownStart = millis();   
    }
    }   
    if (!countdownFinished && (millis() - countdownStart >= countdownDuration))
    {    countdownFinished = true;   
    executePresetProgram();  }  
    if (!countdownFinished) {    recordButtonClicks();  
    }
    }
    void recordButtonClicks()
    {  
    int reading2 = digitalRead(buttonPin2);
    if (reading2 != lastButtonState2) {   
    lastDebounceTime2 = millis();  }  
    if ((millis() - lastDebounceTime2) > debounceDelay2)
    {   
    if (reading2 != buttonState2) {     
    buttonState2 = reading2;      
    if (buttonState2 == HIGH) {   
         clickCount++;      
    tone(speaker, 500, 50);     
    }   
    }  
    }   
    lastButtonState2 = reading2;  //bottom  float
    object_temp = sensor1.readObjTempC();
    Serial.print("Object Temperature: ");   
    Serial.print(object_temp); Serial.println("*C");  
    if(object_temp>=temperature) {
      // if reading the greater than threshold temperature    tone(A0, 500, 100);
      // output a 1000 Hz sound for a tenth of a second  }         
       delay(2000);
    // delay 1 second for every 4 samples per reading}



    附件: 您需要 登录 才可以下载或查看,没有账号?立即注册

    最佳答案

    查看完整内容

    来自Gemini (google.com) 回答分享‎Arduino温度探测器问题 (google.com) 提问格式为复制原文内容,删去图片展示内容您的代码存在以下几个问题: [*]缺少库文件: 您使用了 Adafruit_TMP006.h 库,但没有在代码中包含它。请在代码开头添加 #include 。 [*]重复定义函数: 您在代码中重复定义了 tcaselect() 函数。请删除重复的定义。 [*]未使用的变量: 您定义了 object_temp 变量,但却没有使用它。请将其删除或使用它。 [*]无限循环 ...
    回复

    使用道具 举报

     『先知灵药』英普瑞斯神圣十字章诺克提斯·路西斯·伽拉姆V (DMC5)Vergil维吉尔驯化红龙幼崽月影狼

      本帖最后由 lzy415 于 2024-3-21 06:12 编辑


      提问格式为复制原文内容,删去图片展示内容您的代码存在以下几个问题:
      • 缺少库文件: 您使用了 Adafruit_TMP006.h 库,但没有在代码中包含它。请在代码开头添加 #include <Adafruit_TMP006.h>
      • 重复定义函数: 您在代码中重复定义了 tcaselect() 函数。请删除重复的定义。
      • 未使用的变量: 您定义了 object_temp 变量,但却没有使用它。请将其删除或使用它。
      • 无限循环: 在 loop() 函数中,您使用了 while (!countdownFinished) 来控制循环。但是,countdownFinished 变量在 executePresetProgram() 函数中被设置为 true,但该函数不会立即返回。因此,循环将一直执行下去。请修改代码,确保 countdownFinished 变量在循环结束后被重置为 false
      • 按钮去抖动: 您使用了简单的去抖动方法,但效果可能不够理想。建议使用更完善的去抖动算法,例如使用 debounce() 函数。
      • 代码风格: 您的代码格式不规范,建议使用缩进和空格来提高代码的可读性。


      修复上述问题后,您的代码应该能够正常工作。以下是一些修复后的代码示例:

      1. #include <Adafruit_TMP006.h>

      2. #define TCAADDR 0x70

      3. void tcaselect(uint8_t i) {
      4.   if (i > 7) return;
      5.   Wire.beginTransmission(TCAADDR);
      6.   Wire.write(1 << i);
      7.   Wire.endTransmission();
      8. }

      9. Adafruit_TMP006 sensor1;
      10. uint16_t samples = TMP006_CFG_8SAMPLE;

      11. // I2C address of TMP006, can be 0x40-0x47
      12. const int speaker = A0;
      13. // The CP microcontroller pin for the speaker
      14. const int buttonPin1 = 4;
      15. const int buttonPin2 = 5;
      16. const int devicePin1 = A4;
      17. const int devicePin2 = A5;
      18. int temperature;
      19. int buttonState1 = 0;
      20. int lastButtonState1 = 0;
      21. int buttonState2 = 0;
      22. int lastButtonState2 = 0;
      23. int lastDebounceTime2 = 0;
      24. int debounceDelay2 = 50;
      25. unsigned long countdownStart = 0;
      26. const unsigned long countdownDuration = 3000; // 3 seconds
      27. bool countdownFinished = false;
      28. int clickCount = 0;

      29. void setup() {
      30.   pinMode(buttonPin1, INPUT);
      31.   pinMode(devicePin1, OUTPUT);
      32.   pinMode(buttonPin2, INPUT);
      33.   pinMode(devicePin2, OUTPUT);
      34.   pinMode(speaker, OUTPUT);

      35.   Serial.begin(115200);
      36.   while (!Serial);

      37.   Wire.begin();
      38.   sensor1.begin(TMP006_CFG_8SAMPLE);

      39.   tcaselect(1);
      40.   if (!sensor1.begin()) {
      41.     Serial.println("Ooops, no TMP006 detected ... Check your wiring!");
      42.     while (1);
      43.   }
      44. }

      45. void loop() {
      46.   int reading2 = digitalRead(buttonPin2);
      47.   if (reading2 != lastButtonState2) {
      48.     lastButtonState2 = reading2;
      49.     if (reading2 == HIGH) {
      50.       countdownStart = millis();
      51.     }
      52.   }

      53.   if (!countdownFinished && (millis() - countdownStart >= countdownDuration)) {
      54.     countdownFinished = true;
      55.     executePresetProgram();
      56.   }

      57.   if (!countdownFinished) {
      58.     recordButtonClicks();
      59.   }
      60. }

      61. void recordButtonClicks() {
      62.   int reading2 = digitalRead(buttonPin2);
      63.   if (reading2 != lastButtonState2) {
      64.     lastDebounceTime2 = millis();
      65.   }
      66.   if ((millis() - lastDebounceTime2) > debounceDelay2) {
      67.     if (reading2 != buttonState2) {
      68.       buttonState2 = reading2;
      69.       if (buttonState2 == HIGH) {
      70.         clickCount++;
      71.         tone(speaker, 500, 50);
      72.       }
      73.     }
      74.   }
      75.   lastButtonState2 = reading2;
      76. }

      77. void executePresetProgram() {
      78.   switch (clickCount) {
      79.     case 1:
      80.       activatePreset1();
      81.       break;
      82.     case 2:
      复制代码


      谨慎使用
      代码。






        收起(5)
      • lzy415 lzy415 :理论上会比GPT强,之前用的翻译比较多,变成部分不保真。
        回答分享界面底部的[信息来源]应该也能提供一些帮助
        2024-03-21 06:16 回复
      • 没药 没药 :吓鼠了,现在ai已经那么强了吗
        2024-03-21 10:38 回复
      • sgslrw sgslrw :不好意思,这个代码好像只出来了半截,能试着重跑一下看看有没有新东西出来吗?
        2024-03-21 17:46 回复
      • lzy415 lzy415 :回复 sgslrw :他会这个好像是生成超出限制了,这个可能需要分段去问了。你有谷歌账号吗,这个是谷歌推的ai,有谷歌账号就行的
        这个应该是比较完整的了https://g.co/gemini/share/1cbd2156302f
        2024-03-22 05:55 回复
      • sgslrw sgslrw :回复 lzy415 :一看到就试了,他说我的账号未经管理员同意没有访问权限......没事了已经搞定了,多谢啦
        2024-03-22 06:49 回复
      • 我也说一句

      回复

      使用道具 举报

       『先知灵药:真视』远古石碑龙腾世纪:审判最终幻想XVI时间变异管理局森林羊男牧羊人『不败之花』岛屿探险家图腾饼干

        本帖最后由 woyiwu 于 2024-3-22 01:30 编辑

        虽然但是,直接粘上来没有缩进的代码真的就是一坨……能不能改改缩进……
        我以我一点浅薄的c++知识猜测一下,你这里的recordButtonClicks函数的第二个和第三个if会不会有点冲突,因为你要判断两次按按钮的间隔时长,但是你电位如果改变后会先改变lastDebounceTime2,会不会导致这里时间间隔永远小于debounceDelay2……
        不会arduino,如果说错了求放过 @sgslrw
        回复

        使用道具 举报

        没有梦想的咸鱼男色时代冒险专用绳索Drover人到中年魯杰羅·弗雷迪安德森‧戴维斯安德鲁·库珀

          回复

          使用道具 举报

          『酒馆蛋煲』【圣诞限定】心心念念小雪人『伊黎丝的赞词』 迷之香蕉特饮裸体克里斯旧日支配者—克苏鲁卡利亚权杖骑士遗盔不朽之恋SCP-s-1889-第二页

            这种情况一般建议去淘宝寻找帮助,猫猫只能给出一些精神上的祝福,来自生物猫
            回复

            使用道具 举报

            『不败之花』 『先知灵药』微抖小草香喷喷的烤鸡『还乡歌』

              回复

              使用道具 举报

              牧羊人巴特‧贝克马戏团灰蛋林中之蛋红龙幼崽森林羊男猎鹰图腾山猫图腾眼镜蛇图腾香喷喷的烤鸡

                回复

                使用道具 举报

                  回复

                  使用道具 举报

                    回复

                    使用道具 举报

                    眼镜蛇图腾GM論壇勛章香喷喷的烤鸡牧羊人【一阶】棱镜驯化黑龙幼崽山猫图腾猎鹰图腾驯化红龙幼崽腐化龙蛋

                      回复

                      使用道具 举报

                      没有梦想的咸鱼实现梦想泰比里厄斯雄躯的昇格虚空之海的鲸業火死鬥追忆永浴爱河净化污秽的天照征服之王

                        回复

                        使用道具 举报

                        小小舞台『 弗霖的琴』虚空之海的鲸業火死鬥男巫之歌永浴爱河泰比里厄斯崩朽之青铜龙王岛田源氏风物长宜

                          回复

                          使用道具 举报

                          您需要登录后才可以回帖 登录 | 立即注册

                          本版积分规则

                          文字版|手机版|小黑屋|GameMale

                          GMT+8, 2024-4-27 22:55 , Processed in 0.126259 second(s), 92 queries , Redis On.

                          Copyright © 2013-2024 GameMale

                          All Rights Reserved.

                          快速回复 返回列表