2024-07-15 01:11 AM
Hello everyone,
So I am trying to take Input from the sensor and then turn on the LED_1/Sound on and another senarove is that whne I prace the GPIO_EXTI1 turn on the another LED_2. but what is hapning is when senser is not givin Input even then LED_1/Sound is on.
Bun whin i use the TWO code sepratly THe code work with no problem
1. while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (!button_pressed) {
handle_sensor_input();
}
}
// Handle sensor input
2. void handle_sensor_input(void) {
GPIO_PinState a = HAL_GPIO_ReadPin(SENSOR1_PORT, SENSOR1_PIN);
GPIO_PinState b = HAL_GPIO_ReadPin(SENSOR2_PORT, SENSOR2_PIN);
// Process the sensor state
if (a == GPIO_PIN_RESET || b == GPIO_PIN_RESET) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
HAL_Delay(100);
int randomIndex = rand() % size;
int randomNumber = numbers[randomIndex];
printf("Randomly selected number: %d\n", randomNumber);
selectRandomLED(randomNumber);
// }
} else {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
}
}
3. // EXTI Line1 External Interrupt ISR Handler CallBackFun
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == GPIO_PIN_1) { // If The INT Source Is EXTI Line1 (PB1 Pin)
button_pressed = !button_pressed; // Toggle the button press state
if (button_pressed) {
HAL_GPIO_WritePin(GPIOA, LED_1_Pin | LED_2_Pin | LED_3_Pin | BUZZER_PIN, GPIO_PIN_RESET); // Toggle The Output (LED) Pin
}
}
}
2024-07-15 02:44 AM
Hello @Kai_Satone,
Potential issues are noisy sensor readings or the button_pressed state is not properly synchronized between the interrupt handler and the main loop;
If the sensor readings are already noisy or not properly debounced, it will cause false/missing triggers
Also, use volatile for button_pressed.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.