Home‎ > ‎CS 11M‎ > ‎

Project 4: Resistor Color Codes


  • Code more complex decisions
  • Use a function in a program.
  • Use string variables in a program.
  • Work with resistor color codes.


In this project you write a program to convert resistor color codes to ohm values.

Do all problems in Code Lab 4

Starter Code

#include <ArduinoSTL.h>

using namespace std; 

void setup() {

void loop() {
  string band1;
  string band2; 
  string band3; 
  cout << "Enter a resistor color code." << endl;
  cin >> band1 >> band2 >> band3; 

  int band1value = getNumberFromCode(band1);
  int band2value = getNumberFromCode(band2);
  int band3value = getNumberFromCode(band3);

  // Calculate the resistor value.

int getNumberFromCode(string code) {
  int number = 0;

  // Use the code from the class exercise. 

  return number;


Before you begin: You must update to version v0.1.6 of the ArduinoSTL library if you want floats and doubles to print properly from cout!
  1. Start with a new sketch and save the sketch using the name resistor_codes.
  2. Write a program that takes in three-band resistor color code in the following shorthand notation:
    B   Black
    Br  Brown
    R   Red
    O   Orange
    Y   Yellow
    G   Green
    Bu  Blue
    V   Violet
    Gy  Gray
    W   White
  3. The program gets the three band color codes one at a time with prompts for each value, like:
    Enter three bands:
  4. After obtaining the three band color codes, the program displays a full description like:
    The resistor is 560 ohms
    GRADER: The grading program will look for one number and one of:
    <number> ohms
    <number>k ohms
    <number>M ohms
    Anything else will be ignored.
  5. The colors with two letter codes must work with the second letter either upper or lower case.
  6. Write the following function, which is called three times, to convert color codes to numbers
    int getNumberFromCode(string code)
  7. When the multiplier exceeds 1, convert to kilohms.
  8. Similarly, when the multiplier exceeds 5, convert to megaohms.
  9. After converting one set of codes, allow the user to convert other codes as shown below in the example output.
  10. Submit your resistor_codes.ino file as part of this assignment.

Hints and Explanations

  • Use a switch statement to produce the multiplier.  Avoid the pow() function because of poor floating point precision.
  • You may need large integers to do your calculations. Try using long int or long long int instead of int when you calculate resistor values.
  • cout will not print a long long int (neither will Arduino's Serial.print())

Example Output

Your program should behave similarly to this:
Enter three color codes. Br B Br 100.00 ohms.
Enter three color codes. Br G R 1.50k ohms. Enter three color codes. BR G R 1.50k ohms. Enter three color codes. Bu G W 65000.00M ohms.

Accuracy Extra Credit 

Your program only needs to be accurate for resistor values of 10 M ohms or less. If your program works properly for values larger than 10M ohms you will be awarded +5 extra credit. 

Grading Criteria (20)

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

  • 20: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications
    • Runs as required with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 16: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 12: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 6: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or not original work

How to Submit

Upload all project files to Canvas in the project folder that matches the name of this project. Include the following items for grading:

  1. resistor_codes.ino

You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline, but must resubmit all your assignment files.