I have this program where it calculates resistor values. It's a really poor code but for the most part it works. My problem comes when my program tries to calculate the third band on the resistor (which goes up to 10M). I know that the values don't read because the value is too high and I'm using a string, but I can't find a way around this problem.
String Colours[] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white", "gold", "silver", "none" };
/****************************************************************************************************FUNCTION PROTOTYPES***************************************************************************************************************************************************/
void setup() {
Serial.begin(9600); //Opens communication with the computer for Serial.XYZ functions
}
/********************************************************************************************************Main Program******************************************************************************************************************************************************/
void loop() {
/******************************************************************************************Variable Declaration and Initialization*****************************************************************************************************************************************/
String Answers[14];// String declared
int a = 0, b = 0, c = 0, d = 0;// Variables used to manipulate arrays
char Again = 0; // Used to start program again
double ResValue;
/*******************************************************************************************************MAIN ALGORITHM*****************************************************************************************************************************************************/
do {
Serial.print("This program will calculate a resistors value by asking for the four band colours.");
Serial.print("\n What is the first band colour on your resistor?");
while (Serial.available() == 0) {}; // Wait for user input
Answers[a] = Serial.readString();
Answers[a].toLowerCase();// Force user input to all lowercase
Serial.print("\n You entered: ");
Serial.print(Answers[a]); // Print out user input
if (Answers[a] == "black") { // This will convert the first band colour into a number
a = 0;
}
else if (Answers[a] == "brown") {
a = 1;
}
else if (Answers[a] == "red") {
a = 2;
}
else if (Answers[a] == "orange") {
a = 3;
}
else if (Answers[a] == "yellow") {
a = 4;
}
else if (Answers[a] == "green") {
a = 5;
}
else if (Answers[a] == "blue") {
a = 6;
}
else if (Answers[a] == "violet") {
a = 7;
}
else if (Answers[a] == "grey") {
a = 8;
}
else if (Answers[a] == "white") {
a = 9;
}
else {
Serial.print("\n You entered an invalid colour");
}
Serial.print("\n What is the second band colour on your resistor?");
while (Serial.available() == 0) {}; // Wait for user input
Answers[b] = Serial.readString();
Answers[b].toLowerCase();// Force user input to all lowercase
Serial.print("\n You entered: ");
Serial.print(Answers[b]); // Print out user input
if (Answers[b] == "black") { // This will convert the second band colour into a number
b = 0;
}
else if (Answers[b] == "brown") {
b = 1;
}
else if (Answers[b] == "red") {
b = 2;
}
else if (Answers[b] == "orange") {
b = 3;
}
else if (Answers[b] == "yellow") {
b = 4;
}
else if (Answers[b] == "green") {
b = 5;
}
else if (Answers[b] == "blue") {
b = 6;
}
else if (Answers[b] == "violet") {
b = 7;
}
else if (Answers[b] == "grey") {
b = 8;
}
else if (Answers[b] == "white") {
b = 9;
}
else Serial.print("\n You entered an invalid colour");
Serial.print("\n What is the third band colour on your resistor?");
while (Serial.available() == 0) {}; // Wait for user input
Answers[c] = Serial.readString();
Answers[c].toLowerCase();// Force user input to all lowercase
Serial.print("\n You entered: ");
Serial.print(Answers[c]); // Print out user input
if (Answers[c] == "black") { // This will convert the third band colour into a number
c = 1;
}
else if (Answers[c] == "brown") {
c = 10;
}
else if (Answers[c] == "red") {
c = 100;
}
else if (Answers[c] == "orange") {
c = 1000;
}
else if (Answers[c] == "yellow") {
c = 10000;
}
else if (Answers[c] == "green") {
c = 100000;
}
else if (Answers[c] == "blue") {
c = 1000000;
}
else if (Answers[c] == "violet") {
c = 10000000;
}
else if (Answers[c] == "gold") {
c = 0.1;
}
else if (Answers[c] == "silver") {
c = 0.01;
}
else Serial.print("\n You entered an invalid colour");
Serial.print("\n What is the fourth band colour on your resistor?");
while (Serial.available() == 0) {}; // Wait for user input
Answers[d] = Serial.readString();
Answers[d].toLowerCase();// Force user input to all lowercase
Serial.print("\n You entered: ");
Serial.print(Answers[d]); // Print out user input
if (Answers[d] == "gold") { // This will convert the fourth band colour into a number
d = 5;
}
else if (Answers[d] == "silver") {
d = 10;
}
else if (Answers[d] == "none") {
d = 0;
}
else Serial.print("\n You entered an invalid colour");
ResValue = (a * 10 + b) * c; // Calculate the resistors value
Serial.print("\n The value of your resistor is: ");
Serial.print(ResValue, 0);
Serial.print("Ω +/-");
Serial.print(d);
Serial.print("%");
Serial.println("\n Do you wish to try again? Y or N");
while (Serial.available() == 0) {}; //Get user input
Again = Serial.read(); //Store user input
Serial.println(Again); //Print user input
}
while (Again == 'Y'); // Restart or cancel do loop
Serial.print("\n Thank you");
delay(10000);
Serial.print("\n");
} //Main Program Ends Here
/*****************************************************************************************************FUNCTION DEFINITIONS*************************************************************************************************************************************************/
integer values are 16 bits which go up to 65535 which is why values of 10M don't work. use long
i suggest you creates a subfunction to translate a color code into a numeric value rather than duplicate code. you can use pow() to translate the 3rd value into a multiplier.
you have gold and silver cases for your 3rd color that set an integer, c, to a float value. i don't believe the 3rd color can be gold or silver.