2021年8月23日 星期一

relay

引用網站:
https://openhome.cc/Gossip/CodeData/mBlockArduino/mBlockArduino15.html
mBlock & Arduino(15)認識電晶體與繼電器


Arduino 的 I/O 腳位可供給的電壓、電流有限,只能用於 LED 這類小元件,對於需要更高電壓或電流的設備,像是直流馬達、大型電器等就沒辦法負擔,對於小型直流馬達等元件,可以使用電晶體(Transistor)來供給較大量的電流,對於大型電器,可以透過繼電器(Relay)來控制。

認識電晶體

實際上,提到電晶體時,多半是指雙接面電晶體(Bipolar junction transistor, BJT),依組合的半導體材料,可以分為 NPN 與 PNP 兩大類,以 NPN 電晶體為例,是由 N 型半導體與 P 型半導體組合而成:

雙極性電晶體

電晶體有兩接面、三個端點,端點分別為集極(Collector)、基極(Base)與射極(Emitter),NPN 電晶體的基本特性是,射極電流 Ie 為基極電流 Ib 與集極電流 Ic 之和,而增益 α = Ic / Ie,β = Ic / Ib,Ib 通常遠小於 Ic。

當三個端點施加之偏壓不同時,電晶體會處於不同狀態:

  • B-E 為順向偏壓,且 B-C 為順向偏壓時,此時電晶體狀態處於飽和區(Saturation region),此時 C 與 E 可視為短路,依電路實際連接情況,Ic 會處於一個飽和電流值。
  • B-E 為逆向偏壓,且 B-C 為逆向偏壓時,此時電晶體狀態處於截止區(Cutoff region),此時 C 與 E 可視為開路,不過,實際上還是會有少量電流通過,這個現象稱為「漏電」(Leakage)。
  • B-E 為順向偏壓,且 B-C 為逆向偏壓時,此時電晶體狀態處於動作區(Active region),此時可用於信號放大。

各區實際的偏壓,以及 α、β 等值,需要查詢電晶體的規格書而定,如果想將電晶體作為開關使用,基本電路之一如下:

雙極性電晶體

如果 Vi 處於高電位狀態,那麼流經基極的電流 Ib 就是 (Vi - Vbe) / Rb,而流經集極的電流就是 Ic = β * Ib,因此,可以用 Rb 來控制流經 Rl 的電流。

舉個例子來說,以下的電路連接,可以讓我手邊的 2N3904 電晶體驅動一個直流小馬達(不過會很燙,不要通電太久…XD):

雙極性電晶體

你可以透過一個小程式來測試一下:

雙極性電晶體

執行程式的話,你就會看到直流馬達每轉動一秒就停一秒,如果你直接將直流馬達一端接到腳位 D8,一端接 GND,在腳位為高電位時,會因為電流不足,無法驅動直流小馬達。

如果想驅動 9V 或 12V 以上的馬達,可以如下:

雙極性電晶體

由於馬達是電力產生磁力,而磁力與馬達內部磁鐵作用而產生轉動,為了避免馬達停止供電瞬間而產生的反動電壓,會加上一個二極體加以保護。

(如果想運用電晶體提供更大的電流來驅動馬達,可以使用兩個或多個電晶體的組合,像是 達靈頓電晶體;有些 Arduino 相容板,像是 Motoduino,本身就內建了可驅動馬達的電路,想做馬達驅動相關產品時很方便。)

認識繼電器

如果要控制電力需求更大的電器,那麼可以使用繼電器,若是常見的電磁式繼電器,其運作原理可使用下圖來表示:

繼電器

當腳位 D8 送出高電位時,電晶體會導通而使得線圈通過電流,因而產生磁力將開關吸合,因而導通電路,腳位 D8 送出低電位時,電流停止通過線圈,磁力消失而開關回到原位,為了避免反動電壓,會加上一個二極體保護。

市面上有一些繼電器模組,本身已經將相關電阻、二極體與電晶體兜好,例如我手邊這顆 Keyes_SRly,還有 LED 顯示繼電器是否作動:

繼電器

如果你買的繼電器,只是上頭藍色的元件,那麼就必須如先前介紹,自行將相關電阻、二極體與電晶體兜好,如果是繼電器模組,就只可以直接使用。

首先要注意的是繼電器本身的規格,例如上頭 SRD-05VDC-SL-C 這個繼電器,適用於 10A 的 250V 與 125V 交流電源,以及 10A 的 30V 與 28V 直流電源。

繼電器模組上有三個接點,中間那個是共用接點(COMMON),相當於先前概要圖中的第 2 個接點,NO 表示常開(Normal Open),也就是平時與共用接點保持開路,相當於先前概要圖中第 3 個接點,NC 表示常關(Normal Close),表示平時與共用接點處於接通狀態。

繼電器的接腳有三個,也就是會有 Vcc、GND 以及開關三個接腳,為了安全起見,這邊還是使用繼電器控制直流小馬達來試試看:

繼電器

同樣只要使用上頭撰寫的小程式,測試看看直流小馬達是否如預期般運作(你還可以聽見繼電器吸合時滴答滴答的聲音)。

2021年8月22日 星期日

功能:一開機pin10顯示0hz,7段顯示器顯示0 再壓一下顯示250hz,7段顯示器顯示1 再壓一下顯示500hz,7段顯示器顯示2 再壓一下顯示1khz,7段顯示器顯示3 再壓一下顯示2khz,7段顯示器顯示4 再壓一下顯示4khz,7段顯示器顯示5

 /*

 功能:一開機pin10顯示0hz,7段顯示器顯示0

      再壓一下顯示250hz,7段顯示器顯示1 

      再壓一下顯示500hz,7段顯示器顯示2

      再壓一下顯示1khz,7段顯示器顯示3

      再壓一下顯示2khz,7段顯示器顯示4

      再壓一下顯示4khz,7段顯示器顯示5

 */

unsigned int seg7_pin[]={2,3,4,5,6,7,8};

unsigned char seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};

int buzzer=10;

int speed0=0;//0hz 一開機

int speed1=2000;//0.25khz  1/(2000+2000)u

int speed2=1000;//0.5khz 1/(1000+1000)u

int speed3=500;//1khz 1/(500+500)u

int speed4=250;//2khz 1/(250+250)u

int speed5=125;//4khz 1/(125+125)u

int btn1=9;

int num_val;

int num=-1;

void setup()

{

  Serial.begin(9600);

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

  {

    pinMode(seg7_pin[i],OUTPUT);

  }

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

  {

    digitalWrite(seg7_pin[i],LOW);

  }

 pinMode(btn1,INPUT);

 pinMode(buzzer,OUTPUT);

}

//--------------------------

void outport(byte data)

{

  for(int j=0;j<7;j++)

  {

    if(data %2==1)

     digitalWrite(seg7_pin[j],HIGH);

    else

     digitalWrite(seg7_pin[j],LOW);

    data=data/2;

  }

}

void loop()

{

 num_val=digitalRead(btn1);

 if(num_val==LOW)

 {

  delay(1000);

  while(digitalRead(btn1)==LOW);

  if(num==5)//012345,index執行到5(5 already執行過了)就歸0

   num=0;

  else

   num++;

 }

 if(num==0)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed0);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed0);

   outport(seg7_table[0]);

   Serial.println(speed0);

 }

  if(num==1)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed1);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed1);

   outport(seg7_table[1]);

   Serial.println(speed1);

 }

 if(num==2)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed2);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed2);

   outport(seg7_table[2]);

   Serial.println(speed2);

 }

 if(num==3)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed3);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed3);

   outport(seg7_table[3]);

   Serial.println(speed3);

 }

 if(num==4)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed4);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed4);

   outport(seg7_table[4]);

   Serial.println(speed4);

 }

 if(num==5)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed5);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed5);

   outport(seg7_table[5]);

   Serial.println(speed5);

 }

}

