Getting Nil values for Sparkfun Wind Vane meter

Good Day

I would like to know the reason I am receiving 0 values the hardware is good , have tested the sensor with a multimeter and am getting the desired output the wiring is 100% and have tested each and every cable it an analog signal and am using the Nucleo F446RE Pin PA_1 but getting ) value all the time , connected it as per below

Output :

Wind Direction is 0
Wind Direction is 0.00

Code as per below :
#include “mbed.h”

AnalogIn Wind_vane(PA_1); //Analog in API for analog vane input connected to port pin PA1

Ticker Vane_Interrupt; //Ticker API intrrupt to create interrupt at set interval time

int Vane_ain_current = 0, Vane_ain_old = 0; //Analog input is read as interger value - create variable to store and process the vane integer value

float Vane_angle = 0; //The vane angle is floating point data type =- create variable

void Vane_Interrupt_handler(){ //create the code that will run when the Ticker API interrupt the CPU at the set interval

Vane_ain_current = Wind_vane; //Read the vane analog input and store its value in the variable

if(Vane_ain_current!=Vane_ain_old) //Check to see if the current vane integer value is different from previous the stored value

{switch(Vane_ain_current){ //Use a switch case structure to assign vane angle value according to the read vane integer vaue

    case 3300: Vane_angle = 0.0;    //vane integer value read is 3300, therefore write the vane angle 0.0 into the variable

    case 6570: Vane_angle = 22.5;   //repeat the cases for all vane interger values

    case 8200: Vane_angle = 45.0;

    case 891: Vane_angle = 67.5;

    case 1000: Vane_angle = 90.0;

    case 688: Vane_angle = 112.5;

    case 2200: Vane_angle = 135.0;

    case 1410: Vane_angle = 157.5;

    case 3900: Vane_angle = 180.0;


    case 3140: Vane_angle = 202.5;

    case 16000: Vane_angle = 225.0;


    case 14120: Vane_angle = 247.5;

    case 120000: Vane_angle = 270.0;

    case 42120: Vane_angle = 292.5;

    case 64900: Vane_angle = 315.0;

    case 21880: Vane_angle = 337.5;


    default: Vane_angle = 0.0;      //If read vae integer valuse is not one of the above cases, make the vane angle 0.0 as a default value

}}

}

int main (){

      Vane_Interrupt.attach(&Vane_Interrupt_handler,2000ms); //Ticker interrupt initialised to interrupt the CPU every 2 seconds

    

while(true) { 

    printf("Wind Direction is %6d\n", Vane_ain_current);     //Displays interger value read from vane analog input

    printf("Wind Direction is %4.2f\n",Vane_angle);          //Displays vane angle as assign in witch case structure in the interrupt handler

    ThisThread::sleep_for(500ms);   //Put the main task into a 500 ms sleep - it does not get executed fro 500ms.

    }

}

Hi,

I guess your switch-case usage is not correct, because you don’t have break statement for case blockes. Please refer this:

Good Day

I tried changing switch-case but still same result please find below

Code below :slight_smile:

#include “mbed.h”

AnalogIn Wind_vane(PA_1); //Analog in API for analog vane input connected to port pin PA1

Ticker Vane_Interrupt; //Ticker API intrrupt to create interrupt at set interval time

int Vane_ain_current = 0, Vane_ain_old = 0; //Analog input is read as interger value - create variable to store and process the vane integer value

float Vane_angle = 0; //The vane angle is floating point data type =- create variable

