Question: I need help displaying the correct values on my 4 d 7 s screen. My sonar reads the distance correctly, but when I try to

I need help displaying the correct values on my 4d7s screen. My sonar reads the distance correctly, but when I try to display the value on the screen, none seem to work. Only seven b0111000,//7:ABC --> the only digit displayed correctly works because I played around with the binary value until I got seven.
void setSegments(unsigned char pattern){
// Configure segments A-F on PORTD
PORTD =(PORTD & 0x03)|((pattern & 0xFC)<<2); // Mask to apply pattern on PORTD2-7
// Configure segment G on PORTB
PORTB =(PORTB & 0xFD)|((pattern & 0x02)>>1); // Apply bit pattern to PORTB1
}
// Function to select the active digit on the 4-digit 7-segment display (only D3 and D4 are used)
void selectDigit(unsigned char digit){
// Deactivate all digits (D1-D4, connected to PB2-PB5)
PORTB |=(1<< PORTB2)|(1<< PORTB3)|(1<< PORTB4)|(1<< PORTB5);
// Activate only the selected digit
if (digit ==3){
PORTB &= ~(1<< PORTB3); // Enable D3, connected to PORTB3
} else if (digit ==4){
PORTB &= ~(1<< PORTB2); // Enable D4, connected to PORTB2
}
}
// Sonar task to read and process the distance from the sonar sensor
int Sonar_Tick(int state){
switch(state){// State transitions
case SONAR_INIT:
state = SONAR_READ; // Move to read state
break;
case SONAR_READ:
state = SONAR_READ; // Stay in read state
break;
default:
state = SONAR_INIT;
break;
}
switch(state){// State actions
case SONAR_READ:
// Read distance from the sonar sensor
double distance = sonar_read();
current_distance =(int)(distance +0.5); // Convert to integer cm, rounding up
// Clamp the distance between 3 and 65 cm for display purposes
if (current_distance <3){
current_distance =3;
} else if (current_distance >65){
current_distance =65;
}
// Send distance data to the serial monitor
serial_println("Distance: ");
serial_println(current_distance);
serial_println(" cm");
break;
}
return state;
}
// Display task to handle updating the 7-segment display based on the current distance
int Display_Tick(int state){
// Segment patterns to display digits 0-9 based on ABCDEFG format (MSB to LSB)
static const unsigned char segment_patterns[]={
0b1111110,//0: ABCDEF (no G)
0b0110000,//1: BC
0b1101101,//2: ABDED
0b1111001,//3: ABCD
0b0110011,//4: BCF
0b1011011,//5: ACDF
0b1011111,//6: ACDEF
0b0111000,//7: ABC --> only digit displayed correctly
0b1111111,//8: ABCDEFG
0b1111011//9: ABCDF
};
switch(state){// State transitions
case DISPLAY_INIT:
state = DISPLAY_UPDATE; // Initialize to update state
break;
case DISPLAY_UPDATE:
state = DISPLAY_UPDATE; // Remain in update state
break;
default:
state = DISPLAY_INIT;
break;
}
switch(state){// State actions
case DISPLAY_UPDATE:
if (current_digit ==3){// Display tens place if distance >=10
if (current_distance >=10){
selectDigit(3); // Activate D3 for tens digit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!