RGB 加按鈕 0 1 2,再執行上面num=2就歸零

 int rled_pin = 3;

int gled_pin = 4;

int bled_pin = 5;

int btn1 = 2;

int num_val;

int num = -1;

void setup()

{

  Serial.begin(9600);

  pinMode(btn1, INPUT);

}


void loop()

{

  num_val = digitalRead(btn1);

  if (num_val == LOW)

  {

    delay(1000);

    while (digitalRead(btn1) == LOW);

    if (num ==2)

    {

      num = 0;//此處是一個等號 

      Serial.print("在num=3的後面num="); Serial.println(num);

    }

    else

    {

      num++;//他會執行0 1 2,再執行上面num=2就歸零

      Serial.print("'在else 後的num="); Serial.println(num);

    }

  }

  if (num == 0)

  {

    analogWrite(rled_pin, 255);

    analogWrite(gled_pin, 0);

    analogWrite(bled_pin, 0);

  }

  else if (num == 1)

  {

    analogWrite(rled_pin, 0);

    analogWrite(gled_pin, 255);

    analogWrite(bled_pin, 0);

  }

  else if (num == 2)

  {

    analogWrite(rled_pin, 0);

    analogWrite(gled_pin, 0);

    analogWrite(bled_pin, 255);

  }

}

2021年8月21日 星期六

按一下奇數跑(由左往右) 再按一下(由右往左) 每次都-2

 int led_pin[] = {2, 3, 4, 5, 6, 7, 8, 9};

int j = -2;

int direct = 0;

int btn_pin = 10;

int val = 1;

int r=9;

void setup()

{

  Serial.begin(9600);

  pinMode(btn_pin, INPUT);

  for (int i = 0; i < 8; i++)

  {

    pinMode(led_pin[i], OUTPUT);

  }

}


void loop()

{

  for (int k = 0; k < 8; k++)

  {

    digitalWrite(led_pin[k], LOW);

  }

  val = digitalRead(btn_pin);//按下去沒反應可能電位有問題,用邏輯探測棒debug

  if (val == 0)

  {

    val = digitalRead(btn_pin);

    delay(1000);

    while (digitalRead(btn_pin) == LOW);

    direct = !direct;

  }

  if (direct == 0)//由左往右跑每次+2

  {

     if (j == 6)

      j = 0;

    else

      j = j + 2;

      // 0 1 2 3 4 5 6 7

    if(j==1) j=2;//如果奇數切過來是1就變偶數2(index)

    if(j==3) j=4;//如果奇數切過來是3就變偶數4(index)

    if(j==5) j=6;//如果奇數切過來是5就變偶數6(index)

    if(j==7) j=0;//如果奇數切過來是7就變偶數0(index)

    digitalWrite(led_pin[j], HIGH);

    delay(1000);

  }

  else

  {

     if (j == 1)//由右往左跑,每次-2

      j = 7;

    else

      j = j - 2;

      // 0 1 2 3 4 5 6 7

    if(j==0) j=7;//如果偶數切過來是0就變奇數7(index)

    if(j==2) j=1;//如果偶數切過來是2就變奇數1(index)

    if(j==4) j=3;//如果偶數切過來是4就變奇數3(index)

    if(j==6) j=5;//如果偶數切過來是6就變奇數5(index)

    digitalWrite(led_pin[j], HIGH);

    delay(1000);

  }

}

按下去沒反應可能電位有問題,用邏輯探測棒debug 按一下1357跑 再按一下8642跑

 int led_pin[] = {2, 3, 4, 5, 6, 7, 8, 9};

int j = -2;

int direct = 0;

int btn_pin = 10;

int val = 1;

int r=9;

void setup()

{

  Serial.begin(9600);

  pinMode(btn_pin, INPUT);

  for (int i = 0; i < 8; i++)

  {

    pinMode(led_pin[i], OUTPUT);

  }

}


void loop()

{

  for (int k = 0; k < 8; k++)

  {

    digitalWrite(led_pin[k], LOW);

  }

  val = digitalRead(btn_pin);//按下去沒反應可能電位有問題,用邏輯探測棒debug

  if (val == 0)

  {

    val = digitalRead(btn_pin);

    delay(1000);

    while (digitalRead(btn_pin) == LOW);

    direct = !direct;

  }

  if (direct == 0)//由左往右跑每次+2

  {

     if (L == 6)

      L = 0;

    else

      L = L + 2;

    digitalWrite(led_pin[L], HIGH);

    delay(1000);

  }

  else

  {

     if (r == 1)//由右往左跑,每次-2

      r = 7;

    else

      r = r - 2;

    digitalWrite(led_pin[r], HIGH);

    delay(1000);

  }

}

AMA 中級

 int in1_pin = 10;

int in2_pin = 11;

int buzzer_pin = 12;

int key1_pin = A0;

int key2_pin = A1;

int key3_pin = A2;

int seg7_pin[] = {9, 8, 7, 6, 5, 4, 3, 2};

int seg7_table[]={~0x8E,~0x92,~0x83};

int buzzer = 12;

int mode=10;

void setup()

{

 pinMode(buzzer_pin,OUTPUT);

 pinMode(in1_pin,OUTPUT);

 pinMode(in2_pin,OUTPUT);

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

 {

  pinMode(seg7_pin[i],OUTPUT);

 }

}

//--------------------------------

void outport(byte data)

{

  for(int j=0;j<7;j++)

  {

    if(data%2==1)

     digitalWrite(seg7_pin[j],HIGH);

    else

     digitalWrite(seg7_pin[j],LOW);

    data=data/2;

  }

}

void loop()

{

 if(digitalRead(key1_pin)==0 && mode!=1)

 {

  mode=1;

  outport(seg7_table[0]);

  digitalWrite(in1_pin,HIGH);

  digitalWrite(in2_pin,LOW);

  tone(buzzer,500,300);

 }

 else if(digitalRead(key2_pin)==0 && mode!=2) 

 {

  mode=2;

  outport(seg7_table[1]);

  digitalWrite(in1_pin,LOW);

  digitalWrite(in2_pin,LOW);

  tone(buzzer_pin,500,1000);

 }

 else if(digitalRead(key3_pin)==0 && mode!=3) 

 {

  mode=3;

  outport(seg7_table[2]);

  digitalWrite(in1_pin,LOW);

  digitalWrite(in2_pin,HIGH);

  tone(buzzer_pin,500,300); delay(400);

  tone(buzzer_pin,500,300);

 } 

}

2021年8月20日 星期五

//按指撥開關0000 001顯示1, //按指撥開關10000 001顯示,超過120顯示Err



 unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int weight[] = {64, 32, 16, 8, 4, 2, 1};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99,

                               ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90,

                               ~0x86, ~0xAF

                              };

unsigned char scan_line_pin[] = {9, 10, 11, 12}; //掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int dip_sw[] = {13, 14, 15, 16, 17, 18, 19};

int num_val;

int cnt_val = 0;

int value;

unsigned int number;

int i;

int j;


void setup()

{

  Serial.begin(9600);

  for (int g = 0; g < 5; g++)

  {

    pinMode(dip_sw[g], INPUT);

  }

  for (int m = 0; m < 4; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  //num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  number = 0;

  for (int ii = 0; ii < 7; ii++)//7個dip,如果數字用錯會出現亂碼

  {

    value = digitalRead(dip_sw[ii]);

    if (value == HIGH)

    {

      number = number + weight[ii];

      if (number > 120)

      {

        for (int s = 0; s < 10; s++)//此數字要去測,外圈掃描個10次

        {

          for (int q = 0; q < 50; q++)//內圈掃瞄個50次

          {

            digitalWrite(scan_line_pin[2], HIGH); outport(seg7_table[10]);

            //Serial.print("seg7_table[10]="); Serial.println(seg7_table[10]);善用Serial除錯

            delay(1); digitalWrite(scan_line_pin[2], LOW);

            digitalWrite(scan_line_pin[1], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[1], LOW);

            digitalWrite(scan_line_pin[0], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[0], LOW);

          }

        }

      }

    }

  }

  //Serial.print("number="); Serial.println(number); delay(1000);

  for ( i = 0; i < 4; i++) //scan從index=0  9 10 11 12

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[number % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

      //      Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間

      //      Serial.println(num_val);

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(1);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    number = number / 10;//取餘數完再除10

  }

}

test

 unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int weight[] = {64, 32, 16, 8, 4, 2, 1};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99,

                               ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90,

                               ~0x86, ~0xAF

                              };

unsigned char scan_line_pin[] = {9, 10, 11, 12}; //掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int dip_sw[] = {13, 14, 15, 16, 17, 18, 19};

int num_val;

int cnt_val = 0;

int value;

unsigned int number;

int i;

int j;


void setup()

{

  Serial.begin(9600);

  for (int g = 0; g < 5; g++)

  {

    pinMode(dip_sw[g], INPUT);

  }

  for (int m = 0; m < 4; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  //num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  number = 0;

  for (int ii = 0; ii < 7; ii++)//7個dip,如果數字用錯會出現亂碼

  {

    value = digitalRead(dip_sw[ii]);

    if (value == HIGH)

    {

      number = number + weight[ii];

      if (number > 120)

      {

        for (int s = 0; s < 10; s++)//此數字要去測,外圈掃描個10次

        {

          for (int q = 0; q < 50; q++)//內圈掃瞄個50次

          {

            digitalWrite(scan_line_pin[2], HIGH); outport(seg7_table[10]);

            //Serial.print("seg7_table[10]="); Serial.println(seg7_table[10]);善用Serial除錯

            delay(1); digitalWrite(scan_line_pin[2], LOW);

            digitalWrite(scan_line_pin[1], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[1], LOW);

            digitalWrite(scan_line_pin[0], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[0], LOW);

          }

        }

      }

    }

  }

  //Serial.print("number="); Serial.println(number); delay(1000);

  for ( i = 0; i < 4; i++) //scan從index=0  9 10 11 12

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[number % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

      //      Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間

      //      Serial.println(num_val);

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(1);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    number = number / 10;//取餘數完再除10

  }

}

按指撥開關0000 001顯示1,按指撥開關10000 001顯示,超過120顯示Err

//按指撥開關0000 001顯示1,

//按指撥開關10000 001顯示,超過120顯示Err

 unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int weight[] = {64, 32, 16, 8, 4, 2, 1};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99,

                               ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90,

                               ~0x86, ~0xAF

                              };

unsigned char scan_line_pin[] = {9, 10, 11, 12}; //掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int dip_sw[] = {13, 14, 15, 16, 17, 18, 19};

int num_val;

int cnt_val = 0;

int value;

unsigned int number;

int i;

int j;


void setup()

{

  Serial.begin(9600);

  for (int g = 0; g < 5; g++)

  {

    pinMode(dip_sw[g], INPUT);

  }

  for (int m = 0; m < 4; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  //num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  number = 0;

  for (int ii = 0; ii < 7; ii++)//7個dip,如果數字用錯會出現亂碼

  {

    value = digitalRead(dip_sw[ii]);

    if (value == HIGH)

    {

      number = number + weight[ii];

      if (number > 120)

      {

        for (int s = 0; s < 10; s++)//此數字要去測,外圈掃描個10次

        {

          for (int q = 0; q < 50; q++)//內圈掃瞄個50次

          {

            digitalWrite(scan_line_pin[2], HIGH); outport(seg7_table[10]);

            //Serial.print("seg7_table[10]="); Serial.println(seg7_table[10]);善用Serial除錯

            delay(1); digitalWrite(scan_line_pin[2], LOW);

            digitalWrite(scan_line_pin[1], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[1], LOW);

            digitalWrite(scan_line_pin[0], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[0], LOW);

          }

        }

      }

    }

  }

  //Serial.print("number="); Serial.println(number); delay(1000);

  for ( i = 0; i < 4; i++) //scan從index=0  9 10 11 12

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[number % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

      //      Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間

      //      Serial.println(num_val);

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(1);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    number = number / 10;//取餘數完再除10

  }

}

dip_sw 撥0000001顯示1 撥1000000顯示127

 unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int weight[] = {64,32,16, 8, 4, 2, 1};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99,

                               ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90,

                               ~0x86, ~0xAF

                              };

unsigned char scan_line_pin[] = {9, 10, 11, 12}; //掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int dip_sw[] = {13,14,15,16,17,18,19};

int num_val;

int cnt_val = 0;

int value;

unsigned int number;

int i;

int j;


void setup()

{

  Serial.begin(9600);

  for (int g = 0; g < 5; g++)

  {

    pinMode(dip_sw[g], INPUT);

  }

  for (int m = 0; m < 4; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  //num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  number = 0;

  for (int ii = 0; ii < 7; ii++)//7個dip,如果數字用錯會出現亂碼

  {

    value = digitalRead(dip_sw[ii]);

    if (value == HIGH)

    {

      number = number + weight[ii];

    }

    //Serial.print("number="); Serial.println(number); delay(1000);

  }

  for ( i = 0; i <4; i++)//scan從index=0  9 10 11 12 

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[number % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

      //      Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間

      //      Serial.println(num_val);

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(1);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    number = number / 10;//取餘數完再除10

  }

}

功能:按鈕1按一下上數,按一下暫停,再按一下繼續上數,數到200就暫停 按鈕2按一下下數,按一下暫停,再按一下下數到0就暫停

 //功能:按鈕1按一下上數,按一下暫停,再按一下繼續上數,數到200就暫停

unsigned long time1 = 0;

unsigned long time2 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

unsigned char scan_line_pin[] = {9, 10, 11}; //掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int btn_pin1 = 12;//上數按鈕

int btn_pin2 = 13;//下數按鈕

int btn_status1 = 0; //一開機 0上數 1暫停

int btn_status2=9;//10暫停  11下數

int cnt_val = 0;

int num_val;

int chg_up_low;

int i;

int j;

int read_status1;

int read_status2;

void setup()

{

  Serial.begin(9600);

  pinMode(btn_pin1, INPUT);

  pinMode(btn_pin2, INPUT);

  for (int m = 0; m < 3; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  read_status1 = digitalRead(btn_pin1);//寫這行一定要加btn鍵的線,否則不會亮

  //Serial.print("read_status1=");Serial.println(read_status1);除錯用看read_status1是否進來

  if (read_status1 == LOW)

  {

    delay(1000);

    while (digitalRead(btn_pin1) == LOW);

    chg_up_low=0;

    btn_status1++ ; //每按一下0+1=1,就暫停,再按1下就2歸0

    if (btn_status1 == 2)

      btn_status1 = 0;      

  }

 //--------------------------------------------

  read_status2 = digitalRead(btn_pin2);

  if (read_status2 == LOW )

  {

    delay(1000);

    Serial.print("read_status2=");Serial.println(read_status2);//除錯用看read_status2是否進來

    while (digitalRead(btn_pin2) == LOW);// 

    chg_up_low=1; 

    //Serial.print("chg_up_low=");Serial.println(chg_up_low);//除錯用看chg_up_low是否進來

    //delay(5000);因為跑太快了,所以delay 5秒中

    btn_status2++ ; //每按一下9+1=10,就暫停,再按一下11就歸10

    if (btn_status2 == 12)

      btn_status2 = 10;      

  }

  num_val = cnt_val;//因為每次數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  for ( i = 0; i < 3; i++) //scan從index=1為第二個數開始掃描,9(右1) 10(右2)

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[num_val % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(2);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    num_val = num_val / 10;//取餘數完再除10

    if (millis() - time1 >= 1000)//此處不能寫delay(1000)取代

    {

      time1 = millis();

      if (btn_status1 == 1  && chg_up_low==0)

      {

        //Serial.println(btn_status);//Serial用來偵錯用

        ;//按一下,我什麼都不做,再按一下又繼續exe

      }


      else if (btn_status1 == 0 && chg_up_low==0) 

      {

        Serial.print("btn_status1=");Serial.println(btn_status1);

        Serial.print("chg_up_low=");Serial.println(chg_up_low);

        if (cnt_val == 200)//數到200就停在200

          cnt_val = 200;

        else

          cnt_val++;

      }

      if (millis() - time2 >= 1000)//此處不能寫delay(1000)取代

      {

        time2 = millis();

        if (btn_status2 == 10  && chg_up_low==1)

        {

          Serial.print("btn_status2=");Serial.println(btn_status2);//Serial用來偵錯用

          Serial.println("chg_up_low=");Serial.println(chg_up_low);

          ;//按一下,我什麼都不做,再按一下又繼續exe

        }


        else if (btn_status2 == 11  && chg_up_low==1)

        {

          Serial.println(cnt_val);

          Serial.println(chg_up_low);

          if (cnt_val == 0)//數到0就停在0

            cnt_val = 0;

          else

            cnt_val--;

        }

      }

    }

  }

}

2021年8月19日 星期四

//功能:按鈕1按一下上數,按一下暫停,再按一下繼續上數,數到200就暫停

 //功能:按鈕1按一下上數,按一下暫停,再按一下繼續上數,數到200就暫停

unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

unsigned char scan_line_pin[] = {9, 10,11};//掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int btn_pin = 13;

int btn_status = 0; //

int num_val;


int cnt_val = 0;

int i;

int j;

int read_status;

void setup()

{

  Serial.begin(9600);

  pinMode(btn_pin, INPUT);

  for (int m = 0; m < 3; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  read_status = digitalRead(btn_pin);//寫這行一定要加btn鍵的線,否則不會亮

  if (read_status == LOW)

  {

    delay(1000);

    while (digitalRead(btn_pin) == LOW);

    if (btn_status == 1)

      btn_status = 0;

    else

      btn_status++ ; //每按一下,就暫停

  }

  num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  for ( i = 0; i < 3; i++) //scan從index=1為第二個數開始掃描,9(右1) 10(右2)

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[num_val % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(2);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    num_val = num_val / 10;//取餘數完再除10

    if (millis() - time1 >= 1000)//此處不能寫delay(1000)取代

    {

      time1 = millis();

      if (btn_status == 1)

      {

        //Serial.println(btn_status);//Serial用來偵錯用

        ;//按一下,我什麼都不做,再按一下又繼續exe

      }


      else if (btn_status == 0)

      {

        //Serial.println(btn_status);

        if (cnt_val == 200)//數到200就停在200

          cnt_val = 200;

        else

          cnt_val++;

      }

    }

  }

}

計數到9暫停

 unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

int btn_pin=9;

int read_status;

int num_val;

void setup()

{

  pinMode(btn_pin,INPUT);

  for (int i = 0; i < 8; i++)

  {

    pinMode(seg7_pin[i], OUTPUT);

    digitalWrite(seg7_pin[i], LOW);

  }

}

//---------------------------------------

void outport(byte data)

{

  for (int j = 0; j < 7; j++)

  {

    if (data % 2 == 1)

      digitalWrite(seg7_pin[j], HIGH);

    else

      digitalWrite(seg7_pin[j], LOW);

    data=data/2;

    }

}

void loop()

{

//  read_status=digitalRead(btn_pin);

//  if(read_status==LOW)

//  {

//    delay(100);

//    while(digitalRead(btn_pin)==LOW);

//    num_val++;

//  }

  if(millis()-time1>=2000)

  {

    time1=millis();

    if(num_val==9)

     num_val=9;//計數到9就暫停

    else

    num_val++;

  }

  outport(seg7_table[num_val]);

}


右2順時針 每按一下順變逆,逆變順

 unsigned long time1 = 0;

unsigned long time2 = 0;

int btn_pin=13;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

char seg7_table[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xff};

//                   亮a  亮b  亮c  亮d  亮e  亮f

int scan_line[] = {9, 10};

char bcd[] = {6, 6 }; //一開機,先清除畫面

int i, j = 0, cnt;

int read_status;//讀按鈕是高電位還是低電位

int odd_even=0;//每切換一次奇變偶,偶變奇

//-----------------------------------------------------------------------------

void setup()

{

  pinMode(btn_pin,INPUT);

  for (i = 0; i < 8; i++)

    pinMode(seg7_pin[i], OUTPUT);//資料端

  for (i = 0; i < 4; i++)

    pinMode(scan_line[i], OUTPUT);//掃瞄端

}

//-------------------------------------------------------------------------------

void outport(byte data)

{

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

    if (data % 2 == 1) // 取出 data 的最低位元

      digitalWrite(seg7_pin[j], LOW);

    else

      digitalWrite(seg7_pin[j], HIGH);

    data = data / 2; // 除2,進行下一位元的處理

  }

}

//-----------------------------------------------------------------------------

void loop()

{

  read_status=digitalRead(btn_pin);

  if(read_status==LOW)

  {

    delay(50);

    while(digitalRead(btn_pin)==LOW);

     odd_even++;

  }

  if (millis() - time1 > 5) //超過0.05秒掃瞄scan_line及秀字

  {

    outport(seg7_table[bcd[j]]);

    digitalWrite(scan_line[j], HIGH);//exe j=0

    delay(1);

    digitalWrite(scan_line[j], LOW);

    if (j == 1)//scan_line[0]第9支腳 scan_line[1]第10支腳

      j = 0;//掃瞄是從右到左

    else

      j++;//會先執行j=0(scan_line com0) j=1(0+1=1再執行com1)又在exe scan_line=0

  }

  if (millis() - time2 > 1000) //超過1秒,計數值加1

  {

    time2 = millis();

    if(odd_even%2==0)

    {

      if (cnt== 7)

       cnt=0;

      else

       cnt++;

    }

    else

    {

      if (cnt==0)

       cnt=7;

      else

       cnt--;

    }

  }//兩個七段幾乎同時顯示,因為速度很快, 使用一維陣列,總共要8次變化

  if (cnt == 0)  { bcd[1] = 0;  bcd[0] = 6 ; }//a 亮(十位數)  a不亮(個位數)  因為點亮是從右2到右1(順時針點亮)所以index=1先寫,index=0後寫

  if (cnt == 1)  { bcd[1] = 6;  bcd[0] = 0 ; }

  if (cnt == 2)  { bcd[1] = 6;  bcd[0] = 1 ; }

  if (cnt == 3)  { bcd[1] = 6;  bcd[0] = 2 ; }

  if (cnt == 4)  { bcd[1] = 6;  bcd[0] = 3 ; }

  if (cnt == 5)  { bcd[1] = 3;  bcd[0] = 6 ; }

  if (cnt == 6)  { bcd[1] = 4;  bcd[0] = 6 ; }

  if (cnt == 7)  { bcd[1] = 5;  bcd[0] = 6 ; }

}

外接網卡 內接網卡

 




右2跑外圍aabcddef依續點亮

 unsigned long time1 = 0;

unsigned long time2 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

char seg7_table[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xff};

//                  亮a  亮b  亮c  亮d  亮e  亮f

int scan_line[] = {9, 10};

char bcd[] = {6, 6, 6, 6}; //一開機,先清除畫面

int i, j = 0, cnt;

//-----------------------------------------------------------------------------

void setup()

{

  for (i = 0; i < 8; i++)

    pinMode(seg7_pin[i], OUTPUT);//資料端

  for (i = 0; i < 4; i++)

    pinMode(scan_line[i], OUTPUT);//掃瞄端

}

//-------------------------------------------------------------------------------

void outport(byte data)

{

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

    if (data % 2 == 1) // 取出 data 的最低位元

      digitalWrite(seg7_pin[j], LOW);

    else

      digitalWrite(seg7_pin[j], HIGH);

    data = data / 2; // 除2,進行下一位元的處理

  }

}

//-----------------------------------------------------------------------------

void loop()

{

  if (millis() - time1 > 5) //超過0.05秒掃瞄scan_line及秀字

  {

    outport(seg7_table[bcd[j]]);

    digitalWrite(scan_line[j], HIGH);

    delay(1);

    digitalWrite(scan_line[j], LOW);

    if (j == 2)//scan_line[0]第9支腳 scan_line[1]第10支腳

      j = 0;//掃瞄是從右到左

    else

      j++;

  }

  if (millis() - time2 > 1000) //超過1秒,計數值加1

  {

    time2 = millis();

    cnt++;

    if (cnt > 7)

      cnt = 0;

  }//兩個七段幾乎同時顯示,因為速度很快

  if (cnt == 0)  { bcd[1] = 0;  bcd[0] = 6 ; }//a 亮(十位數)  a不亮(個位數)  因為點亮是從右2到右1所以index=1先寫,index=0後寫

  if (cnt == 1)  { bcd[1] = 6;  bcd[0] = 0 ; }

  if (cnt == 2)  { bcd[1] = 6;  bcd[0] = 1 ; }

  if (cnt == 3)  { bcd[1] = 6;  bcd[0] = 2 ; }

  if (cnt == 4)  { bcd[1] = 6;  bcd[0] = 3 ; }

  if (cnt == 5)  { bcd[1] = 3;  bcd[0] = 6 ; }

  if (cnt == 6)  { bcd[1] = 4;  bcd[0] = 6 ; }

  if (cnt == 7)  { bcd[1] = 5;  bcd[0] = 6 ; }

}

2021年8月18日 星期三

00~-99 btn_pin 壓一下上數 再壓一下下數

unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

unsigned char scan_line_pin[] = {11, 12};//掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int btn_pin = 13;

int odd_even = 0; //0一開機為上數,1一開機為下數

int num_val;

int cnt_val = 0;

int i;

int j;

int read_status;


void setup()

{

  pinMode(btn_pin, INPUT);

  for (int m = 0; m < 4; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  read_status = digitalRead(btn_pin);

  if (read_status == LOW)

  {

    delay(50);

    while (digitalRead(btn_pin) == LOW);

    odd_even++;//每按一下,由奇變偶,或偶變奇

  }

  num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  for ( i = 1; i >= 0; i--)//scan從index=1為第二個數開始掃描,12(右1) 11(右2) 10(左2) 9(左1)

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[num_val % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

      //      Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間

      //      Serial.println(num_val);

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(2);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    num_val = num_val / 10;//取餘數完再除10

    if (millis() - time1 >= 1000)//此處不能寫delay(1000)取代

    {

      time1 = millis();

      if (odd_even % 2 == 0)

      {

        if (cnt_val == 99)

          cnt_val = 0;

        else

          cnt_val++;

      }

      else

      {

        if (cnt_val == 0)

          cnt_val = 99;

        else

          cnt_val--;

      }

    }

  }

}

0000~9999四位七段顯示器

 unsigned long time1 = 0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

unsigned char  seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

unsigned char scan_line_pin[] = {9, 10, 11, 12};//掃描線從com0個位數開始掃描

//3 2 1 0所以每認-1即依續scan個十百千

int num_val;

int cnt_val = 0;

int i;

int j;

void setup()

{

  Serial.begin(9600);

  for (int m = 0; m < 4; m++)

  {

    pinMode(scan_line_pin[m], OUTPUT);

    //digitalWrite(scan_line_pin[m], LOW);

  }

  for (int s = 0; s < 7; s++)

  {

    pinMode(seg7_pin[s], OUTPUT);

    digitalWrite(seg7_pin[s], LOW);

  }

}

//----------------------------------

void outport(byte data)//呼叫個別數字副程式

{

  for (int k = 0; k < 7; k++)

  {

    if (data % 2 == 1)//整數部分連除法,從a開始取到g

      digitalWrite(seg7_pin[k], HIGH);

    else

      digitalWrite(seg7_pin[k], LOW);

    data = data / 2;//每除完一次除2

  }

}

void loop()

{

  num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值

  for ( i = 3; i >= 0; i--)//scan從index=3開始掃描,12(右1) 11(右2) 10(左2) 9(左1) 

  {

    for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位

    {

      outport(seg7_table[num_val % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,

//      Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間

//      Serial.println(num_val);

    }//即個位數的com0高電位,送字

    digitalWrite(scan_line_pin[i], HIGH); delay(2);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字

    digitalWrite(scan_line_pin[i], LOW);//在把個十百千

    num_val = num_val / 10;//取餘數完再除10

    if (millis() - time1 >= 20)//時間到就進來處理

    {

      time1 = millis();

      if (cnt_val == 9999)

        cnt_val = 0;

      else

        cnt_val++;

    }

  }

}

7seg 繞圓圈圈,一開機順時針慢轉1秒 壓一下逆時針快轉0.3秒 L(13pin都要blinking)

 unsigned long time1=0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int seg7_table[] = {~0xfe, ~0xfd, ~0xfb, ~0xf7, ~0xef, ~0xdf};

int led_pin=13;

int delaytime = 1000;

int btn_pin = 9;

int num_val;

int cnt_val;

int even_odd;

void setup()

{

  pinMode(led_pin,OUTPUT);

  pinMode(btn_pin, INPUT);

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

  {

    pinMode(seg7_pin[i], OUTPUT);

    digitalWrite(seg7_pin[i], LOW);

  }

}

//-------------------------------

void outport(byte data)

{

  for (int j = 0; j < 6; j++)

  {

    if (data % 2 == 1)

      digitalWrite(seg7_pin[j], HIGH);

    else

      digitalWrite(seg7_pin[j], LOW);

    data = data / 2;

  }

}

void loop() 

{

  if(millis()-time1>500)

  {

    time1=millis();

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

    digitalWrite(led_pin,LOW);delay(1000);

  }

  num_val = digitalRead(btn_pin);

  if (num_val == 0)

  {

    while (digitalRead(btn_pin) == LOW)

    {

      if (cnt_val == 0)

        cnt_val = 5;

      else

        cnt_val--;

      outport(seg7_table[cnt_val]);

      delay(300);

    }

  }

  else

  {

    if (cnt_val == 5)

      cnt_val = 0;

    else

      cnt_val++;

    outport(seg7_table[cnt_val]);

    delay(1000);

  }

}

一開機上數1秒 按一下按著不放下數0.3秒,放開又上數

 unsigned long time1=0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

int led_pin=13;

int delaytime = 1000;

int btn_pin = 9;

int num_val;

int cnt_val;

int even_odd;

void setup()

{

  pinMode(led_pin,OUTPUT);

  pinMode(btn_pin, INPUT);

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

  {

    pinMode(seg7_pin[i], OUTPUT);

    digitalWrite(seg7_pin[i], LOW);

  }

}

//-------------------------------

void outport(byte data)

{

  for (int j = 0; j < 7; j++)

  {

    if (data % 2 == 1)

      digitalWrite(seg7_pin[j], HIGH);

    else

      digitalWrite(seg7_pin[j], LOW);

    data = data / 2;

  }

}

void loop() 

{

  if(millis()-time1>500)

  {

    time1=millis();

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

    digitalWrite(led_pin,LOW);delay(1000);

  }

  num_val = digitalRead(btn_pin);

  if (num_val == 0)

  {

    while (digitalRead(btn_pin) == LOW)

    {

      if (cnt_val == 0)

        cnt_val = 9;

      else

        cnt_val--;

      outport(seg7_table[cnt_val]);

      delay(300);

    }

  }

  else

  {

    if (cnt_val == 9)

      cnt_val = 0;

    else

      cnt_val++;

    outport(seg7_table[cnt_val]);

    delay(1000);

  }

}

btn + up/down + L(13 pin)亮暗

 unsigned long time1=0;

int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

int led_pin=13;

int delaytime = 1000;

int btn_pin = 9;

int num_val;

int cnt_val;

int even_odd;

void setup()

{

  pinMode(led_pin,OUTPUT);

  pinMode(btn_pin, INPUT);

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

  {

    pinMode(seg7_pin[i], OUTPUT);

    digitalWrite(seg7_pin[i], LOW);

  }

}

//-------------------------------

void outport(byte data)

{

  for (int j = 0; j < 7; j++)

  {

    if (data % 2 == 1)

      digitalWrite(seg7_pin[j], HIGH);

    else

      digitalWrite(seg7_pin[j], LOW);

    data = data / 2;

  }

}

void loop() 

{

  if(millis()-time1>500)

  {

    time1=millis();

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

    digitalWrite(led_pin,LOW);delay(1000);

  }

  num_val = digitalRead(btn_pin);

  if (num_val == 0)

  {

    while (digitalRead(btn_pin) == LOW)

    {

      even_odd++;

    }

  }

  if (even_odd % 2 == 0)

  {

    if (cnt_val == 9)

      cnt_val = 0;

    else

      cnt_val++;

  }

  else

  {

    if (cnt_val == 0)

      cnt_val = 9;

    else

      cnt_val--;

  }

  outport(seg7_table[cnt_val]);

  delay(delaytime);

}

test up/dow seg7 + btn

 int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90};

int delaytime = 1000;

int btn_pin = 9;

int num_val;

int cnt_val;

int even_odd;

void setup()

{

  pinMode(btn_pin, INPUT);

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

  {

    pinMode(seg7_pin[i], OUTPUT);

    digitalWrite(seg7_pin[i], LOW);

  }

}

//-------------------------------

void outport(byte data)

{

  for (int j = 0; j < 7; j++)

  {

    if (data % 2 == 1)

      digitalWrite(seg7_pin[j], HIGH);

    else

      digitalWrite(seg7_pin[j], LOW);

    data = data / 2;

  }

}

void loop() {

  num_val = digitalRead(btn_pin);

  if (num_val == 0)

  {

    while (digitalRead(btn_pin) == LOW)

    {

      even_odd++;

    }

  }

  if (even_odd % 2 == 0)

  {

    if (cnt_val == 9)

      cnt_val = 0;

    else

      cnt_val++;

  }

  else

  {

    if (cnt_val == 0)

      cnt_val = 9;

    else

      cnt_val--;

  }

  outport(seg7_table[cnt_val]);

  delay(delaytime);

}

cnt_val 上下數 七段顯示器 + btn

 int seg7_one_array[]={2,3,4,5,6,7,8};

int seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};

int debounce_delay=50;

int btn=9;

int num_val;

int num_key=0;

int cnt_val;

void setup()

{

 pinMode(btn,INPUT);

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

 {

  pinMode(seg7_one_array[i],OUTPUT);

  digitalWrite(seg7_one_array[i],LOW);

 }

}

//---------------------------

void outport(byte data)

{

  for(int j=0;j<7;j++)

  {

    if(data %2==1)

      digitalWrite(seg7_one_array[j],HIGH);

    else

      digitalWrite(seg7_one_array[j],LOW);

    data=data/2;

  }

}

  

void loop()

{

  for(int j=0;j<8;j++)//此行加不加都不effect結果

  {

    digitalWrite(seg7_one_array[j],LOW);

  }

  num_val=digitalRead(btn);

  if(num_val==LOW)//你有壓按鈕,讀值為0條件成立才會進來

  {

    delay(debounce_delay);

    while(digitalRead(btn)==LOW);

    num_key++;//數值加1,判斷奇偶數

  }

  if(num_key%2==0)//一開機num_key你初值設為0,所以進來就上數

  {//每次加1,偶數奇數一直輪流,你壓一下奇數,再壓一下偶數

   if(cnt_val==9)//偶數就上數

    cnt_val=0;

   else

    cnt_val++;

  }

  else

  {

   if(cnt_val==0)//奇數就下數

    cnt_val=9;//上下數計數值要一樣,因為切數過去可以數字連續上數或下數

   else

    cnt_val--;

  }

   outport(seg7_table[cnt_val]);

   delay(1000);//你要加delay 1秒,否則7段顯示器會全亮

}

要加delay(1000)否則7seg會全亮

 int seg7_one_array[]={2,3,4,5,6,7,8};

int seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};

void setup()

{

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

 {

  pinMode(seg7_one_array[i],OUTPUT);

  digitalWrite(seg7_one_array[i],LOW);

 }

}

//---------------------------

void outport(byte data)

{

  for(int j=0;j<7;j++)

  {

    if(data %2==1)

      digitalWrite(seg7_one_array[j],HIGH);

    else

      digitalWrite(seg7_one_array[j],LOW);

    data=data/2;

  }

}

  

void loop()

{

 for(int k=0;k<7;k++)

 {

   outport(seg7_table[k]);

   delay(1000);//你要加delay 1秒,否則7段顯示器會全亮

 }

}

led_one_array

 int unsigned long time1 = 0;

int unsigned long time2 = 0;

int sw = 10;

int led_pin = 13;

int led_one_array[] = {2, 3, 4, 5, 6, 7, 8, 9};//同一屬性一維陣列

int debounce_delay = 100;//設定延遲時間

int val;

int i;

int j = 7;//因為您設最左邊為開始

void setup()

{

  pinMode(led_pin, OUTPUT);

  pinMode(sw, INPUT);

  digitalWrite(sw, HIGH);

  for (int i = 0; i < 8; i++)

  {

    pinMode(led_one_array[i], OUTPUT);

  }

}

void loop()

{


  for (i = 0; i < 8; i++)//此行一定要加,每loop一次就全部led關閉

  {//如果不加會出現76543210燈依續亮,加此for loop才會一次亮一個燈

    digitalWrite(led_one_array[i], LOW);

  }

  if (millis() - time1 > 500)//大於0.5秒才進來一次

  {

    time1 - millis();

    digitalWrite(led_pin, HIGH); delay(500);//亮0.5秒

    digitalWrite(led_pin, LOW); delay(500);//不亮0.5秒

  }

  val = digitalRead(sw);

  if (val == LOW)//按下去LOW才進來執行

  {

    if (millis() - time2 > 300)//大於300ms=0.3秒才進來執行

    {

      time2=millis();

      digitalWrite(led_one_array[j], HIGH);

      delay(300);

      if (j == 7)//由右向左快跑

        j = 0;

      else

        j++;

    }

  }

  else

  {

    digitalWrite(led_one_array[j], HIGH);

    delay(1000);

    if (j == 0)//到最右邊0時就又歸7最左邊

      j = 7;

    else//即為一開機為由左向右

      j--;//在全域變數下, 您j=7, 每次-1即7654321

  }

}

L(內建led 1秒亮暗) + 右移 按一下左移0.3秒,放開在右移1秒

 int unsigned long time1 = 0;

int unsigned long time2 = 0;

int sw = 10;

int led_pin = 13;

int led[] = {2, 3, 4, 5, 6, 7, 8, 9};

int debounce_delay = 100;

int val;

int i;

int j = 7;//因為您設最左邊為開始

int direct = 0;

void setup()

{

  pinMode(led_pin, OUTPUT);

  pinMode(sw, INPUT);

  digitalWrite(sw, HIGH);

  for (int i = 0; i < 8; i++)

  {

    pinMode(led[i], OUTPUT);

  }

}

void loop()

{


  for (i = 0; i < 8; i++)//此行一定要加,每loop一次就全部led關閉

  {//如果不加會出現76543210燈依續亮,加此for loop才會一次亮一個燈

    digitalWrite(led[i], LOW);

  }

  if (millis() - time1 > 500)//大於0.5秒才進來一次

  {

    time1 - millis();

    digitalWrite(led_pin, HIGH); delay(500);//亮0.5秒

    digitalWrite(led_pin, LOW); delay(500);//不亮0.5秒

  }

  val = digitalRead(sw);

  if (val == LOW)//按下去LOW才進來執行

  {

    if (millis() - time2 > 300)//大於300ms=0.3秒才進來執行

    {

      digitalWrite(led[j], HIGH);

      delay(300);

      if (j == 7)//由右向左快跑

        j = 0;

      else

        j++;

    }

  }

  else

  {

    digitalWrite(led[j], HIGH);

    delay(1000);

    if (j == 0)//到最右邊0時就又歸7最左邊

      j = 7;

    else//即為一開機為由左向右

      j--;//在全域變數下, 您j=7, 每次-1即7654321

  }

}

正常由左而右1秒,按下由右而左0.3秒,放開又正常左而右

 int unsigned long time1 = 0;

int unsigned long time2 = 0;

int sw = 10;

int led_pin = 13;

int led[] = {2, 3, 4, 5, 6, 7, 8, 9};

int debounce_delay = 100;

int val;

int i;

int j = 7;

int direct = 0;

void setup()

{

  pinMode(led_pin, OUTPUT);

  pinMode(sw, INPUT);

  digitalWrite(sw, HIGH);

  for (int i = 0; i < 8; i++)

  {

    pinMode(led[i], OUTPUT);

  }

}

void loop()

{


  for (i = 0; i < 8; i++)

  {

    digitalWrite(led[i], LOW);

  }

  if (millis() - time1 > 500)

  {

    time1 - millis();

    digitalWrite(led_pin, HIGH); delay(500);

    digitalWrite(led_pin, LOW); delay(500);

  }

  val = digitalRead(sw);

  if (val == LOW)

  {

    if (millis() - time2 > 1000)

    {

      digitalWrite(led[j], HIGH);

      delay(300);

      if (j == 7)

        j = 0;

      else

        j++;

    }

  }

  else

  {

    digitalWrite(led[j], HIGH);

    delay(1000);

    if (j == 0)

      j = 7;

    else

      j--;

  }

}

13 pin+ pilill燈

 int unsigned long time1=0;

int sw=10;

int led_pin=13;

int led[]={2,3,4,5,6,7,8,9};

int debounceDelay=20;

int val;

int i;

int j=7;

int direct=0;

void setup() 

  pinMode(led_pin,OUTPUT);

  pinMode(sw,INPUT);  

  digitalWrite(sw,HIGH);

  for(int i=0;i<8;i++)

  {

    pinMode(led[i],OUTPUT);

  }  

}

