XuanTao_2453143

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

4.

// C++ code

//

void setup()

pinMode (0 , OUTPUT ) ;

pinMode (1 , OUTPUT ) ;

void loop()

digitalWrite(0, HIGH);

digitalWrite(1, LOW);

delay(2000);

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

delay(2000);// Wait for 1000 millisecond(s)

4.2

void setup(){

pinMode(1, OUTPUT);

pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

void loop(){

digitalWrite(1, HIGH);

digitalWrite(3, HIGH);

digitalWrite(5, HIGH);

digitalWrite(7, HIGH);

digitalWrite(2, LOW);

digitalWrite(4, LOW);

digitalWrite(6, LOW);

digitalWrite(8, LOW);

delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, LOW);

digitalWrite(5, LOW);

digitalWrite(7, LOW);

digitalWrite(2, HIGH);

digitalWrite(4, HIGH);

digitalWrite(6, HIGH);

digitalWrite(8, HIGH);
delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, HIGH);

digitalWrite(5, LOW);

digitalWrite(7, HIGH);

digitalWrite(2, HIGH);

digitalWrite(4, LOW);

digitalWrite(6, LOW);

digitalWrite(8, HIGH);

delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, LOW);

digitalWrite(5, LOW);

digitalWrite(7, HIGH);

digitalWrite(2, HIGH);

digitalWrite(4, LOW);

digitalWrite(6, LOW);

digitalWrite(8, LOW);

delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, HIGH);

digitalWrite(5, HIGH);
digitalWrite(7, LOW);

digitalWrite(2, HIGH);

digitalWrite(4, LOW);

digitalWrite(6, HIGH);

digitalWrite(8, HIGH);

delay(1000);

digitalWrite(1, HIGH);

digitalWrite(3, HIGH);

digitalWrite(5, LOW);

digitalWrite(7, HIGH);

digitalWrite(2, LOW);

digitalWrite(4, LOW);

digitalWrite(6, LOW);

digitalWrite(8, LOW);

delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, LOW);

digitalWrite(5, LOW);

digitalWrite(7, HIGH);

digitalWrite(2, LOW);

digitalWrite(4, LOW);

digitalWrite(6, LOW);

digitalWrite(8, HIGH);
delay(1000);

digitalWrite(1, HIGH);

digitalWrite(3, LOW);

digitalWrite(5, HIGH);

digitalWrite(7, LOW);

digitalWrite(2, HIGH);

digitalWrite(4, LOW);

digitalWrite(6, HIGH);

digitalWrite(8, HIGH);

delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, HIGH);

digitalWrite(5, HIGH);

digitalWrite(7, HIGH);

digitalWrite(2, LOW);

digitalWrite(4, HIGH);

digitalWrite(6, LOW);

digitalWrite(8, HIGH);

delay(1000);

digitalWrite(1, LOW);

digitalWrite(3, LOW);

digitalWrite(5, LOW);

digitalWrite(7, HIGH);

digitalWrite(2, LOW);
digitalWrite(4, LOW);

digitalWrite(6, LOW);

digitalWrite(8, LOW);

delay(1000);

4.3

int pinled [] = {0,1,2,3,4,5,6};

