最傻的方法实现四位数码管0到9重复循环分开显示

直接上代码

int a = 6;
int b = 7;
int c = 8;
int d = 9;
int e = 10;
int f = 11;
int g = 12;


void setup() {
  // put your setup code here, to run once:

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);

  Serial.begin(9600);
}

int dd = 2;
int d1[4] = {1, 0, 0, 0};
int d2[4] = {0, 1, 0, 0};
int d3[4] = {0, 0, 1, 0};
int d4[4] = {0, 0, 0, 1};

int current = 0;
void loop() {
  // put your main code here, to run repeatedly:

  if (dd > 5) {
    dd = 2;
  }

if (current > 9) {
    current = 0;
  }

  Serial.println(dd);
  switch (dd) {
    case 2: open1(); break;
    case 3: open2(); break;
    case 4: open3(); break;
    case 5: open4(); break;
    default: open1(); break;
  }

  digitalWrite(6, HIGH);

  switch (current) {
    case 0: print0(); break;
    case 1: print1(); break;
    case 2: print2(); break;
    case 3: print3(); break;
    case 4: print4(); break;
    case 5: print5(); break;
    case 6: print6(); break;
    case 7: print7(); break;
    case 8: print8(); break;
    case 9: print9(); break;
    default: print0(); break;
  }
  current++;
  delay(1000);
  dd++;

}

void open1() {
  int b = sizeof(d1) / sizeof(d1[0]);
  int j = 2;
  for (int i = 0; i < b; i++) {
    //digitalWrite(d+i, d1[i]);
    digitalWrite(i + 2, d1[i]);
  }
}

void open2() {
  int b = sizeof(d2) / sizeof(d2[0]);
  for (int i = 0; i < b; i++) {
    digitalWrite(i + 2, d2[i]);
  }
}


void open3() {
  int b = sizeof(d3) / sizeof(d3[0]);
  for (int i = 0; i < b; i++) {
    digitalWrite(i + 2, d3[i]);
  }
}

void open4() {
  int b = sizeof(d4) / sizeof(d4[0]);
  for (int i = 0; i < b; i++) {
    digitalWrite(i + 2, d4[i]);
  }
}


void print0() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void print1() {
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void print2() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void print3() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
void print4() {
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void print5() {
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void print6() {
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void print7() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void print8() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void print9() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}






接线是控制四个数码管的引脚分别为2,3,4,5.控制abcdefg的引脚为6,7,8,9,10,11,12

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值