void loop() 

{

  if(millis()-time1>500)

  {

    time1-millis();

    digitalWrite(led_pin,HIGH);delay(500);

    digitalWrite(led_pin,LOW);delay(500);

  }

  val=digitalRead(sw);

  if(val==LOW)

  {

    delay(debounceDelay);

    while(digitalRead(sw)==LOW)

      ;  

    direct=!direct;                 

  } 

  for(i=0;i<8;i++)

  {

    digitalWrite(led[i],LOW);

  }  

  if(direct==0)

  {

    digitalWrite(led[j],HIGH);

    delay(1000);

    if(j==0)

      j=7;

    else

      j--;

  }

  else

  {

    digitalWrite(led[j],HIGH);

    delay(300);

    if(j==7)

      j=0;

    else

      j++;    

  }     

}

2021年8月17日 星期二

一開機pin10顯示0hz,7段顯示器顯示0

 /*

 功能:一開機pin10顯示0hz,7段顯示器顯示0

      再壓一下顯示250hz,7段顯示器顯示1 

      再壓一下顯示500hz,7段顯示器顯示2

      再壓一下顯示1khz,7段顯示器顯示3

      再壓一下顯示2khz,7段顯示器顯示4

      再壓一下顯示4khz,7段顯示器顯示5

 */

