A little help with my first ever MySensors (and Arduido) sketch: Rotary Angle Sensor (knob) + Nano + esp8266 gateway



  • UPD: I forgot to paste the important part, this:

    #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
    

    I want to show angular value (0-300) upstream to Home Assistant as a sensor, and lockstep to it from there to control home assistant services - volume control, scene selector, media seek, etc.
    Mysensors 2.11 in arduino, gateway is still 2.0. This is the sensor, not the same as encoder evidently:
    http://wiki.seeed.cc/Grove-Rotary_Angle_Sensor/
    and the code. Thanks!

    #include <SPI.h>
    #include <GroveEncoder.h>
    #include <MyConfig.h>
    #include <MySensors.h>
    #define ROTARY_ANGLE_SENSOR A0
    
    #define ADC_REF 5//reference voltage of ADC is 5v.If the Vcc switch on the seeeduino
                     //board switches to 3V3, the ADC_REF should be 3.3
    #define VCC 5//VCC of the grove interface is normally 5v
    #define FULL_ANGLE 300//full value of the rotary angle is 300 degrees
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    #define SN "RotaryAngleSensor"
    #define SV "1.0"
    #define CHILD_ID
    unsigned long SLEEP_TIME = 100
    
    Rotary Angle Sensor;
    
    MyMessage msg(CHILD_ID, V_VAR1);
    int lastDegrees;
    
    void setup() 
    {
        Serial.begin(9600);
        pinsInit();
        sendSketchInfo(SN, SV);
        present(CHILD_ID, S_CUSTOM);
    }
    
    void loop() 
    {
        int degrees;
        degrees = getDegree();
        Serial.println(degrees);
        if (degrees != lastDegrees) {
            send(msg.set(degrees));
            lastDegrees = degrees;
        }
        
        sleep(SLEEP_TIME);
    }
    
    void pinsInit()
    {
        pinMode(ROTARY_ANGLE_SENSOR, INPUT);
    }
    
    /************************************************************************/
    /*Function: Get the angle between the mark and the starting position    */
    /*Parameter:-void                                                       */
    /*Return:   -int,the range of degrees is 0~300                          */
    int getDegree()
    {
        int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
        float voltage;
        voltage = (float)sensor_value*ADC_REF/1023;
        float degrees = (voltage*FULL_ANGLE)/VCC;
        return degrees;
    }```

  • Mod

    Welcome to the MySensors community @vadimbz

    Remove #include <MyConfig.h> and move #include <MySensors.h> to after the lines starting with #define



  • New error. Also is it normal that IDE recognizes nano board, but not its port?

    WARNING: library GroveEncoder claims to run on [arc32] architecture(s) and may be incompatible with your current board which runs on [avr] architecture(s).
    encoderTest:22: error: 'Rotary' does not name a type
    
     Rotary Angle Sensor;
    
     ^
    
    encoderTest:24: error: expected primary-expression before ',' token
    
     MyMessage msg(CHILD_ID, V_VAR1);
    
                           ^
    
    C:\Users\Vadim\Documents\Arduino\encoderTest\encoderTest.ino: In function 'void setup()':
    
    encoderTest:32: error: expected primary-expression before ',' token
    
         present(CHILD_ID, S_CUSTOM);
    
                         ^
    
    exit status 1
    'Rotary' does not name a type
    
    

  • Mod

    @vadimbz seems like the GroveEncoder library doesn't support avr (which is what Arduino Nano and Pro Mini uses). You'll need to find another library.



  • It was never used in fact, and it wasn't used in a reference sketch either. removing it killed only the top error message.


  • Mod

    @vadimbz could you post your current sketch?



  • Rotary... line before mymessage is causing problems. I think I misunderstood its meaning

    #include <SPI.h>
    #define ROTARY_ANGLE_SENSOR A0
    
    #define ADC_REF 5//reference voltage of ADC is 5v.If the Vcc switch on the seeeduino
                     //board switches to 3V3, the ADC_REF should be 3.3
    #define VCC 5//VCC of the grove interface is normally 5v
    #define FULL_ANGLE 300//full value of the rotary angle is 300 degrees
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    #define SN "RotaryAngleSensor"
    #define SV "1.0"
    #define CHILD_ID
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 100;
    
    Rotary Angle Sensor;
    
    MyMessage msg(CHILD_ID, V_VAR1);
    int lastDegrees;
    
    void setup() 
    {
        Serial.begin(9600);
        pinsInit();
        sendSketchInfo(SN, SV);
        present(CHILD_ID, S_CUSTOM);
    }
    
    void loop() 
    {
        int degrees;
        degrees = getDegree();
        Serial.println(degrees);
        if (degrees != lastDegrees) {
            send(msg.set(degrees));
            lastDegrees = degrees;
        }
        
        sleep(SLEEP_TIME);
    }
    
    void pinsInit()
    {
        pinMode(ROTARY_ANGLE_SENSOR, INPUT);
    }
    
    /************************************************************************/
    /*Function: Get the angle between the mark and the starting position    */
    /*Parameter:-void                                                       */
    /*Return:   -int,the range of degrees is 0~300                          */
    int getDegree()
    {
        int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
        float voltage;
        voltage = (float)sensor_value*ADC_REF/1023;
        float degrees = (voltage*FULL_ANGLE)/VCC;
        return degrees;
    }```

  • Mod

    @vadimbz just remove it?



  • Cascading errors (

    It seems that I needencoderTest:21: error: expected primary-expression before ',' token
    
     MyMessage msg(CHILD_ID, V_VAR1);
    
                           ^
    
    C:\Users\Vadim\Documents\Arduino\encoderTest\encoderTest.ino: In function 'void setup()':
    
    encoderTest:29: error: expected primary-expression before ',' token
    
         present(CHILD_ID, S_CUSTOM);
    
                         ^
    
    exit status 1
    expected primary-expression before ',' token
    
    
    


  • Sketch notations were updated between 2.0 and 2.1.1, weren't they?


  • Mod

    @vadimbz change

    #define CHILD_ID
    

    to something like

    #define CHILD_ID 1
    


  • YES, that's it, compiled and flashed... at which point the board started acting out, but I can bounce from here J
    Thanks!
    While we're still here, is there a way to connect 3-pin sensors (with one signal pin) to digisparks along with radio? 3-pin radio connection scheme is not an option at the moment unfortunately...
    ...and I feel like an idiot with a handful of those -)


  • Mod

    @vadimbz could you explain what a 3-pin sensor is? Many of the MySensors examples use sensors with multiple pins so that shouldn't be a problem.



  • I nwas about talking about digispark (attiny85) board that would be a good match for rolling out mysensors if only had a couple more pins:

    0_1498271387766_s-l300.jpg

    I was asking if it's possible to use them after all one way or another, at least for basic sensors with 3 pins (GND, Vcc, Signal) from which we need only Signal.
    I also mentioned this as known, but unwated solution:
    https://www.hackster.io/arjun/nrf24l01-with-attiny85-3-pins-74a1f2


Log in to reply
 

Suggested Topics

  • 8
  • 3
  • 90
  • 1
  • 2
  • 44

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts