Introduction
The program is designed so as to calculate the resistance of a resistor based on the color bands that the resistor has. Color bands are used as a simple way of representing the resistance values especially since indicating the figures may not be a feasible alternative, owing majorly to the small size of the resistors. The colors that are normally used in resistors (and their corresponding values) are: Black (1), Brown (2), Red (3), Orange (4), Yellow (5), Green (6), Blue (7), Violet (8), Gray (9), White (10). The color bands in a four band resistor each serves a special function, with the first two bands serving as an indicator for the numeric value of the resistor. The third band serves to denote the exponent to base 10 (power of ten multiplier) of the resistance value. The final band is used to denote the tolerance value, with the most common values for 4 band resistors being Gold (±5%), Silver (±10%) and ±20% when there is no 4th color band.
Project Objective
The objective of this project is to create a simple C-based program that will calculate the values of resistance for a 4-band resistor based on the colors of the bands. The program takes in the textual representation of the colors, performs calculations based on the values of these colors and returns the computed values to the code that called it. Or simply, the program will get the colors as input and give the resistance as output. The program serves to give an easy and efficient way to calculate resistance values with minimal computation and maximum accuracy
Methodology
The program first begins by prompting the user to state whether they want to calculate resistance for 4-band resistors based on their color values. If they accept (choose ‘yes’), the program prompts the user for inputs, specifically the color codes for the 4-bands on the resistor. The colors are represented internally as (an array of) characters. Along with the prompt, the program issues some directions to ensure that the values are entered correctly, and the colors chosen are from a subset of valid colors. Following the choice, the program gets the color codes and runs them through a search function that ensures the user input is of the right type, and that the colors are in the array that is used to store valid resistor band values. If valid, the program uses the color codes for calculation of resistance. Resistance is calculated as
Resistance = ((10*first color band + second color band )*(pow(10,third color band)))
The tolerance is calculated based on the fourth color band. Tolerance is calculated as a positive or negative factor on the resistance value, that is if the band denotes a tolerance value of 5%, then the resistance value will range from between 95-105% of the resistance.
Resistance Range = Resistance Value ± Tolerance
The program then outputs the relevant values for resistance and resistance range.
Algorithm Development
Pseudocode
DECLARE variables colorBand1 through to colorBand 4
WHILE user wants to calculate the resistance for a resistor
PROMPT user for inputs colors
GET input colors
SEARCH for colors in array
IF colors are valid members of the array
CALCULATE resistance
DISPLAY resistance
CALCULATE tolerance
DISPLAY resistance range
END IF
END WHILE
EXIT
Flowchart
C-program Code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int search (char codes[10][7], char target[], int size);
int main (void)
{
char COLORCODES [10][7] = {“black”, “brown”, “red”, “orange”, “yellow”, “green”, “blue”, “violet”, “gray”, “white”};
float resValue;
char colorBand1[7];
char colorBand2[7];
char colorBand3[7];
char colorBand4[7];
int i,j,k;
char question;
printf(“\nDo you want to run the program now (Type ‘y’ for yes, or ‘n’ for no)?\n “);
scanf(“%c”, &question);
while (question == ‘y’)
{
printf(“\nEnter the colors of the resistor’s four bands. Type the colors in lowercase letters only. “);
printf(“\nThe color for the three first bands are as follows\n\n”);
printf(“\tblack\n\tbrown\n\tred\n\torange\n\tyellow\n\tgreen\n\tblue\n\tviolet\n\tgray\n\twhite\n”);
printf(“\nThe colors for the tolerance band are as follows\n”);
printf(“\n\tgold\n\tsilver\n\tnone\n\n”);
printf (“Enter the first Band:”);
scanf(“%s”,&colorBand1);
printf (“Enter the second Band:”);
scanf(“%s”,&colorBand2);
printf (“Enter the third Band:”);
scanf(“%s”,&colorBand3);
printf (“Enter the fourth Band:”);
scanf(“%s”,&colorBand4);
i = search (COLORCODES, colorBand1, 10);
j = search (COLORCODES, colorBand2, 10);
k = search (COLORCODES, colorBand3, 10);
resValue = ((10*i+j)*(pow(10,k)));
if (i == -1 || i == 0)
{
printf (“Invalid colors : %s”, i);
}
if (j == -1 )
{
printf (“Invalid colors : %s”, j);
}
if (k == -1 )
{
printf (“Invalid colors : %s”, k);
}
else
{
printf (“Resistance value: %.0f ohms\n”, resValue);
}
double tolerance;
if(colorBand4 == “gold”){
tolerance = .05*resValue;
}else if(colorBand4 == “silver”){
tolerance = .1*resValue;
}else{
tolerance = .2*resValue;
}
printf(“\nResistance value range is between %.2f and %.2f Ohms”, resValue-tolerance, resValue+tolerance);
printf(“\nDo you want run the program again (Type ‘y’ for yes, or ‘n’ for no)? \n”);
scanf(” %c”, &question);
}
system(“pause”);
return 0;
}
int search (char codes[][7], char target[], int size)
{
int x = 0;
int match = 0;
int location = 0;
while (!match && x < size)
{
if(strcmp(codes[x], target) == 0)
{
match = 1;
}
else
{
++x;
}
}
if(match == 1)
{
location = x;
}
else
{
location = -1;
}
return(location);
}
Testing and Results
Testing is done to ensure that the operation is as expected. The program is expected to give values that are consistent with what a manual calculation of the same will give. The tests that the program has to undergo include that it only accepts the right type of data, returns the right type of data and handles computation correctly. Final test involves ensuring that the loops in the program are coded correctly and logic does not fall through.
The test data for the program is as follows
# | COLOR BAND | VALUE |
1 | Brown | 1 |
2 | Black | 0 |
3 | Red | 103 |
4 | Silver | ±10% |
Table 1. Test data
From the test data, resistance should be calculated as ((1*10+0)*(103) ±10%). The result that is expected is a resistance value of 1000 Ohms before factoring in tolerance. With tolerance, the value is expected to range between 900-1100Ohms.
The results of running the program on a similar set of data is as follows:
Fig 1. Computation results
The program results are found to be consistent with the values that are expected for the test results.
Conclusion
The program is found to be an accurate way to get a quick assessment of the resistance value of a resistor based on the color bands that are printed on it. With this program, quick and accurate assessments of resistance values can be achieved. With a little extension, the program can be modified to carry out the same kind of computation on 3-band and 5-band resistors. Evidently, this is a handy tool for the said computation.
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more