unsigned int seg7_pin[]={2,3,4,5,6,7,8};

unsigned char seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};

int buzzer=10;

int speed0=0;//0hz 一開機

int speed1=2000;//0.25khz  1/(2000+2000)u

int speed2=1000;//0.5khz 1/(1000+1000)u

int speed3=500;//1khz 1/(500+500)u

int speed4=250;//2khz 1/(250+250)u

int speed5=125;//4khz 1/(125+125)u

int btn1=9;

int num_val;

int num=-1;

void setup()

{

  Serial.begin(9600);

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

  {

    pinMode(seg7_pin[i],OUTPUT);

  }

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

  {

    digitalWrite(seg7_pin[i],LOW);

  }

 pinMode(btn1,INPUT);

 pinMode(buzzer,OUTPUT);

}

//--------------------------

void outport(byte data)

{

  for(int j=0;j<7;j++)

  {

    if(data %2==1)

     digitalWrite(seg7_pin[j],HIGH);

    else

     digitalWrite(seg7_pin[j],LOW);

    data=data/2;

  }

}

void loop()

{

 num_val=digitalRead(btn1);

 if(num_val==LOW)

 {

  delay(1000);

  while(digitalRead(btn1)==LOW);

  num++;

  if(num==6)

   num=0;

 }

 if(num==0)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed0);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed0);

   outport(seg7_table[0]);

   Serial.println(speed0);

 }

  if(num==1)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed1);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed1);

   outport(seg7_table[1]);

   Serial.println(speed1);

 }

 if(num==2)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed2);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed2);

   outport(seg7_table[2]);

   Serial.println(speed2);

 }

 if(num==3)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed3);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed3);

   outport(seg7_table[3]);

   Serial.println(speed3);

 }

 if(num==4)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed4);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed4);

   outport(seg7_table[4]);

   Serial.println(speed4);

 }

 if(num==5)

 {

   digitalWrite(buzzer,HIGH);

   delayMicroseconds(speed5);

   digitalWrite(buzzer,LOW);

   delayMicroseconds(speed5);

   outport(seg7_table[5]);

   Serial.println(speed5);

 }

}

