Flex Sensor

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello.
I am currently working on interfacing of flex sensor with the PSOC 4200 but I can't get the analog values(float) to print on the terminal.

   

Here's the code:

   

#include <project.h>
#include <stdio.h>
int main()
{

   

float R_DIV = 47500.0; //47K Ohm approx
float VCC=5.0;
float STRAIGHT_RESISTANCE = 37300.0;
float BEND_RESISTANCE = 90000.0;

   

uint8 flexADC;
float32 flexV, flexR;
char str[10];
float angle=0.0;

   

UART_Start();
ADC_Start();

   

CyGlobalIntEnable;

   

for(;;)
{
ADC_StartConvert();
ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
flexADC = ADC_GetResult16(0);
flexV = ADC_CountsTo_Volts(0,flexADC);
flexR = R_DIV * (VCC / flexV - 1.0); 
UART_PutString("Resistance in Ohms");
sprintf(str,"%.2f  ",flexR);
UART_PutString("%s\n",str);
angle= (flexR-STRAIGHT_RESISTANCE)*(90.0-0.0)/(BEND_RESISTANCE-STRAIGHT_RESISTANCE)+0.0;
UART_PutString("Bend angle in degrees");
sprintf(str,"%.2f  ",angle);
UART_PutString("%s\n",str);
CyDelay(2000);

   

}

   


}

0 Likes
7 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum.

   

Printing floats needs some settings:

   
        
  • Set "use newlib nano" to true (Project butld settings -> linker)
  •     
  • Set "use newlib nano float formatting" to true (Project butld settings -> linker)
  •     
  • Set "Heap Size" to 0x0200 (System view)
  •    
   

 

   

That should do the job

   

Bob

0 Likes
Anonymous
Not applicable

I still can't get the float values on the terminal. What is the API to be used to print float?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your program seems ok so far. Can you please post your complete project or a shortened version that shows the error so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

Here is your program and it is working. I don't have the component you where using for the Flex so I just stuck some default items in the program for resistance and bend angle. The issue was in Project settings it was not working after Resistance in ohms print out.  Now it is working you will need to take out the default values.

   

0 Likes
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

As you didn't send your program I pasted your information in to a PSoc Ble device CY8C4247LQI-BL483 I picked this as your post was in IOT. If that is not the correct device you can change it to your device. 

0 Likes
Anonymous
Not applicable

Hello.
Thank you Bob for helping me yesterday. I was able to configure my flex sensor.

   

Can you assist me how I can use the map function in my program

   

like this: 
float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE, 0, 90.0);

   

instead of
/* angle= (flexR-STRAIGHT_RESISTANCE)*(90.0-0.0)/(BEND_RESISTANCE-STRAIGHT_RESISTANCE)+0.0; */

0 Likes
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

Here is how you use MAP

   

0 Likes