void Vane_Interrupt_handler(){ //create the code that will run when the Ticker API interrupt the CPU at the set interval

Vane_ain_current = Wind_vane; //Read the vane analog input and store its value in the variable

if(Vane_ain_current!=Vane_ain_old) //Check to see if the current vane integer value is different from previous the stored value

{switch(Vane_ain_current){ //Use a switch case structure to assign vane angle value according to the read vane integer vaue

    case 3300: Vane_angle = 0.0;    //vane integer value read is 3300, therefore write the vane angle 0.0 into the variable
     printf("Vane_angle = 0.0\n");
    break; 

    case 6570: Vane_angle = 22.5;   //repeat the cases for all vane interger values
     printf("Vane_angle = 22.5\n");     
    break;

    case 8200: Vane_angle = 45.0;
    printf("Vane_angle = 45.0\n");
    break;

    case 891: Vane_angle = 67.5;
     printf("Vane_angle = 67.5\n");       
    break;

    case 1000: Vane_angle = 90.0;
     printf("Vane_angle = 90.0\n");         
    break;

    case 688: Vane_angle = 112.5;
     printf("Vane_angle = 112.5\n");          
    break;

    case 2200: Vane_angle = 135.0;
      printf("Vane_angle = 135\n");         
    break;

    case 1410: Vane_angle = 157.5;
    break;

    case 3900: Vane_angle = 180.0;
     printf("Vane_angle = 180.0\n");          
    break;

    case 3140: Vane_angle = 202.5;
      printf("Vane_angle = 202.5\n");         
    break;

    case 16000: Vane_angle = 225.0;
      printf("Vane_angle = 225.0\n");         
    break;

    case 14120: Vane_angle = 247.5;
     printf("Vane_angle = 247.5\n");          
    break;
    case 120000: Vane_angle = 270.0;
      printf("Vane_angle = 270.0\n");         
    break;

    case 42120: Vane_angle = 292.5;
     printf("Vane_angle = 292.5\n");          
    break;

    case 64900: Vane_angle = 315.0;
      printf("Vane_angle = 315.0\n");         
    break;

    case 21880: Vane_angle = 337.5;
      printf("Vane_angle = 337.5\n");         
    break;

    default: Vane_angle = 0.0;      //If read vae integer valuse is not one of the above cases, make the vane angle 0.0 as a default value
      printf("Vane_angle = 0.0\n"); 

}}

}

int main (){

      Vane_Interrupt.attach(&Vane_Interrupt_handler,2000ms); //Ticker interrupt initialised to interrupt the CPU every 2 seconds

    

while(true) { 

    printf("Wind Direction is %6d\n", Vane_ain_current);     //Displays interger value read from vane analog input

    printf("Wind Direction is %4.2f\n",Vane_angle);          //Displays vane angle as assign in witch case structure in the interrupt handler

    ThisThread::sleep_for(500ms);   //Put the main task into a 500 ms sleep - it does not get executed fro 500ms.

    }
    }

Hi,

