Asked
— Edited

Hi @Athena,
I am experiencing some difficulty with the implementation of the map
function in C++. Could you help me review the following code snippet to determine if it is correct? The code is designed to adjust the speed of two stepper motors based on a variable, psudoDesCrsDiffSum
, which can take values from -500 to 500.
// leftSpeed = speed setting for the left stepper motor
// rightSpeed = speed setting for the right stepper motor
// psudoDesCrsDiffSum = an integer value ranging from -500 to +500
leftSpeed = map(psudoDesCrsDiffSum, -500, 500, 1600, 400);
rightSpeed = map(psudoDesCrsDiffSum, -500, 500, 400, 1600);
Thank you, Jack
Related Hardware Roli Rover
Hi DJ_Sures,
You're absolutely right-unit testing is a very effective approach to isolate and verify your code logic, and your two outlined strategies are spot on. Let’s break them down:
1. Using Compiler Directives
By leveraging compiler directives (e.g., using #ifdef, #ifndef, etc.), you can compile only specific sections of your code for testing. For example:
This way, you can selectively isolate code blocks and run tests with predetermined constants and conditions without impacting the production code. It works particularly well when working in environments like Arduino, where code size and efficiency are important.
2. Splitting into Granular Functions
The second approach is to break down your program into smaller, independent functions. Once modularized, these functions can be directly invoked from a separate test harness. For example:
You can then create a separate test file or section:
This method makes it easier to pinpoint issues since you isolate and test smaller logical units rather than debugging the entire program at once.
Additional Thoughts
I absolutely agree-taking a little extra time to test small portions not only improves code reliability but also builds confidence in your development process. If you need further examples or help in setting up unit tests within your project, or how to best integrate this with your Synthiam ARC setup, feel free to ask!
What’s your experience been like with unit testing in your projects so far?
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.