Instruction:
Code:
- User types decimal number (0-15) in serial monitor.
- Arduino reads the input and shows the corresponding hexadecimal number using Single seven segment display.
- If user types a out of range decimal value, Arduino will handle that by flashing “E” on the seven-segment display and an error message in serial monitor.
Code:
/* Name: Naive Decimal to Hex Converter
*/
int pin_2 = 2;
int pin_3 = 3;
int pin_4 = 4;
int pin_5 = 5;
int pin_6 = 6;
int pin_7 = 7;
int pin_8 = 8;
String string = ""; //initialize string
void displayZero() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, LOW);
}
void displayOne() {
digitalWrite(pin_2, LOW);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, LOW);
digitalWrite(pin_6, LOW);
digitalWrite(pin_7, LOW);
digitalWrite(pin_8, LOW);
}
void displayTwo() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, LOW);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, LOW);
digitalWrite(pin_8, HIGH);
}
void displayThree() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, LOW);
digitalWrite(pin_7, LOW);
digitalWrite(pin_8, HIGH);
}
void displayFour() {
digitalWrite(pin_2, LOW);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, LOW);
digitalWrite(pin_6, LOW);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displayFive() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, LOW);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, LOW);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displaySix() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, LOW);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displaySeven() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, LOW);
digitalWrite(pin_6, LOW);
digitalWrite(pin_7, LOW);
digitalWrite(pin_8, LOW);
}
void displayEight() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displayNine() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, LOW);
digitalWrite(pin_6, LOW);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displayA() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, LOW);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displayB() {
digitalWrite(pin_2, LOW);
digitalWrite(pin_3, LOW);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displayC() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, LOW);
digitalWrite(pin_4, LOW);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, LOW);
}
void displayD() {
digitalWrite(pin_2, LOW);
digitalWrite(pin_3, HIGH);
digitalWrite(pin_4, HIGH);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, LOW);
digitalWrite(pin_8, HIGH);
}
void displayE() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, LOW);
digitalWrite(pin_4, LOW);
digitalWrite(pin_5, HIGH);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void displayF() {
digitalWrite(pin_2, HIGH);
digitalWrite(pin_3, LOW);
digitalWrite(pin_4, LOW);
digitalWrite(pin_5, LOW);
digitalWrite(pin_6, HIGH);
digitalWrite(pin_7, HIGH);
digitalWrite(pin_8, HIGH);
}
void setup() {
//set pin 2 to 8 as output
pinMode(pin_2, OUTPUT);
pinMode(pin_3, OUTPUT);
pinMode(pin_4, OUTPUT);
pinMode(pin_5, OUTPUT);
pinMode(pin_6, OUTPUT);
pinMode(pin_7, OUTPUT);
pinMode(pin_8, OUTPUT);
Serial.begin(9600); //initialize serial
}
void loop() {
if(Serial.available() > 0){
char ch = (char) Serial.read();
if(ch == '\r') {
if(string == "0") {
displayZero();
} else if(string == "1") {
displayOne();
} else if(string == "2") {
displayTwo();
} else if(string == "3") {
displayThree();
} else if(string == "4") {
displayFour();
} else if(string == "5") {
displayFive();
} else if(string == "6") {
displaySix();
} else if(string == "7") {
displaySeven();
} else if(string == "8") {
displayEight();
} else if(string == "9") {
displayNine();
} else if(string == "10") {
displayA();
} else if(string == "11") {
displayB();
} else if(string == "12") {
displayC();
} else if(string == "13") {
displayD();
} else if(string == "14") {
displayE();
} else if(string == "15") {
displayF();
} else {
displayE();
Serial.println("NUMBER IS OUT OF RANGE");
}
string = "";
} else {
string += ch;
}
}
}
No comments:
Post a Comment