color +1

 int rled_pin=9;

int gled_pin=10;

int bled_pin=11;

int btn1=2;

int num_val;

int num=-1;

void setup()

{

  pinMode(btn1,INPUT);

}


void loop() 

{

  num_val=digitalRead(btn1);

  if(num_val==LOW)

  {

    delay(1000);

    while(digitalRead(btn1)==LOW);

    num++;

    if(num==3)

     num==0;

  }

  if(num==0)

  {

    analogWrite(rled_pin,255);

    analogWrite(gled_pin,0);

    analogWrite(bled_pin,0);

  }

  else if(num==1)

  {

    analogWrite(rled_pin,0);

    analogWrite(gled_pin,255);

    analogWrite(bled_pin,0);

  }

  else if(num==2)

  {

    analogWrite(rled_pin,0);

    analogWrite(gled_pin,0);

    analogWrite(bled_pin,255);

  }

}

pwm +1

 int pwm_led_pin=5;

int btn1_pin=2;

int btn2_pin=3;

int num1_val;

int num2_val;

int num;

void setup()

{

  pinMode(btn1_pin,INPUT);

  pinMode(btn2_pin,INPUT);

}


void loop()

{

 num1_val=digitalRead(btn1_pin);

 num2_val=digitalRead(btn2_pin);

 if(num1_val==LOW)

 {

  delay(50);

  while(digitalRead(btn1_pin)==LOW);

  analogWrite(pwm_led_pin,num=num+5);

 }

 if(num2_val==LOW)

 {

  delay(50);

  while(digitalRead(btn2_pin)==LOW);

  analogWrite(pwm_led_pin,num=num-5);

 }

}

