I am designing a traffic light control system with the following description:
- When pressing button 1 (name this button rst: reset), the traffic light system immediately goes into control state (programming anti-vibration button), the green light in the North-South direction will light up, the light will turn in the opposite direction. East and West will be bright
- When switch 1 is turned on, the yellow light in the North-South direction will light up for 3 seconds, counting down to 0 and then switching back to the red light, while the red light in the East-West direction will wait until the red light in the North-South direction appears before going back to the red light for 3 seconds. return to 0 and then switch back to green light.
- When switch 1 is turned off, the yellow light in the East-West direction will light up for 3 seconds, counting down to 0 and then switching back to the red light, while the red light in the North-South direction will wait until the red light in the East-West direction appears before going back to the red light for 3 seconds. return to 0 and then switch back to green light.
- When pressing the reset button, everything will return from step 2 However, when I run the code, all 4 of my yellow lights are on and I cannot control my lights. Please help me
I tried to fix my code, I have fixed the code and programmed the anti-vibration button, but it's all in vain, Here's my code:
uint8_t direct = HAL_GPIO_ReadPin(direct_GPIO_Port, direct_Pin);
uint8_t rst = HAL_GPIO_ReadPin(rst_GPIO_Port, rst_Pin);
uint8_t i;
uint8_t j;
uint8_t pre_state = GR;
uint8_t next_state;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1){
if (rst == 1) {
pre_state = GR;
} else {
pre_state = next_state;
}
switch (pre_state) {
case GR:
if (direct == 0) {
next_state = GR;
} else {
next_state = YR;
i = 3;
}
break;
case YR:
if (i != 0) {
next_state = YR;
i = i - 1;
} else if (i == 0){
pre_state = RR1;
next_state = RR1;
j = 3;
}
break;
case RR1:
if (j != 0) {
next_state = RR1;
j = j - 1;
} else if (j == 0){
pre_state = RG;
next_state = RG;
}
break;
case RG:
if (direct == 1) {
next_state = RG;
} else {
next_state = RY;
j = 3;
}
break;
case RY:
if (j != 0) {
next_state = RY;
j = j - 1;
} else if (j == 0){
pre_state = RR2;
next_state = RR2;
i = 3;
}
break;
case RR2:
if (i != 0) {
next_state = RR2;
i = i - 1;
} else if (i == 0){
pre_state = GR;
next_state = GR;
}
break;
}
if (pre_state == GR) {
HAL_GPIO_WritePin(R1_GPIO_Port, R1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(G1_GPIO_Port, G1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(Y1_GPIO_Port, Y1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(R2_GPIO_Port, R2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G2_GPIO_Port, G2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y2_GPIO_Port, Y2_Pin, GPIO_PIN_RESET);
} else if (pre_state == YR) {
HAL_GPIO_WritePin(R1_GPIO_Port, R1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(G1_GPIO_Port, G1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y1_GPIO_Port, Y1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(R2_GPIO_Port, R2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G2_GPIO_Port, G2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y2_GPIO_Port, Y2_Pin, GPIO_PIN_RESET);
} else if (pre_state == RR1) {
HAL_GPIO_WritePin(R1_GPIO_Port, R1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G1_GPIO_Port, G1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y1_GPIO_Port, Y1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(R2_GPIO_Port, R2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G2_GPIO_Port, G2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y2_GPIO_Port, Y2_Pin, GPIO_PIN_RESET);
} else if (pre_state == RR2) {
HAL_GPIO_WritePin(R1_GPIO_Port, R1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G1_GPIO_Port, G1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y1_GPIO_Port, Y1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(R2_GPIO_Port, R2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G2_GPIO_Port, G2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y2_GPIO_Port, Y2_Pin, GPIO_PIN_RESET);
} else if (pre_state == RG) {
HAL_GPIO_WritePin(R1_GPIO_Port, R1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(G1_GPIO_Port, G1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y1_GPIO_Port, Y1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(R2_GPIO_Port, R2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(G2_GPIO_Port, G2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(Y2_GPIO_Port, Y2_Pin, GPIO_PIN_RESET);
} else {
HAL_GPIO_WritePin(R1_GPIO_Port, R1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(G1_GPIO_Port, G1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y1_GPIO_Port, Y1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(R2_GPIO_Port, R2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(G2_GPIO_Port, G2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(Y2_GPIO_Port, Y2_Pin, GPIO_PIN_SET);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Since you are reading the
uint8_t direct = HAL_GPIO_ReadPin(direct_GPIO_Port, direct_Pin);anduint8_t rst = HAL_GPIO_ReadPin(rst_GPIO_Port, rst_Pin);only once and outside of yourwhile(1)loop you only put the initial state of those pins in the mentioned variables. To fix this replace the therstanddirectwithHAL_GPIO_ReadPin(...)inside your while loop.