Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
1
Indexable
Never
buffer
int speed;
char inputBuffer[10];
int inputIndex=0;
uint8_t ch;

 while (1)
  {
    /* USER CODE END WHILE */
	  if(HAL_UART_Receive(&huart3,&ch,sizeof(ch),100) == HAL_OK)
	   {
	       if (ch == '\r')  // Check if the received character is carriage return (Enter key)
	       {
	          inputBuffer[inputIndex] = '\0'; // Null-terminate the string
	          int inputNumber = atoi(inputBuffer); // Convert string to integer


	          	if(inputNumber > 80){
	          		HAL_UART_Transmit(&huart3, tx2, sizeof(tx2),HAL_MAX_DELAY);
	          	}
	          	else if( inputNumber>30&&inputNumber <=80){
	          		HAL_UART_Transmit(&huart3, tx1, sizeof(tx1),HAL_MAX_DELAY);
	          	}
	          	else if( inputNumber < 30){
	          		HAL_UART_Transmit(&huart3, tx, sizeof(tx),HAL_MAX_DELAY);
	          	}

	            inputIndex = 0; // Reset the buffer index for the next input
	       }
	       else if (ch >= '0' && ch <= '9')
	             {
	               if (inputIndex < sizeof(inputBuffer) - 1)
	               {
	                 inputBuffer[inputIndex++] = ch; // Store the character in the buffer
	               }
	             }
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
  }
Leave a Comment