DHT11

參考網站https://arduinoinfo.mywikis.net/wiki/DHT11-Humidity-TempSensor 

DHT11-Humidity-TempSensor

Jump to navigationJump to search
Digital output temperature and humidity sensor DHT11 - DHT22
DHT11-HumidityTempVSG.jpg
NOTE: Available as both a plain component, or as an Electronic Brick. (Available here: )

See photos at right: Connections are (V)oltage, (S)ignal, (G)round. On the Electronic Brick: (S)Signal, (V)Voltage,(G)Ground

NOTE: Needs 4.7K to 10K pullup resistor from +5 to Signal pin (Electronic Brick version has it built-in). Connect to Arduino Digital pin, not Analog pin. (Manufacturer says this about distance/pullup): When the connecting cable is shorter than 20 metres, a 5K pull-up resistor is recommended; when the connecting cable is longer than 20 metres, choose a appropriate pull-up resistor as needed.

Specifications:

Humidity measuring range: 20% ~ 9 0% RH (0-50 ? temperature compensation)
- Temperature measuring range: 0 ~ +50 ?C ;

Temperature-Humidity-DHT11.jpg

- Humidity measuring accuracy: 5.0% RH
- Temperature measurement accuracy: 2.0 C
- Response time: (Updated by Rob Tillaart: now < 50 ms)
- Low power consumption
Features
.. single wire digital interface ( the most simple system integration, ultra-low prices )
.. ultra-small size ( 12X15.5X5.5 mm )
.. high reliability
.. optimized long-term stability

Test Software Sketch for DHT11 (Credits: THANKS to Rob Tillaart)


DETAILS: Before you can use an "Arduino Software Library", you must make it available by copying it into the correct "libraries" folder. See Libraries HowTo HERE

Here is a .ZIP file for the newest Rob Tillaart version (recommended):

media:DHTNEW.zip


NOTE: Lots of good information about the DHT software library on the Arduino "Playground" site HERE.
NOTE: After you install the DHTLIB Library, look in EXAMPLES for similar test sketches for DHT21, DHT22
NOTE: Set your Serial Monitor(click for Info) to 115200 (Lower right of window)


//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 5

void setup()
{
  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
                Serial.print("OK,\t");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.print("Checksum error,\t");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.print("Time out error,\t");
                break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default:
                Serial.print("Unknown error,\t");
                break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(2000);
}
//
// END OF FILE
//



The output from this program to the Serial Monitor will be something like this:


DHT TEST PROGRAM
LIBRARY VERSION: 0.1.20

Type,    status,    Humidity (%),    Temperature (C)
DHT11,     OK,    32.0,    29.0
DHT11,     OK,    33.0,    28.0
DHT11,     OK,    33.0,    28.0
DHT11,     OK,    33.0,    28.0  (BREATHE On Sensor ! )
DHT11,     OK,    75.0,    30.0
DHT11,     OK,    75.0,    29.0
DHT11,     OK,    75.0,    29.0

 


To get ALL of Rob Tillaart's libraries download HERE


For an example of a Web Server that displays DHT11 sensor values, [/ethernet-temp-humidity see THIS:]

EXAMPLE SKETCH: DHT11, Temp, Humidity, Time (from RTC) [Connection Diagram Below]
THANKS to David Loveridge
Note: Not Tested by Terry yet...

//
// AUTHOR: Rob Tillaart, Terry King, David Loveridge
// VERSION:
// PURPOSE: DHT, RTC and LCD display Arduino
//     URL:
// Three examples brought together to create a temp / humidity and time display on a 16/2 LCD.
// RTC and LCD use i2c port (i2c Gnd 5V and pins a4 sda, a5 scl) DHT-11 BRICK unit uses Gnd 5V and pin 2
// Released to the public domain
//
/*-----( Import needed libraries )-----*/
#include <Wire.h>              // In standard library
#include <dht.h>               // https://https://arduinoinfo.mywikis.net/wiki/TemperatureHumidity
#include <LiquidCrystal_I2C.h> // https://https://arduinoinfo.mywikis.net/wiki/LCD-Blue-I2C
#include "RTClib.h"            // https://https://arduinoinfo.mywikis.net/wiki/DS1307_RealTime_Clock_Brick
/*-----( Declare Constants )-----*/

/*-----( Declare objects )-----*/
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address Ox3F (Check yours)
RTC_DS1307 rtc;    // Create a RealTimeClock object (I set the time in another sketch)
/*-----( Declare Variables )-----*/
dht DHT;
#define DHT11_PIN 2 // use pin 2 on UNO to sample data from DHT module

void setup()
{
    Serial.begin(9600);
    Serial.println("DHT TEST PROGRAM ");
    Serial.print("DHT LIBRARY VERSION: ");
    Serial.println(DHT_LIB_VERSION);
    Serial.println();
    Serial.println("Humidity % \tTemperature (C) \tTime \tDate");
    lcd.begin(16,2); // defines it is a 16 character two line display
    rtc.begin(); // Start the RTC library code
    }

void loop()
{
    // READ DATA
    DateTime now = rtc.now();
    int chk = DHT.read11(DHT11_PIN);
    Serial.print(DHT.humidity, 1);
    Serial.print(",\t");
    Serial.print("\t");
    Serial.print(DHT.temperature, 1);
    Serial.print(",\t");
    Serial.print("\t");
    Serial.print("\t");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.println(now.year(), DEC);
    lcd.setCursor(0,0); // start postion of Humidity text on LCD
    lcd.print(DHT.humidity, 0); // 0 creates whole number, 1 two decimal
    lcd.print("% Humidity ");
    lcd.setCursor(0,1);  // start postion of temperature text on LCD
    lcd.print(DHT.temperature, 0);
    lcd.print(" C");
    lcd.setCursor(6,1); // start postion of time text on LCD
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print('.');
    lcd.print(now.second(), DEC);
    // You can display in lcd by changing Serial to lcd I have only used time above not date
    //Serial.print(now.year(), DEC);
    //Serial.print('/');
    //Serial.print(now.month(), DEC);
    //Serial.print('/');
    //Serial.print(now.day(), DEC);
    //Serial.print(' ');
    //Serial.print(now.hour(), DEC);
    //Serial.print(':');
    //Serial.print(now.minute(), DEC);
    //Serial.print(':');
    //Serial.print(now.second(), DEC);
    //Serial.println();
    delay(1000); // screen - sample & LCD refresh time 1 second although DHT say min 2 seconds but works ok.
}
//
// END OF FILE
//



UNO_to_LCD_RTC_ TEMP.jpg

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...