const byte pin[]={0,1,2,3,4,5,6,7}; //宣告Ppin接腳為arduino 0,1,2,3,4,5,6,7
int i,j; //宣告變數名稱a,b
const int sw0=8; //switch pin
int value; //read switch pulse
void setup() {
// put your setup code here, to run once:
for(i=0;i<8;i++) //宣告led接腳0~7為輸出
{
pinMode(pin[i],OUTPUT);
}
pinMode(sw0,INPUT); //宣告指撥開關sw0為輸入
}
void loop() {
// put your main code here, to run repeatedly:
for(i=0;i<8;i++) //宣告led全部關閉
{
digitalWrite(pin[i],HIGH);
}
//---
for(i=0;i<8;i++) //宣告led全部亮
{
digitalWrite(pin[i],LOW);
}
delay(1000); //延遲1秒
for(i=0;i<8;i++) //宣告led全部關閉
{
digitalWrite(pin[i],HIGH);
}
delay(1000); //延遲1秒
//--
value=digitalRead(sw0); //宣告value讀取指撥開關sw0的訊號
if(value==HIGH) //指撥開關撥上去
{
for(j=0;j<10;j++) //跑馬燈run 10次
{
for(i=0;i<8;i++) //跑馬燈 code
{
digitalWrite(pin[i],LOW);
delay(500);
digitalWrite(pin[i],HIGH);
}
}
}
}
led 跑馬燈左右移
const byte pin_show[]={2,3,4,5,6,7,8,9};
int i=7;
int dir=1; /*0-left模式 1-right模式*/
int j;
void setup() {
// put your setup code here, to run once:
for(i=0;i<8;i++) /*宣告腳位0-7*/
{
pinMode(pin_show[i],OUTPUT);
}
for(i=0;i<8;i++) /*宣告功能初始為關閉*/
{
digitalWrite(pin_show[i],HIGH);
}
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pin_show[i],LOW); /*宣告單燈移動功能*/
delay(500);
digitalWrite(pin_show[i],HIGH);
if(dir==1) /*判斷右移模式 */
{
for(j=0;j<3;j++)
{
i--;
if(i<=0)
i=7; /*改變為左移模式*/
}
if(j==3)
{dir=0;i=0;}
}
else
{
for(j=0;j<3;j++)
{
i++;
if(i>=7)
i=0; /*改變為右移模式*/
}
if(j==3)
{dir=1;i=7;}
}
}
3個sw 功能顯示
const byte pin_led[]={2,3,4,5,6,7,8,9};
const byte sw0=10; // #define sw0 10
const byte sw1=11;
const byte sw2=12;
//--
int i,j=0,m=0,dir=0,k=0;
int val,val1,val2;
//--
void setup() {
// put your setup code here, to run once:
//--第1種方式宣告
//DDRD=B11111111; // 1-Output 0-input
//--第2種方式宣告
for(i=0;i<8;i++)
{
pinMode(pin_led[i],OUTPUT); //2-9 ==>OUTPUT
}
pinMode(sw0,INPUT);
pinMode(sw1,INPUT);
pinMode(sw2,INPUT);
//--初始宣告全部關閉
for(i=0;i<8;i++)
{
digitalWrite(pin_led[i],LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
//----判斷讀取按鈕訊號
val=digitalRead(sw0);
val1=digitalRead(sw1);
val2=digitalRead(sw2);
// if(val==HIGH) { val ==LOW;delay(20);m=1; }
// if(val1==HIGH){ val1==LOW;delay(20);m=2; }
// if(val2==HIGH){ val1==LOW;delay(20);m=3; }
if(val==1) {(val==0);delay(10);m=1; }
if(val1==1){(val1==0);delay(10);m=2; }
if(val2==1){(val1==0);delay(10);m=3; }
//--left LED run
if(m==0)
{
digitalWrite(pin_led[k],HIGH);
delay(500);
digitalWrite(pin_led[k],LOW);
k++;
if(k>7)
k=0;
}
//---right led run
if(m==1)
{
digitalWrite(pin_led[k],HIGH);
delay(500);
digitalWrite(pin_led[k],LOW);
k--;
if(k<0)
{ k=7;
j++;
if(j>4)
{j=0;m=0;}}
}
//--on-off
if(m==2)
{
k++;
if(k>9)
k=0;
//--
for(j=0;j<8;j++)
{
digitalWrite(pin_led[j],HIGH);
}
delay(500);
for(j=0;j<8;j++)
{
digitalWrite(pin_led[j],LOW);
}
delay(500);
for(j=0;j<8;j++)
{
digitalWrite(pin_led[j],HIGH);
}
//--
}
//--left-right
if(m==3)
{
digitalWrite(pin_led[k],HIGH);
delay(500);
digitalWrite(pin_led[k],LOW);
if(dir==0)
{
k++;
if(k>6)
dir=1;
}
else
{
k--;
if(k<=0)
{dir=0;
j++;
if(j>4)
{j=0;m=0;}
}
}
}
}
const char num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //顯示0-9的資料
const int seg[]={0,1,2,3,4,5,6,7}; //data的接腳宣告
const int scan[]={8,9,10,11};
int i,k,Value=0,number;
unsigned long time=0;
void setup() {
// put your setup code here, to run once:
for(i=0;i<8;i++)
pinMode(seg[i],OUTPUT);
//---
for(k=0;k<4;k++)
{
pinMode(scan[k],OUTPUT);
digitalWrite(scan[k],HIGH);
}
//--
}
void loop() {
// put your main code here, to run repeatedly:
//----7seg dispaly
number=Value;
for(i=0;i<8;i++)
digitalWrite(seg[i],LOW); // 關閉7段顯示器--消除殘影
for(k=0;k<4;k++)
{
for(i=0;i<8;i++) //data
{
if(bitRead(num[number%10],i)) //read array data
digitalWrite(seg[i],HIGH);
else
digitalWrite(seg[i],LOW);
}
digitalWrite(scan[k],0);
delay(5);
digitalWrite(scan[k],1);
number=number/10;
if(millis()-time>=10)
{
time=millis();
Value=Value+1;
if(Value>9999)
Value=0;
}
//---
}
}
#define trig1 A1
#define echo1 A0
float value,value1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trig1,OUTPUT); //傳送 chip
pinMode(echo1,INPUT); //接收 chip
//--
pinMode(12,OUTPUT); //led red
digitalWrite(12,LOW); //led off
pinMode(13,OUTPUT); //pz green
digitalWrite(13,LOW); //pz off
}
void loop() {
// put your main code here, to run repeatedly:
//---HC_SR04 主程式
digitalWrite(trig1,HIGH);
delayMicroseconds(10); //1us*10=10us
digitalWrite(trig1,LOW);
value=pulseIn(echo1,HIGH);
value1=value/58;
Serial.print("distance=");
Serial.print(value1,2);
Serial.println(" cm");
delay(5000); //1ms*1000=1s
}
0-F 顯示
const
unsigned char NUMBER[17] =
{
0x3F, // --0--
0x06, // --1--
0x5B, // --2--
0x4F, // --3--
0x66, // --4--
0x6D, // --5--
0x7D, // --6--
0x07, // --7--
0x7F, // --8--
0x6F, // --9--
0x77, // --A--
0x7C, // --B--
0x39, // --C--
0x5E, // --D--
0x79, // --E--
0x71, // --F--
0x80, // --:--
};
const int seg_pin[]={22,23,24,25,26,27,28,29};
const int scan[]={30,31,32,33};
int i,j,m;
void setup() {
// put your setup code here, to run once:
for(i=0;i<8;i++)
{
pinMode(seg_pin[i],OUTPUT);
digitalWrite(seg_pin[i],HIGH);
}
for(j=0;j<4;j++)
{
pinMode(scan[j],OUTPUT);
digitalWrite(scan[j],HIGH);
}
}
void loop() {
// put your main code here, to run repeatedly:
//
// digitalWrite(30,LOW);
// digitalWrite(22,LOW);
for(j=0;j<4;j++)
digitalWrite(scan[j],HIGH);
digitalWrite(scan[0],LOW);
delay(1);
for(m=0;m<17;m++)
{
for(i=0;i<8;i++)
{
if(bitRead(~NUMBER[m],i))
digitalWrite(seg_pin[i],HIGH);
else
digitalWrite(seg_pin[i],LOW);
}
delay(500);
}
}
#include "DHT11.h"
DHT11 dht(2);
float humidity,temp;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//--
}
void loop() {
// put your main code here, to run repeatedly:
int value=dht.read(humidity,temp);
if(value==0) //DHT11 on
{
Serial.print("現在濕度(humidity):");
Serial.print(humidity);
Serial.println("%");
Serial.println("//---");
Serial.print("現在溫度(temperature):");
Serial.print(temp);
// Serial.print((char)0xDf); //顯示度
Serial.println("*C");
delay(1000);
}
}
溫濕度顯示在lcd
#include "DHT11.h"
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
DHT11 dht(2);
float humidity,temp;
LiquidCrystal_I2C lcd(0x27,16,2);
#define pz_pin 11
int statu=1;
void DTH(void);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pz_pin,OUTPUT);
digitalWrite(pz_pin,LOW); //noTone(pz_pin);
lcd.init();
lcd.backlight();
}
void DTH(void){
int value=dht.read(humidity,temp);
//if(int value=dht.read(humidity,temp)==0)
if(value==0) //data 啟動傳輸讀取
{
Serial.print("現在濕度:");
Serial.println(humidity,2);
Serial.print("%\n");
//---
Serial.print("現在溫度:");
Serial.print(temp,2);
// Serial.print((char)0xDf); //顯示度
Serial.print("*C\n");
//---
/*
lcd.setCursor(0,0); //col-x=0 row-y=0
lcd.print("humidity: ");
lcd.print(humidity,1);
lcd.print(" %"); //col-x=0 row-y=1
lcd.setCursor(0,1);
lcd.print("temp: ");
lcd.print(temp,1);
lcd.print(" *C");
*/
}
delay(1000);
if(humidity>=40)
{
tone(pz_pin,500);
delay(100);
tone(pz_pin,300);
delay(100);
}
else
noTone(pz_pin);
}
void loop() {
// put your main code here, to run repeatedly:
//int value=dht.read(dataPin);
if(statu==1)
{
DTH();
}
delay(2000);
}