Aim:
To take a messy piece of code and improve it for clarity, reliability, and maintainability.
Requirements:
- Arduino IDE or Thonny IDE
- Arduino Uno or Raspberry Pi Pico
- Multi-sensor setup (e.g., LDR + LM35 temperature sensor)
- A sample buggy code (given by teacher)
Procedure (Step-by-Step)
- Examine the Given Code
- Read through the messy code carefully.
- Identify unclear variable names, repeated logic, and missing explanations.
- Add Comments
- Write a short note above each important section explaining what it does.
Example:
cpp
CopyEdit
// Reading temperature from LM35 sensor
int tempValue = analogRead(A0);
- Organize into Functions
- Break the code into smaller functions such as:
- setupSensor() → to initialize sensors
- readTemp() → to get temperature data
- controlLight() → to turn LED ON/OFF based on conditions
- Break the code into smaller functions such as:
- Test the Code
- Upload your cleaned code to the microcontroller.
- Run and check if it behaves as expected.
- Debug and Improve
- If it doesn’t work, use Serial Monitor or print statements to see where it’s going wrong.
- Remove any unnecessary lines and make the code more efficient.
- Final Review
- Ensure that the code works in all test cases.
- Check if it’s readable for someone new to the project.