void setup(){

for (int i = 0; i < 7; i++) {

pinMode(pinled [i], OUTPUT);

void displayNumber(int i){

if (i == 0){

digitalWrite(0, HIGH);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 1){


digitalWrite(0, HIGH);

digitalWrite(1, HIGH);

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 2){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

}else if(i == 3){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 4){

digitalWrite(0, LOW);

digitalWrite(1, LOW);
digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 5){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 6){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, HIGH);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 7){

digitalWrite(0, HIGH);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);
digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 8){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 9){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

void loop(){

displayNumber(0);

delay(1000);

for (int i = 0; i < 10; i++) {


displayNumber(i);

delay(1000);

4.4

// C++ code

//

void setup()

for(int i = 0; i < 14; i++){

pinMode(i, OUTPUT);

void displayNumber1(int j){

if (j == 0){

digitalWrite(0, HIGH);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(j == 1){

digitalWrite(0, HIGH);

digitalWrite(1, HIGH);
digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(j == 20){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

void displayNumber2(int i){

if (i == 0){

digitalWrite(7, HIGH);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}else if(i == 1){


digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

digitalWrite(9, HIGH);

digitalWrite(10, LOW);

digitalWrite(11, HIGH);

digitalWrite(12, HIGH);

digitalWrite(13, LOW);

}else if(i == 2){

digitalWrite(7, LOW);

digitalWrite(8, HIGH);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, HIGH);

}else if(i == 3){

digitalWrite(7, LOW);

digitalWrite(8, HIGH);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}else if(i == 4){

digitalWrite(7, LOW);

digitalWrite(8, LOW);
digitalWrite(9, HIGH);

digitalWrite(10, LOW);

digitalWrite(11, HIGH);

digitalWrite(12, HIGH);

digitalWrite(13, LOW);

}else if(i == 5){

digitalWrite(7, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, HIGH);

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}else if(i == 6){

digitalWrite(7, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, HIGH);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}else if(i == 7){

digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

digitalWrite(9, LOW);

digitalWrite(10, LOW);
digitalWrite(11, HIGH);

digitalWrite(12, HIGH);

digitalWrite(13, LOW);

}else if(i == 8){

digitalWrite(7, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}else if(i == 9){

digitalWrite(7, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}else if(i == 20){

digitalWrite(7, HIGH);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, LOW);

digitalWrite(12, LOW);
digitalWrite(13, LOW);

void loop()

for(int j = 0; j < 2; j++){

displayNumber1(j);

for(int i = 0; i < 10; i++){

displayNumber2(i);

delay(1000);

displayNumber1(20);

displayNumber2(0);

delay(1000);

4.5

void setup(){

for (int i = 0; i < 14; i++) {

pinMode(i, OUTPUT);

}
void displayNumber(int i){

if (i == 0){

digitalWrite(0, HIGH);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 1){

digitalWrite(0, HIGH);

digitalWrite(1, HIGH);

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 2){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);
digitalWrite(6, HIGH);

}else if(i == 3){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 4){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 5){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}
}

void loop(){

digitalWrite(9,LOW);

digitalWrite(8, HIGH);

for(int i =5; i>=0;i--){

displayNumber(i);

delay(1000);}

delay(500);

digitalWrite(8, LOW);

digitalWrite(10, HIGH);

for(int i = 2; i >=0;i--){

displayNumber(i);

delay(1000);}

delay(500);

digitalWrite(10, LOW);

digitalWrite(9, HIGH);

for(int i = 3; i >=0 ; i--){

displayNumber(i);

delay(1000);}

delay(500);

4.6, 4.7

void setup(){

for (int i = 0; i < 14; i++) {

pinMode(i, OUTPUT);
}

void displayNumber(int i){

if (i == 0){

digitalWrite(0, HIGH);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 1){

digitalWrite(0, HIGH);

digitalWrite(1, HIGH);

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 2){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);
digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

}else if(i == 3){

digitalWrite(0, LOW);

digitalWrite(1, HIGH);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

}else if(i == 4){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

}else if(i == 5){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);
digitalWrite(6, LOW);

void loop(){

digitalWrite(8, HIGH);

digitalWrite(13, HIGH);

for(int i =4; i>=0;i--){

displayNumber(i);

delay(1000);

if(i == 2){

digitalWrite(13, LOW);

digitalWrite(12, HIGH);

}}

digitalWrite(8, LOW);

digitalWrite(12, LOW);

delay(1000);

digitalWrite(11, HIGH);

digitalWrite(10, HIGH);

for(int i =4; i>=0;i--){

displayNumber(i);

delay(1000);

if(i == 3){

digitalWrite(10, LOW);

digitalWrite(9, HIGH);

}}

digitalWrite(11, LOW);
digitalWrite(9, LOW);

4.8

int second = 0;

int minute = 0;

int hour = 0;

void setup()

for(int i = 0; i < 12; i++){

pinMode(i, OUTPUT);

void displayOnClock(int num){

if(num <= 12 && num>=1){

digitalWrite(num-1, HIGH);

//TODO: Implement your code here

void clearClock(){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);
digitalWrite(5, LOW);

digitalWrite(6, LOW);

digitalWrite(7, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, LOW);

digitalWrite(12, LOW);

//TODO: Clear 12 LEDs on Clock

void loop()

for(int i = 0; i < 12; i++){

displayOnClock(i + 1);

delay(1000);

clearClock();

delay(1000);

clearClock();

4.9

int second = 0;

int minute = 0;

int hour = 0;
void setup()

for(int i = 0; i < 12; i++){

pinMode(i, OUTPUT);

void displayOnClock(int num){

if(num <= 12 && num>=1){

digitalWrite(num-1, HIGH);

//TODO: Implement your code here

void clearClock(){

digitalWrite(0, LOW);

digitalWrite(1, LOW);

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

digitalWrite(6, LOW);

digitalWrite(7, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

digitalWrite(10, LOW);

digitalWrite(11, LOW);
digitalWrite(12, LOW);

//TODO: Clear 12 LEDs on Clock

void loop()

for(second = 0; second < 10 ; second++){

if(second >= 0 && second <= 4)

displayOnClock(12);

delay(500);

clearClock();

if(second >= 5 && second <=9){

displayOnClock(1);

delay(500);

clearClock();

4.10

int second = 0;

int minute = 0;
int hour = 0;

void setup()

for(int i = 0; i < 12; i++){

pinMode(i, OUTPUT);

void displayOnClock(int num){

if(num <= 12 ){

digitalWrite(num-1, HIGH);

//TODO: Implement your code here

void clearClock(){

for(int i = 0; i < 12; i++){

digitalWrite(i, LOW);

//TODO: Clear 12 LEDs on Clock

void loop()

minute=second/60;
hour=minute/60;

int secCount = second/5 - minute*12;

int minCount=minute/5-hour*12;

int hourCount = hour%12;

if (secCount == 0 ){secCount=12;}

if (minCount == 0 ){minCount=12;}

if (hourCount == 0 ){hourCount=12;}

clearClock();

displayOnClock(secCount);

displayOnClock(minCount);

displayOnClock(hourCount);

delay(1000);

second +=1;

You might also like