Grove Rotary Angle Sensor - Overloaded Function


#1

Hi,

I’m testing out the Grove Rotary Sensor Angle that comes in the Grove kit. I tried testing out this example which is linked off of part.cl/grove and the example is throwing the following error:

call of overloaded ‘map(float&, int, int, int, int)’ is ambiguous

//code
/macro definitions of Rotary angle sensor and LED pin/

#define ROTARY_ANGLE_SENSOR A0
#define LED D4  //the Grove - LED is connected D4 pin
#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 GROVE_VCC 5 //VCC of the grove interface is normally 5v
#define FULL_ANGLE 300 //full value of the rotary angle is 300 degrees

void setup()
{
    Serial.begin(9600);
    pinMode(ROTARY_ANGLE_SENSOR, INPUT);
    pinMode(LED,OUTPUT);   
}

void loop()
{   
    float voltage;
    int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
    voltage = (float)sensor_value*ADC_REF/1023;
    float degrees = (voltage*FULL_ANGLE)/GROVE_VCC;
    Serial.println("The angle between the mark and the starting position:");
    Serial.println(degrees);

    int brightness;
    brightness = map(degrees, 0, FULL_ANGLE, 0, 255);
    analogWrite(LED,brightness);
    delay(500);
}

Any ideas?

Thanks.


#2

The error message provides all you need to know :wink:

There is no version of map() that takes one float and four ints.
There are onle versions that either take all floats or all ints.

While casting int to float or float to int can be done implicitly, the compiler can’t decide which way it should do it, so you need to help it decide.

BTW

Particle ADCs are of 12bit resolution (0…4095) unlike Arduino’s 10bit (0…1023).
And ADC_REF is 3.3V not 5 - make sure to never apply more than 3.3V to an ADC pin.


#3

Thanks @ScruffR, I ws able to get it to work by changing the ADC_REF value to 3.3. I assumed the GROVE_VCC value also needed to be change. My function to get the Degree now looks like so:

int getDegree()
    {
        int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
        float voltage;
        voltage = (float)sensor_value*ADC_REF/4095;
        float degrees = (voltage*FULL_ANGLE)/GROVE_VCC;
        return degrees;
    }

Question, when I Serial.print() either degrees or sensor_value I get a fluctuating number despite the input sensor being in a static position. This makes it difficult to properly detect for a change event for the rotation of the input and/or degrees. I come from a web programming background so I am used to having listeners for things like onRotateStart onRotateEnd where I could capture the start degree and end degree as variables. Would you have a recommendation on how to achieve this?


#4

We can’t really see how big your fluctuations are, but limiting the display precision would be an initial step.
The next thing would be to check the delta between the last displayed value and the current measurement and only update when a certain threshold is exceeded - optionally adding a update time interval.

ADCs are typically not producing static results even with static input values due to many factors.

Even for that you’d need a reliable “data” source to detect the start and end of the action, but as said above you’d first need to compensate for the ADC noise.
You need to be aware that your are now much closer to the hardware and hence it’s quirks and limitations.