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 ds screen. My sonar reads the distance correctly, but when I try to display the value on the screen, none seem to work. Only seven b:ABC the only digit displayed correctly works because I played around with the binary value until I got seven.
void setSegmentsunsigned char pattern
Configure segments AF on PORTD
PORTD PORTD & xpattern & xFC; Mask to apply pattern on PORTD
Configure segment G on PORTB
PORTB PORTB & xFDpattern & x; Apply bit pattern to PORTB
Function to select the active digit on the digit segment display only D and D are used
void selectDigitunsigned char digit
Deactivate all digits DD connected to PBPB
PORTB PORTB PORTB PORTB PORTB;
Activate only the selected digit
if digit
PORTB & ~ PORTB; Enable D connected to PORTB
else if digit
PORTB & ~ PORTB; Enable D connected to PORTB
Sonar task to read and process the distance from the sonar sensor
int SonarTickint state
switchstate State transitions
case SONARINIT:
state SONARREAD; Move to read state
break;
case SONARREAD:
state SONARREAD; Stay in read state
break;
default:
state SONARINIT;
break;
switchstate State actions
case SONARREAD:
Read distance from the sonar sensor
double distance sonarread;
currentdistance intdistance ; Convert to integer cm rounding up
Clamp the distance between and cm for display purposes
if currentdistance
currentdistance ;
else if currentdistance
currentdistance ;
Send distance data to the serial monitor
serialprintlnDistance: ;
serialprintlncurrentdistance;
serialprintln cm;
break;
return state;
Display task to handle updating the segment display based on the current distance
int DisplayTickint state
Segment patterns to display digits based on ABCDEFG format MSB to LSB
static const unsigned char segmentpatterns
b: ABCDEF no G
b: BC
b: ABDED
b: ABCD
b: BCF
b: ACDF
b: ACDEF
b: ABC only digit displayed correctly
b: ABCDEFG
b: ABCDF
;
switchstate State transitions
case DISPLAYINIT:
state DISPLAYUPDATE; Initialize to update state
break;
case DISPLAYUPDATE:
state DISPLAYUPDATE; Remain in update state
break;
default:
state DISPLAYINIT;
break;
switchstate State actions
case DISPLAYUPDATE:
if currentdigit Display tens place if distance
if currentdistance
selectDigit; Activate D for tens digit
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
