Program is not working consistantly

Hello, hope you can help to solve the issue,

I wonder what causes the program is not working consistently. I worked on SPI camera, but sometimes it runs and sometimes it doesn't. Even I used the Teensy 3.6 board or Leonardo board and it happens the same. I also changed the camera but it still the issue. Any advice?

Thanks

Add debug output to your code, to learn more about where the program hangs.

I'm not sure how "Add debug output to the code". would you teach me how? Thanks

Add some print commands at strategic points to print out some variable values, blink an led