In: Computer Science
Q1) Modify the following code to sequentially display 154628577990449700
/* DisplayDigitsUsingIndexedArray - Sequentially displays digits 0 - 9 using an indexed array and register I/O Written by Duncan McGehee 1 October 2011 */ /*PORTD.7 is connected to the A-segment of the 7-segment display PORTD.6 = B, PORTD.5 = C, PORTD.4 = D, PORTD.3 = E PORTD.2 = F, PORTD.1 = G */ /* Declare an array of binary numbers that will be used to drive the 7-segment display. This is done once, globally, so that we don't redeclare it each time the program loops */ byte DisplayNumbers[] = {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110}; void setup() { DDRD = B11111110; /* DDRD is the direction register for the Arduino's PORT D (Digital Pins 0 - 7). DDRD = B11111110 tells Arduino to set pin 0 for input, pins 1 through 7 for output */ } void loop() //This function loops forever { //Send each digit to the 7-segment display for .5 second for (byte index = 0; index <= 9; index++) { PORTD = DisplayNumbers[index]; //Use the indexed array to choose the digit delay(500); } //turn off the 7-segment display for half a second PORTD = B00000000; delay(500); } |
Please up vote ,comment if any query . Thanks for question . Be safe .
Note : check attached image for simulation output ,code compiled in ARDUINO IDE and simulation tested in Proteus VSM .
Program Plan :
Program :
/* DisplayDigitsUsingIndexedArray - Sequentially displays
digits 0 - 9 using an indexed array and register I/O
Written by Duncan McGehee
1 October 2011 */
/*PORTD.7 is connected to the A-segment of the 7-segment display
PORTD.6 = B, PORTD.5 = C, PORTD.4 = D, PORTD.3 = E
PORTD.2 = F, PORTD.1 = G */
/* Declare an array of binary numbers that will be used to drive
the 7-segment display. This is done once, globally, so that we
don't redeclare it each time the program loops */
/*
byte DisplayNumbers[] = {B11111100, B01100000, B11011010,
B11110010,
B01100110, B10110110, B10111110, B11100000,
B11111110, B11110110};*/
//Create new array of DisplayNumbers with value of size 18
//1,5,4,6,2,8,5,7,7,9,9,0,4,4,9,7,0,0
byte
DisplayNumbers[]={B01100000,B10110110,B01100110,B10111110,B11011010,B11111110,B10110110,B11100000,B11100000,
B11110110, B11110110,B11111100,
B01100110,B01100110,B11110110, B11100000,B11111100,B11111100
};
void setup()
{
DDRD = B11111110; /* DDRD is the direction register for
the Arduino's PORT D (Digital Pins 0 - 7). DDRD = B11111110
tells Arduino to set pin 0 for input, pins 1 through 7 for output */
}
void loop() //This function loops forever
{
//Send each digit to the 7-segment display
for .5 second
//display element from array index 0 to 17
for (byte index = 0; index < 18; index++)
{
PORTD = DisplayNumbers[index]; //Use the indexed array to choose the digit
delay(500);
}
//turn off the 7-segment display for half a second
PORTD = B00000000;
delay(500);
}
Simulation Output :
Please comment if you want any changes .