极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 904|回复: 0

如何使数字各别显示,麻烦路过的大佬帮忙

[复制链接]
发表于 2020-5-28 16:50:34 | 显示全部楼层 |阅读模式
下面代码是想用温湿度感测器侦测湿度,并用两颗七段显示器显示出来,不知道为什么十位数与个位数不能分开显示,请求大佬们帮忙下啦。
#include <DHT.h>
#define DHTPIN 2      
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);
int a = 9;
int b = 3;
int c = 4;
int d = 5;
int e = 12;
int f = 7;
int g = 8;
int GND1 = 10;
int GND2 = 13;
int dig1 = 0;
int dig2 = 0;

void setup()
{
  Serial.begin(9600);
  dht.begin();
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
}
void loop()
{
  int h = dht.readHumidity();  
  int t = dht.readTemperature();  
  //Serial.println(t);   
  Serial.println(h);
   
    dig1 = h / 10;//十位數
    dig2 = h - (dig1 * 10);//個位數
    Serial.println(dig1);
    Serial.println(dig2);
  
        digitalWrite( GND2, HIGH);
        digitalWrite( GND1,LOW);  
        DisplayNumber(dig2);//個位數
        delay(1000);
        digitalWrite( GND2,LOW);
        digitalWrite( GND1,HIGH);   
        DisplayNumber(dig1);//十位數
        delay(1000);
}

byte bcd_code[10][4] = {
  {0,0,0,0},
  {0,0,0,1},
  {0,0,1,0},
  {0,0,1,1},
  {0,1,0,0},
  {0,1,0,1},
  {0,1,1,0},
  {0,1,1,1},
  {1,0,0,0},
  {1,0,0,1}
};

void DisplayNumber(int x) {
  switch (x) {
    case 0:
    digitalWrite(7, bcd_code[0][0]);
    digitalWrite(5, bcd_code[0][1]);
    digitalWrite(4, bcd_code[0][2]);
    digitalWrite(3, bcd_code[0][3]);
    break;
    case 1:
    digitalWrite(7, bcd_code[1][0]);
    digitalWrite(5, bcd_code[1][1]);
    digitalWrite(4, bcd_code[1][2]);
    digitalWrite(3, bcd_code[1][3]);
    break;
    case 2:
    digitalWrite(7, bcd_code[2][0]);
    digitalWrite(5, bcd_code[2][1]);
    digitalWrite(4, bcd_code[2][2]);
    digitalWrite(3, bcd_code[2][3]);
    break;
    case 3:
    digitalWrite(7, bcd_code[3][0]);
    digitalWrite(5, bcd_code[3][1]);
    digitalWrite(4, bcd_code[3][2]);
    digitalWrite(3, bcd_code[3][3]);
    break;
    case 4:
    digitalWrite(7, bcd_code[4][0]);
    digitalWrite(5, bcd_code[4][1]);
    digitalWrite(4, bcd_code[4][2]);
    digitalWrite(3, bcd_code[4][3]);
    break;
    case 5:
    digitalWrite(7, bcd_code[5][0]);
    digitalWrite(5, bcd_code[5][1]);
    digitalWrite(4, bcd_code[5][2]);
    digitalWrite(3, bcd_code[5][3]);
    break;
    case 6:
    digitalWrite(7, bcd_code[6][0]);
    digitalWrite(5, bcd_code[6][1]);
    digitalWrite(4, bcd_code[6][2]);
    digitalWrite(3, bcd_code[6][3]);
    break;
    case 7:
    digitalWrite(7, bcd_code[7][0]);
    digitalWrite(5, bcd_code[7][1]);
    digitalWrite(4, bcd_code[7][2]);
    digitalWrite(3, bcd_code[7][3]);
    break;
    case 8:     
    digitalWrite(7, bcd_code[8][0]);
    digitalWrite(5, bcd_code[8][1]);
    digitalWrite(4, bcd_code[8][2]);
    digitalWrite(3, bcd_code[8][3]);
    break;
    case 9:     
    digitalWrite(7, bcd_code[9][0]);
    digitalWrite(5, bcd_code[9][1]);
    digitalWrite(4, bcd_code[9][2]);
    digitalWrite(3, bcd_code[9][3]);
    break;
   
  }
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2020-7-4 18:52 , Processed in 0.050242 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表