2024-06-17 8:12 AM
I'm trying to make a smart irrigation using stm32f407 so, i used a Soil Moisture Sensors
i write my code but when i trying to run the problem is :Description Resource Path Location Type conflicting types for 'Read_Soil_Moisture'; have 'uint16_t(void)' {aka 'short unsigned int(void)'} main.c /Projectrrr/Core/Src line 89 C/C++ Problem
and this is my code:
so how i can solve this problem
2024-06-17 8:18 AM - edited 2024-06-17 8:18 AM
Please use this button to properly post source code:
Please post the full text of the message - copy from the 'Console' window, and paste here.
The full message should tell you both what type it was expecting, and what type it actually got - you've only posted the bit saying what it's got.
2024-06-17 8:25 AM
#include "main.h"
// Function prototypes
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM2_Init(void);
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim2;
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM2_Init();
while (1) {
uint16_t moisture_level = Read_Soil_Moisture();
if (moisture_level < 2000) { // Example threshold
Control_Servo(90); // Open valve
} else {
Control_Servo(0); // Close valve
}
HAL_Delay(1000); // Delay for 1 second
}
}
void SystemClock_Config(void) {
// System clock configuration code (auto-generated by STM32CubeMX)
}
static void MX_GPIO_Init(void) {
// GPIO initialization code (auto-generated by STM32CubeMX)
}
static void MX_ADC1_Init(void) {
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK) {
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler();
}
}
static void MX_TIM2_Init(void) {
TIM_OC_InitTypeDef sConfigOC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 83;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 20000 - 1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) {
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1500; // 1.5ms pulse width (90 degrees)
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) {
Error_Handler();
}
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
}
uint16_t Read_Soil_Moisture(void) {
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t value = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
return value;
}
void Control_Servo(uint8_t position) {
// Convert position (0-180) to pulse width (1ms to 2ms)
uint16_t pulse = (position * 1000 / 180) + 1000;
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, pulse);
}
void Error_Handler(void) {
while (1) {
// Stay here...
}
}
2024-06-17 8:26 AM
Lacking in contextual detail.
You've probably defined or used it earlier and the compiler used that.
Check .H files and any instance where you might have created a prototype or used it previously.
2024-06-17 8:28 AM
Used it on line 21 and compiler guessed, you need a prototype or definition before first usage.
uint16_t Read_Soil_Moisture(void); // prototype first, and early
2024-06-17 8:32 AM - edited 2024-06-17 8:36 AM
You didn't show the full message!
In fact, please show the full build output - that will give more info.
EDIT: This is the 'Console' window - copy from here:
As @Tesla DeLorean said, likely you are calling the Read_Soil_Moisture() function before you have declared it, so the compiler will have created a default declaration.
There should be a warning about that.
Probably, the default declaration doesn't match your actual definition - hence you get the error when the compiler reaches your actual definition.
This is a good example of why it's important to handle compiler messages in order - starting with the first one reported.
Note that this is general C stuff - nothing specific to STM32
2024-06-17 11:22 AM
In line 21 Read_Soil_Moisture() called without previous declaration. Either move this function up before main(), or add the prototype declaration after line 8.