Question: I ' m having trouble with my code currently. the code needs to split a playback delay so the display and buzzer are on for

I'm having trouble with my code currently. the code needs to split a "playback delay" so the display and buzzer are on for half and then blank and silent for the other half currently this is what i have: (test results at the bottom)
void delay_function(int percentage){
// Ensure percentage is non-zero and valid
if (percentage ==0){
return; // Avoid unnecessary delays if percentage is 0
}
// Calculate the base delay as a function of the potentiometer input, scaled between 0.25 and 2 seconds
potentiometer_value = ADC0.RESULT0; // Read the potentiometer input
scaled_value =(((float)potentiometer_value /255.0)*(2.0-0.25))+0.25; // Scale to 0.25-2 seconds
// Adjust the delay using the percentage value
clock_delay =(scaled_value *66666)/ percentage; // Calculate clock cycles required for the delay
// Delay loop
for (volatile uint32_t i =0; i < clock_delay; i++){
// No operation (NOP)
}
}
void playback_sequence(uint8_t sequence_values[], int sequence_length){
for (uint8_t i =0; i < sequence_length && sequence_status ==1; i++){
play_sequence_step(sequence_values[i]); // Play each step (set display and buzzer)
// Active buzzer and display for 50% of the duration (125 ms equivalent)
delay_function(2); // Use percentage for half of the playback duration with tone and display active
// Turn off both the buzzer and the display after 125 ms
TCA0.SINGLE.PER =0; // Turn off the buzzer
TCA0.SINGLE.CMP0=0; // Set the duty cycle to 0(silence)
spi_transmit(display_buffer[0]= SEGMENT_OFF); // Transmit the off state for the first display segment
spi_transmit(display_buffer[1]= SEGMENT_OFF); // Transmit the off state for the second display segment
// Delay for the remaining 50% of the duration (inactive period)
delay_function(2); // Use percentage for half of the playback duration with tone and display off
}
sequence_status =0; // Sequence is done
}
void handle_value(uint8_t display_one, uint8_t display_two, int tone){
spi_transmit(display_buffer[0]= display_one); // Set and transmit the first display value
spi_transmit(display_buffer[1]= display_two); // Set and transmit the second display value
TCA0.SINGLE.PER = tone; // Set the tone frequency
TCA0.SINGLE.CMP0=(tone /2)+1; // Set the duty cycle for the tone (50%)
}
void play_sequence_step(uint8_t step_value){
// Handle tone and display for the active phase
if (step_value ==0){
handle_value(SEGMENT_1, SEGMENT_OFF, TONE1_PER); // Play tone 1 and display
} else if (step_value ==1){
handle_value(SEGMENT_2, SEGMENT_OFF, TONE2_PER); // Play tone 2 and display
} else if (step_value ==2){
handle_value(SEGMENT_OFF, SEGMENT_1, TONE3_PER); // Play tone 3 and display
} else if (step_value ==3){
handle_value(SEGMENT_OFF, SEGMENT_2, TONE4_PER); // Play tone 4 and display
}
}
test results:
Gameplay events: 350 ms: S31450 ms: S42855 ms: S23955 ms: S24205 ms: S1 Detected buzzer events: 6 ms: 427.10 Hz,50.00% duty cycle 360 ms: 0.00 Hz,0.00% duty cycle Detected display events: 1 ms: "|"1 ms: "8"81 ms: "8"181 ms: "|"181 ms: "8"241 ms: ""251 ms: "|"341 ms: ""341 ms: "8"451 ms: ""[EVENT 1- Simon][PLAYBACK DELAY: 250 ms] FAIL. Buzzer did not turn OFF after 125 ms. FAIL. Display produced "|" too late. Correct output was detected 181 ms after program start. Further testing requires all tests to pass.

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!