forked from passion-tech/Hello-tech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7segment_display_code.ino
74 lines (59 loc) · 2.08 KB
/
7segment_display_code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#define type 0 // 0 for common cathode and 1 for common anode
const int displayPins[7] = {2,3,4,5,6,7,8}; // define pins
const int dicimalPoint = 9;// decimal point
// array for seven segment LED values. Do not change.
//This is used for both common anode and common cathode
byte displayLEDs[10][7] = {
{ 0,0,0,0,0,0,1 }, // = 0
{ 1,0,0,1,1,1,1 }, // = 1
{ 0,0,1,0,0,1,0 }, // = 2
{ 0,0,0,0,1,1,0 }, // = 3
{ 1,0,0,1,1,0,0 }, // = 4
{ 0,1,0,0,1,0,0 }, // = 5
{ 0,1,0,0,0,0,0 }, // = 6
{ 0,0,0,1,1,1,1 }, // = 7
{ 0,0,0,0,0,0,0 }, // = 8
{ 0,0,0,0,1,0,0 } // = 9
};
void writeDigit(int digit) {
//Seven Segment Display for Arduino
Serial.print(digit);
Serial.print(" ");
int digitValue;
for (int i=0; i < 7; i++)
{
digitalWrite(displayPins[i], convertHighLow(displayLEDs[digit][i]));// write the HGIH or LOW (depending on the type) to output pin
Serial.print(convertHighLow(displayLEDs[digit][i]));// print the result to the Serial monitor
}// for i
}//writeDigit ends here
int convertHighLow(int v)
{
int convertedValue;
if(type ==0)
{
convertedValue = 1 - v;// if common anode, change the HIGH to LOW and LOW to HIGH
}else{
convertedValue = v;// if common cathode, keep it the same
}
return convertedValue;
}
void setup() {
//Seven Segment Display for Arduino
for(int d=0; d<8; d++)
{
pinMode(displayPins[d], OUTPUT);// set pin as output
}
pinMode(dicimalPoint,OUTPUT);// define a pin for decimal pint
digitalWrite(dicimalPoint,LOW);// turn decimal point OFF. Set to HIGH if common Anode and LOW for common cathode
Serial.begin(9600);// initialize serial monitor with 9600 baud
}// setup ends here
void loop() {
//Seven Segment Display for Arduino
for (int i=0; i<=9 ; i++) {
writeDigit(i);
Serial.println("=====");
delay(1000);
}
digitalWrite(dicimalPoint,HIGH);// turn decimal point ON
delay(3000);
}// loop ends here