analogRead only works in Arduino IDE[solved]



  • Arduino Nano, Arduino IDE 1.6.8.
    I am reading 4 analog inputs, everything works fine when I have the Node powered to my PC and running AIDE with Monitor opened , if I close the Monitor it goes into a special mode waiting for programming(this is normal) but when I power the Node from the wall(USB) it doesn't send Child data. I see the Node is registering in Domoticz but no Child data.

    If i remove the delay(5000) or gw.wait(5000), just before sending Child data, it doesn't work in Arduino IDE either, very strange for me.

    #include <MySensor.h>  
    #include <SPI.h>
    
    #define CHILD_ID_MQ2 0
    #define CHILD_ID_MQ3 1
    #define CHILD_ID_MQ7 2
    #define CHILD_ID_MQ135 3
    
    unsigned long SLEEP_TIME = 60000;
    
    const int MQ2 = A0; //GAS sensor output pin to Arduino analog A0 pin
    const int MQ3 = A1; //GAS sensor output pin to Arduino analog A0 pin
    const int MQ7 = A2; //GAS sensor output pin to Arduino analog A0 pin
    const int MQ135 = A3; //GAS sensor output pin to Arduino analog A0 pin
    
    int mq2;
    int mq3;
    int mq7;
    int mq135;
    
    MySensor gw;
    MyMessage MQ2Msg(CHILD_ID_MQ2, V_LEVEL);
    MyMessage MQ3Msg(CHILD_ID_MQ3, V_LEVEL);
    MyMessage MQ7Msg(CHILD_ID_MQ7, V_LEVEL);
    MyMessage MQ135Msg(CHILD_ID_MQ135, V_LEVEL);
    
    void setup()
    {
      gw.begin();
    
      gw.sendSketchInfo("Multi MQ Sensors", "1.0");
    
      gw.present(CHILD_ID_MQ2, S_AIR_QUALITY);
      gw.present(CHILD_ID_MQ3, S_AIR_QUALITY);
      gw.present(CHILD_ID_MQ7, S_AIR_QUALITY);
      gw.present(CHILD_ID_MQ135, S_AIR_QUALITY);
      
      Serial.begin(115200); //Initialize serial port - 9600 bps
      
    }
    
    void loop()
    {
      mq2 = analogRead(MQ2);
      //gw.wait(5000);
      mq3 = analogRead(MQ3);
      //gw.wait(5000);
      mq7 = analogRead(MQ7);
      //gw.wait(5000);
      mq135 = analogRead(MQ135);
      delay(5000);
      gw.send(MQ2Msg.set(mq2));
      gw.send(MQ3Msg.set(mq3));
      gw.send(MQ7Msg.set(mq7));
      gw.send(MQ135Msg.set(mq135));
      gw.sleep(SLEEP_TIME);
    }
    

    I had some other strange errors, sometimes when I rebooted I could send to GW.

    After I changed the Nano to Pro Mini it works all the time and every where. I think I got a damaged Nano, even if it was working perfect for month as another Node.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts