in arduino c. 3. Resistor Color Code a. The name of this program will be lab5p3
ID: 3751424 • Letter: I
Question
in arduino c.
3. Resistor Color Code a. The name of this program will be lab5p3 b. Write the proto-type for function convert_color that has as input a character representing a color and as output an integer number, based on the resistor color code (below). The input character can be upper or lowercase c. In loop(), ask for three characters at once. You only need one prompt, but you can store (and echo print) three characters with 3 Serial.read) statements. d. Convert each character to an integer by calling covert_color three times. e. Using the formula below, the resistor value can be calculated IntlInt2 x 10 Int3 f. For example, a resistor with colors: red-violet-orange has value 27x10 3-27000 Ohms g. Use floating point for the resistor value, since integers are limited to /-32000 h. Below is a sample run. As part of your program also write and call a function to display the rather detailed instructions below (which you can cut and paste) RESISTOR COLOR-CODE PROGRAM Page 2 EEI 12-Embedded Systems Lab 5 Fall 2018 Input three characters corresponding to the three color bands on a resistor: k for black-0 n for brown-1 r for red-2 o for orange-3 y for yellow 4 g for green 5 u for blue-6 v for violet-7 e for grey8 w for white9 Input three characters rVO 27000 Ohms Input three characters: GuK xxxxx Ohms i. Convert input sequence GuK to a resistor value in Ohms Run the program. Show the results to the TA. Each student upload the source file to CanvasExplanation / Answer
#include <math.h>
int res;
String code;
int int1, int2, int3;
float result;
int convert_color(char c)
{
if(c == 'k' || c == 'K')
res = 0;
else if(c == 'n' || c == 'N')
res = 1;
else if(c == 'r' || c == 'R')
res = 2;
else if(c == 'o' || c == 'O')
res = 3;
else if(c == 'y' || c == 'Y')
res = 4;
else if(c == 'g' || c == 'G')
res = 5;
else if(c == 'u' || c == 'U')
res = 6;
else if(c == 'v' || c == 'V')
res = 7;
else if(c == 'e' || c == 'E')
res = 8;
else
res = 9;
return res;
}
void display()
{
Serial.println("Input three characters corressponding to the three color bands on a resistor: ");
Serial.println("k for black - 0");
Serial.println("n for brown - 1");
Serial.println("r for red - 2");
Serial.println("o for orange - 3");
Serial.println("y for yellow - 4");
Serial.println("g for green - 5");
Serial.println("u for blue - 6");
Serial.println("v for violet - 7");
Serial.println("e for grey - 8");
Serial.println("w for white - 9");
Serial.println("");
Serial.print("Input three characters: ");
}
void setup()
{
Serial.begin(9600);
display();
}
void loop()
{
while(Serial.available())
{
code = Serial.readString();
}
int1 = convert_color(code[0]);
int2 = convert_color(code[1]);
int3 = convert_color(code[2]);
result = (int1*10 + int2) * (float)pow(10, int3);
Serial.println(result, 0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.