Please use ``` for code encloser.
In you code, you are coping a value from AnalogIn instance to int variable and this may not work expectedly.

Vane_ain_current = Wind_vane; //Read the vane analog input and store its value in the variable

Please see API document.
https://os.mbed.com/docs/mbed-os/v6.15/mbed-os-api-doxy/classmbed_1_1_analog_in.html

You can probably use read_u16() methoud.

Vane_ain_current = Wind_vane.read_u16(); 

Also, you can not use printf in the ISR. Is the Vane_Interrupt_handler() function running? (calling by every 2 sec?)
https://os.mbed.com/docs/mbed-os/v6.15/apis/ticker.html

Hi

I tried it still getting the same result :frowning:

Hi,

The read_u16 method of the AnalogIn class also doesn’t seem to be ISR safe. I run your code on the NUCLEO_F446RE target, I got exception below:

Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0


++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x20000568, Not allowed in ISR context
Location: 0x80093C1
Error Value: 0x20000568
Current Thread: main Id: 0x20001E90 Entry: 0x8006869 StackSize: 0x1000 StackMem: 0x200008F8 SP: 0x2001FF24 
For more info, visit: https://mbed.com/s/error?error=0x80010133&tgt=NUCLEO_F446RE
-- MbedOS Error Info --

To avoid this, you are able to use EventQueue API for time periodic event as it is Thread and ISR safe. Your main function can be someting like below:

int main () {
    EventQueue queue;
    queue.call_every(2000ms, Vane_Interrupt_handler);

    while(true) { 
        printf("Wind Direction is %6d\n", Vane_ain_current);     //Displays interger value read from vane analog input
        printf("Wind Direction is %4.2f\n",Vane_angle);          //Displays vane angle as assign in witch case structure in the interrupt handler
        queue.dispatch_once();
        ThisThread::sleep_for(500ms);   //Put the main task into a 500 ms sleep - it does not get executed fro 500ms.
        
    }
}

And then, I got a result below.

Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Wind Direction is      0
Wind Direction is 0.00
Vane_angle = 0.0
Wind Direction is   8946
Wind Direction is 0.00
Wind Direction is   8946
Wind Direction is 0.00
Wind Direction is   8946
Wind Direction is 0.00
Wind Direction is   8946
Wind Direction is 0.00
Vane_angle = 0.0
Wind Direction is   6865
Wind Direction is 0.00
Wind Direction is   6865
Wind Direction is 0.00

Please note that I don’t connect anything for the PA_1 port, so the input value is probably just an analog noise. The value is not much any cases in the switch-case code, so the Vane_ain_current set always 0. If you think this is a problem, you may check your switch-case logic (because switch-case method doesn’t support range handling) and specification of the Sparkfun wind vane meter.

Hi

I tried it getting the below %4.2F output error

Please see:

Hi

Ok I sorted the output out but am still getting 0 values ,

code currently running

#include “mbed.h”

AnalogIn Wind_vane(PA_1); //Analog in API for analog vane input connected to port pin PA1

Ticker Vane_Interrupt; //Ticker API intrrupt to create interrupt at set interval time

int Vane_ain_current = 0, Vane_ain_old = 0; //Analog input is read as interger value - create variable to store and process the vane integer value

float Vane_angle = 0; //The vane angle is floating point data type =- create variable

void Vane_Interrupt_handler(){ //create the code that will run when the Ticker API interrupt the CPU at the set interval

Vane_ain_current = Wind_vane; //Read the vane analog input and store its value in the variable

if(Vane_ain_current!=Vane_ain_old) //Check to see if the current vane integer value is different from previous the stored value

{switch(Vane_ain_current){ //Use a switch case structure to assign vane angle value according to the read vane integer vaue

    case 3300: Vane_angle = 0.0;    //vane integer value read is 3300, therefore write the vane angle 0.0 into the variable
     printf("Vane_angle = 0.0\n");
    break; 

    case 6570: Vane_angle = 22.5;   //repeat the cases for all vane interger values
     printf("Vane_angle = 22.5\n");     
    break;

    case 8200: Vane_angle = 45.0;
    printf("Vane_angle = 45.0\n");
    break;

    case 891: Vane_angle = 67.5;
     printf("Vane_angle = 67.5\n");       
    break;

    case 1000: Vane_angle = 90.0;
     printf("Vane_angle = 90.0\n");         
    break;

    case 688: Vane_angle = 112.5;
     printf("Vane_angle = 112.5\n");          
    break;

    case 2200: Vane_angle = 135.0;
      printf("Vane_angle = 135\n");         
    break;

    case 1410: Vane_angle = 157.5;
    break;

    case 3900: Vane_angle = 180.0;
     printf("Vane_angle = 180.0\n");          
    break;

    case 3140: Vane_angle = 202.5;
      printf("Vane_angle = 202.5\n");         
    break;

    case 16000: Vane_angle = 225.0;
      printf("Vane_angle = 225.0\n");         
    break;

    case 14120: Vane_angle = 247.5;
     printf("Vane_angle = 247.5\n");          
    break;
    case 120000: Vane_angle = 270.0;
      printf("Vane_angle = 270.0\n");         
    break;

    case 42120: Vane_angle = 292.5;
     printf("Vane_angle = 292.5\n");          
    break;

    case 64900: Vane_angle = 315.0;
      printf("Vane_angle = 315.0\n");         
    break;

    case 21880: Vane_angle = 337.5;
      printf("Vane_angle = 337.5\n");         
    break;

    default: Vane_angle = 0.0;      //If read vae integer valuse is not one of the above cases, make the vane angle 0.0 as a default value
     printf("Vane_angle = 0.0\n"); 

}}

}

int main (){

EventQueue queue;
queue.call_every(2000ms, Vane_Interrupt_handler);

    

while(true) { 

    printf("Wind Direction is %6d\n", Vane_ain_current);     //Displays interger value read from vane analog input
    printf("Wind Direction is %4.2f\n",Vane_angle);          //Displays vane angle as assign in witch case structure in the interrupt handler
    queue.dispatch_once();
    ThisThread::sleep_for(500ms);   //Put the main task into a 500 ms sleep - it does not get executed fro 500ms.

    }
    }

Hi,

Again, you need to use read_u16() meathod to get int type value fromAnalogIn instance.

Vane_ain_current = Wind_vane.read_u16();

I’ve got this.

Ah thanks so much last question what command do I use to divide the answer by 1000 because I need it in km/h thanks

Hi,

You can simply use divide operator e.g.

a = a / 1000;

Thanks