How to convert array to int
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 2:32 AM - last edited on ‎2024-07-30 2:47 AM by Andrew Neil
halo!
i have a adc_value as array and i hava this for potentiometer, i want to call the adc_value as int, but i can't thingking for this now, can anyone help me?
- Labels:
-
STM32F3 Series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 2:46 AM
Your question doesn't make sense - please clarify.
@meriarajagukguk wrote:halo!
i have a adc_value as array and i hava this for potentiometer
So what does this "array" represent - a sequence of samples of the potentiometer values?
What type is the array?
@meriarajagukguk wrote:i want to call the adc_value as int,
What do you mean by that?
Surely, each element in the array can be accessed in the normal way? Cast it to an int if it isn't already an int.
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 3:07 AM
I have an ADC value that I store in uint32_t BUFFER_DAC[4];
and I want to use that value in 'int input' but I don't know how to convert/call it to be int.
Can anyone help me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 3:55 AM
Conversion from uint32_t to int is automatic. Shouldn't require anything more than using the variable name.
int x = BUFFER_DAC[0];
Buffer should probably be uint16_t, not uint32_t.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 4:01 AM
but the result for int x = BUFFER_DAC[0]; is :
../Core/Src/main.c(39): warning: array index 4 is past the end of the array (that has type 'uint32_t[4]' (aka 'unsigned int[4]')) [-Warray-bounds] why?
and for uint32_t AD_RES_BUFFER[4]; i'm a beginner and to be honestly i used that from tutorial
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 4:05 AM
@meriarajagukguk wrote:I have an ADC value that I store in uint32_t BUFFER_DAC[4];
still unclear what you mean by that.
Is that four successive samples, each 32 bits? Or is one sample 128 bits (4x32) ???
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 4:13 AM - edited ‎2024-07-30 4:42 AM
@meriarajagukguk wrote:but the result for int x = BUFFER_DAC[0]; is :
../Core/Src/main.c(39): warning: array index 4 is past the end of the array (that has type 'uint32_t[4]' (aka 'unsigned int[4]')) [-Warray-bounds] why?
Clearly, that error does not relate to the line of code you showed.
int x = BUFFER_DAC[0];
That should work perfectly fine - the array index there is 0 - not 4 !
(but, if the array is uint32_t, then you should also declare x as uint32_t).
You need to show the actual code which is producing that error.
See the tips for how to properly post source code:
Also post the full build output - using the same </> button.
@meriarajagukguk wrote:i'm a beginner and to be honestly i used that from tutorial
Please give a link to that tutorial.
It sounds like you need to take a step back, and spend some time on the basics of the C programming language
https://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-07-30 4:19 AM
@meriarajagukguk wrote:warning: array index 4 is past the end of the array (that has type 'uint32_t[4]'rial
Remember array indexes in C start from zero:
uint32_t array[4]; // Declares an array of four elements
// Because indexes start at zero,
// those elements are accessed as:
// array[0] - the first element;
// array[1] - the second element;
// array[2] - the third element;
// array[3] - the fourth & final element.
A complex system designed from scratch never works and cannot be patched up to make it work.
