完成了一個可以處理國中物理相關問題的器材,包含光學、聲音、運動學、溫度變化的測量展示儀器,感謝龍山國中鄭志鵬小P老師讓我參考他的程式,還有網路上的其他資源,加上數位深耕計畫提供材料、金源與進度壓力才能逼迫我自己寫出這個東西。
基本上還缺1、2個功能就完整了,到時候再改版(預計擴增:1.霍爾感測器測磁場、2.JFET場效電晶體偵測靜電場,3.一組光閘,可用腳位剩下0.1.7.8,不知道夠不夠寫XD,或是0.1腳位的限制,如果沒問題應該是剛剛好4個腳位需求)
基本上如果能夠照下方連接圖插好,把程式碼寫進去nano裡面就可以運作了
所需要準備的器材,露天買起來含電池總價大概是700多塊一套,可以請廠商協助裝成一盒
aduino nano V3.0 ATMEGA328P
arduino nano UNO多用 擴展板 (E006)
麵包板SYB-170
輕觸按鍵開關4P立式電阻10KΩ
可變電阻WH148 B10K+梅花柄塑膠旋鈕
0.96吋黃藍雙色I2C IIC通信12864OLED
DS18B20不鏽鋼封裝防水型
電阻4.7KΩ
LED紅綠藍各1 or LED共陰全彩
無源蜂鳴器5V
超音波測距模塊HC-SR04伺服馬達SG90 Tower Pro
杜邦線公母頭 4色*3組,3色*4組,2色*1組
中號無格塑膠小盒子15.8*11.8*5.8即可收納
電源推薦18650+充電盒
連結圖如下(未連接到版上的黑色接GND,紅色接5V)
製作影片可參考6/4(四)研習影片
選單0 DHT11溫濕度+不鏽鋼防水溫度計
功能1 顯示溫濕度+不鏽鋼溫度計溫度(採樣更新頻率100ms~1100ms)
功能2 顯示溫濕度+不鏽鋼溫度計溫度+6組過去數值(採樣更新頻率100ms~1100ms)
功能3 顯示溫濕度+不鏽鋼溫度計溫度+6組過去數值(採樣更新頻率1000ms~11000ms)
選單1 RGB燈(我加了一個衛生紙燈罩,可以讓顏色混和更均勻,其實就是用衛生紙把燈包起來就好)
功能1 旋鈕調整R紅色亮度0~255
功能2 旋鈕調整G紅色亮度0~255
功能3 旋鈕調整B紅色亮度0~255
選單2 蜂鳴器
功能1 旋鈕調整Hz頻率0~1000
功能2 旋鈕調整Hz頻率0~10000
選單3 超音波測距(2cm~60cm較準確,僅取這一段)
功能1 顯示距離(採樣更新頻率100ms~1100ms)
功能2 顯示距離+9組過去數值(採樣更新頻率100ms~1100ms)
功能3 顯示距離+9組過去數值(採樣更新頻率10ms~110ms)
選單4 伺服馬達
功能 旋鈕調整馬達角度5~180
選單5 介紹畫面
---------------以下是arduino程式碼-------------
/*
* PHYduino_V2_202006
* design by orangeV,taiwan,tainan 台南大成國中 蔡智恆
* need libraries 需要程式庫<Servo.h>,<DHT.h>,<SPI.h>,<Wire.h>,<Adafruit_GFX.h>,<Adafruit_SSD1306.h>,<OneWire.h>,<DallasTemperature.h>
* Acknowledgments: 龍山國中 鄭志鵬、Proman Huang
*/
int
potPin = A3,
R = 0,
button = 2,
buzzer = 3,
motorPin = 4,
ds18b20 = 9,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
menu1N = 5,
menu2N = 6,
delaytime = 100;
float cm = 0.0,cm0[9]={0};
int i=0 , j;
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//D18B20
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#define OLED_RESET 4
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
Wire.begin();
dht.begin();
//motor
myservo.attach(motorPin);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 0)DHTxx();
if(mode2 == 1)RGBLED();
if ( mode2 != 1 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) noTone(buzzer);
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if ( mode2 != 4 ) myservo.write(0);
if(mode2 == 5)openmenu();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == menu1N ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 31 ~ 1000 HZ");
}else{
display.println(" 31 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<31){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic 2to60");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("t= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==2) delaytime = delaytime/10;
display.print(delaytime);
if(mode1==1) delaytime = delaytime - 90;
if(mode1==2) delaytime = delaytime - 10;
display.setCursor(44,8);
display.println("ms");
int cm10=cm*10;
display.setTextSize(2);
if (cm<60&&cm>2){
display.print(cm10/10);
display.setTextSize(1);
display.setCursor(25,23);
display.print(".");
int cm100=cm*100;
display.print(cm100%100/10);
}else{
display.print("err");
}
display.setTextSize(2);
display.setCursor(37,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==1||mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=9)i=0;
if(i<9){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(70+20*(i%3),(i/3)*7+10);
display.print("*");
display.setCursor(75,10);
display.print(cm0[0],0);
display.setCursor(95,10);
display.print(cm0[1],0);
display.setCursor(115,10);
display.print(cm0[2],0);
display.setCursor(75,16);
display.print(cm0[3],0);
display.setCursor(95,16);
display.print(cm0[4],0);
display.setCursor(115,16);
display.print(cm0[5],0);
display.setCursor(75,23);
display.print(cm0[6],0);
display.setCursor(95,23);
display.print(cm0[7],0);
display.setCursor(115,23);
display.print(cm0[8],0);
i++;
}
}
delay(delaytime);
}
void DHTxx(){
sensors.requestTemperatures();
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
float cc = sensors.getTempCByIndex(0);
display.print("T & H ");
if ( mode1 >= 3 ) mode1 = 1;
display.print("t = ");
delaytime = R/100 * 100+100;
if(mode1==2) delaytime = delaytime*10-400;
display.print(delaytime+400);
display.print(" ms");
display.print(" m");
display.println(mode1);
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.println("C");
display.setTextSize(2);
if(cc>-127){
display.print(cc,1);
}
display.print("C");
if(mode1>=1){
if(i>=6)i=0;
if(i<6){
if (cc>-170){
cm0[i]=cc;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(60+30*(i%2),(i/2)*6+10);
display.print("*");
display.setCursor(65,10);
display.print(cm0[0],1);
display.setCursor(95,10);
display.print(cm0[1],1);
display.setCursor(65,16);
display.print(cm0[2],1);
display.setCursor(95,16);
display.print(cm0[3],1);
display.setCursor(65,22);
display.print(cm0[4],1);
display.setCursor(95,22);
display.print(cm0[5],1);
i++;
}
}
display.display();
display.clearDisplay();
delay(delaytime);
delaytime = 0;
}
void motor(){
R = 0;
R = analogRead(potPin);
R = map(R, 0, 1023, 0, 175); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor 5 ~ 180");
display.setTextSize(2);
display.print("angle ");
display.print(R+5);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
R = 0;
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR*0.8);//調弱輸出電壓,保護LED燈跟眼睛
analogWrite(ledGPin, cG*0.4);
analogWrite(ledBPin, cB*0.8);
}
--------------以上是程式碼--------------
自從學了比較正統的語言學之後,雖然覺得還是差很多練習,但是有感覺比較順一點了,目前工作進度是這樣,DHT11溫溼度、DS18b20溫度、超音波測距、顏色顯示與調整1按鈕1可變電阻、馬達可變電阻調整角度、發聲頻率調整等...
--------------第一版構思--------------
int
PulseWire = A2,
Threshold = 550,
potPin = A3,
R = 0,
ds18b20 = 1,
button = 2,
buzzer = 3,
motorPin = 4,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
cm = 0,
menu1N = 4,
menu2N = 6,
delaytime = 100;
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//DS18b20
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//程式庫標頭檔
#include <OneWire.h>
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
Wire.begin();
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
dht.begin();
//motor
myservo.attach(motorPin);
myservo.write(90);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0); //字體位置
display.setTextSize(1); //字體大小
if(sw == HIGH)menu();
if(mode2 == 0)RGBLED();
if ( mode2 != 0 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 1)DHTxx();
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) {
noTone(buzzer);
}
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if(mode2 == 5)openmenu();
}
void menu(){
unsigned long duration = pulseIn(button, HIGH ,2500000);
if (duration != 0) {
Serial.print("duration: ");
Serial.print(duration);
Serial.println(" microseconds");
mode1 = mode1 + 1;
} else {
Serial.println("timeout");
mode2 = mode2 + 1;
}
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == (menu1N+1) ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 0 ~ 1000 HZ");
}else{
display.println(" 0 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<10){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(2);
display.print(cm);
display.setCursor(50,9);
display.println("CM");
display.setTextSize(1);
display.print("sampling= ");
R = analogRead(potPin);
delaytime = R + 100;
display.print(delaytime);
display.setCursor(100,24);
display.print("ms");
display.display();
display.clearDisplay();
if(mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
}
delay(delaytime);
}
void DHTxx(){
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
display.println("DHT");
display.setTextSize(2);
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.print("C");
display.display();
display.clearDisplay();
delay(delaytime);
}
void motor(){
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 180); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor");
display.setTextSize(2);
display.print("angle ");
display.print(R);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
--------------第二版按鈕不敏銳--------------
int
PulseWire = A2,
Threshold = 550,
potPin = A3,
R = 0,
ds18b20 = 1,
button = 2,
buzzer = 3,
motorPin = 4,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
cm = 0,
menu1N = 4,
menu2N = 6,
delaytime = 100;
int i=0 , j,cm0[6]={0};
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//DS18b20
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//程式庫標頭檔
#include <OneWire.h>
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
Wire.begin();
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
dht.begin();
//motor
myservo.attach(motorPin);
myservo.write(0);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 3)RGBLED();
if ( mode2 != 0 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 1)DHTxx();
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) {
noTone(buzzer);
}
if(mode2 == 0)supersonic();
if(mode2 == 4)motor();
if(mode2 == 5)openmenu();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == (menu1N+1) ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 0 ~ 1000 HZ");
}else{
display.println(" 0 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<10){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("sampling= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
display.print(delaytime);
display.setCursor(100,8);
display.println("ms");
display.setTextSize(2);
if (cm<60&&cm>3){
display.print(cm);
}else{
display.print("??");
}
display.setCursor(45,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=6)i=0;
if(i<6){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(75,16);
display.print(cm0[0]);
display.setCursor(95,16);
display.print(cm0[1]);
display.setCursor(115,16);
display.print(cm0[2]);
display.setCursor(75,23);
display.print(cm0[3]);
display.setCursor(95,23);
display.print(cm0[4]);
display.setCursor(115,23);
display.print(cm0[5]);
i++;
}
}
delay(delaytime);
}
void DHTxx(){
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
display.println("DHT");
display.setTextSize(2);
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.print("C");
display.display();
display.clearDisplay();
delay(delaytime);
}
void motor(){
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 180); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor");
display.setTextSize(2);
display.print("angle ");
display.print(R);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
--------------第三版快完成了--------------
int
PulseWire = A2,
Threshold = 550,
potPin = A3,
R = 0,
button = 2,
buzzer = 3,
motorPin = 4,
ds18b20 = 9,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
menu1N = 4,
menu2N = 6,
delaytime = 100;
float cm = 0.0;
int i=0 , j,cm0[9]={0};
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//
#include <OneWire.h>
//DS18b20
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
Wire.begin();
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
dht.begin();
//motor
myservo.attach(motorPin);
myservo.write(0);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 0)DHTxx();
if(mode2 == 1)RGBLED();
if ( mode2 != 1 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) noTone(buzzer);
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if ( mode2 != 4 ) myservo.write(0);
if(mode2 == 5)openmenu();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == (menu1N+1) ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 0 ~ 1000 HZ");
}else{
display.println(" 0 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<10){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 4 ) mode1 = 1;
display.print("SoundSonic 2to60");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("t= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==3) delaytime = delaytime/10;
display.print(delaytime);
display.setCursor(44,8);
display.println("ms");
int cm10=cm*10;
display.setTextSize(2);
if (cm<60&&cm>2){
display.print(cm10/10);
display.setTextSize(1);
display.setCursor(25,23);
display.print(".");
int cm100=cm*100;
display.print(cm100%100/10);
}else{
display.print("err");
}
display.setTextSize(2);
display.setCursor(37,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==2||mode1==3){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=9)i=0;
if(i<9){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(70+20*(i%3),(i/3)*7+10);
display.print("*");
display.setCursor(75,10);
display.print(cm0[0]);
display.setCursor(95,10);
display.print(cm0[1]);
display.setCursor(115,10);
display.print(cm0[2]);
display.setCursor(75,16);
display.print(cm0[3]);
display.setCursor(95,16);
display.print(cm0[4]);
display.setCursor(115,16);
display.print(cm0[5]);
display.setCursor(75,23);
display.print(cm0[6]);
display.setCursor(95,23);
display.print(cm0[7]);
display.setCursor(115,23);
display.print(cm0[8]);
i++;
}
}
delay(delaytime);
}
void DHTxx(){
sensors.requestTemperatures();
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
float cc = sensors.getTempCByIndex(0);
display.print("T & H ");
if ( mode1 >= 4 ) mode1 = 1;
display.print("t = ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==3) delaytime = delaytime*10;
display.print(delaytime);
display.println(" ms");
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.println("C");
display.setTextSize(2);
if(cc>-127){
display.print(cc,1);
}
display.print("C");
if(mode1>=2){
if(i>=6)i=0;
if(i<6){
if (cc>-170){
cm0[i]=cc;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(60+30*(i%2),(i/2)*6+10);
display.print("*");
display.setCursor(65,10);
display.print(cm0[0],1);
display.setCursor(95,10);
display.print(cm0[1],1);
display.setCursor(65,16);
display.print(cm0[2],1);
display.setCursor(95,16);
display.print(cm0[3],1);
display.setCursor(65,22);
display.print(cm0[4],1);
display.setCursor(95,22);
display.print(cm0[5],1);
i++;
}
}
display.display();
display.clearDisplay();
delay(delaytime);
delaytime = 0;
}
void motor(){
R = 0;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 180); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor");
display.setTextSize(2);
display.print("angle ");
display.print(R);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
R = 0;
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
----
參考資料
1.葉難
http://yehnan.blogspot.com/
2.阿簡生物筆記
http://a-chien.blogspot.com/
3.按鍵程式
https://www.geek-workshop.com/thread-28007-1-1.html
4.霍爾感測
https://makerpro.cc/2019/08/the-principles-and-application-of-magnetic-sensors/
5.林宣安老師_創意教具DIY-靜電磁場感測
http://l0930984547.blogspot.com/2017/09/blog-post_30.html
----------------最新版本放在最後,但是接線跟上面的不一樣,所以請202009後的版本再參考這個程式碼,新增開機畫面跟修正顯示文字,雙DHT11、預計擴充規劃------------------------------------
* need libraries 需要程式庫<Servo.h>,<DHT.h>,<SPI.h>,<Wire.h>,<Adafruit_GFX.h>,<Adafruit_SSD1306.h>,<OneWire.h>,<DallasTemperature.h>
* Acknowledgments: 龍山國中 鄭志鵬、Proman Huang
*/
int
potPin = A3,
R = 0,
button = 2,
buzzer = 3,
motorPin = 4,
ds18b20 = 9,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
menu1N = 5,
menu2N = 6,
delaytime = 100;
float cm = 0.0,cm0[9]={0};
int i=0 , j;
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//D18B20
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#define OLED_RESET 4
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
Wire.begin();
dht.begin();
//motor
myservo.attach(motorPin);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 0)DHTxx();
if(mode2 == 1)RGBLED();
if ( mode2 != 1 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) noTone(buzzer);
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if ( mode2 != 4 ) myservo.write(0);
if(mode2 == 5)openmenu();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == menu1N ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 31 ~ 1000 HZ");
}else{
display.println(" 31 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<31){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic 2to60");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("t= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==2) delaytime = delaytime/10;
display.print(delaytime);
if(mode1==1) delaytime = delaytime - 90;
if(mode1==2) delaytime = delaytime - 10;
display.setCursor(44,8);
display.println("ms");
int cm10=cm*10;
display.setTextSize(2);
if (cm<60&&cm>2){
display.print(cm10/10);
display.setTextSize(1);
display.setCursor(25,23);
display.print(".");
int cm100=cm*100;
display.print(cm100%100/10);
}else{
display.print("err");
}
display.setTextSize(2);
display.setCursor(37,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==1||mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=9)i=0;
if(i<9){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(70+20*(i%3),(i/3)*7+10);
display.print("*");
display.setCursor(75,10);
display.print(cm0[0],0);
display.setCursor(95,10);
display.print(cm0[1],0);
display.setCursor(115,10);
display.print(cm0[2],0);
display.setCursor(75,16);
display.print(cm0[3],0);
display.setCursor(95,16);
display.print(cm0[4],0);
display.setCursor(115,16);
display.print(cm0[5],0);
display.setCursor(75,23);
display.print(cm0[6],0);
display.setCursor(95,23);
display.print(cm0[7],0);
display.setCursor(115,23);
display.print(cm0[8],0);
i++;
}
}
delay(delaytime);
}
void DHTxx(){
sensors.requestTemperatures();
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
float cc = sensors.getTempCByIndex(0);
display.print("T & H ");
if ( mode1 >= 3 ) mode1 = 1;
display.print("t = ");
delaytime = R/100 * 100+100;
if(mode1==2) delaytime = delaytime*10-400;
display.print(delaytime+400);
display.print(" ms");
display.print(" m");
display.println(mode1);
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.println("C");
display.setTextSize(2);
if(cc>-127){
display.print(cc,1);
}
display.print("C");
if(mode1>=1){
if(i>=6)i=0;
if(i<6){
if (cc>-170){
cm0[i]=cc;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(60+30*(i%2),(i/2)*6+10);
display.print("*");
display.setCursor(65,10);
display.print(cm0[0],1);
display.setCursor(95,10);
display.print(cm0[1],1);
display.setCursor(65,16);
display.print(cm0[2],1);
display.setCursor(95,16);
display.print(cm0[3],1);
display.setCursor(65,22);
display.print(cm0[4],1);
display.setCursor(95,22);
display.print(cm0[5],1);
i++;
}
}
display.display();
display.clearDisplay();
delay(delaytime);
delaytime = 0;
}
void motor(){
R = 0;
R = analogRead(potPin);
R = map(R, 0, 1023, 0, 175); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor 5 ~ 180");
display.setTextSize(2);
display.print("angle ");
display.print(R+5);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
R = 0;
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR*0.8);//調弱輸出電壓,保護LED燈跟眼睛
analogWrite(ledGPin, cG*0.4);
analogWrite(ledBPin, cB*0.8);
}
--------------以上是程式碼--------------
自從學了比較正統的語言學之後,雖然覺得還是差很多練習,但是有感覺比較順一點了,目前工作進度是這樣,DHT11溫溼度、DS18b20溫度、超音波測距、顏色顯示與調整1按鈕1可變電阻、馬達可變電阻調整角度、發聲頻率調整等...
--------------第一版構思--------------
int
PulseWire = A2,
Threshold = 550,
potPin = A3,
R = 0,
ds18b20 = 1,
button = 2,
buzzer = 3,
motorPin = 4,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
cm = 0,
menu1N = 4,
menu2N = 6,
delaytime = 100;
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//DS18b20
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//程式庫標頭檔
#include <OneWire.h>
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
Wire.begin();
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
dht.begin();
//motor
myservo.attach(motorPin);
myservo.write(90);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0); //字體位置
display.setTextSize(1); //字體大小
if(sw == HIGH)menu();
if(mode2 == 0)RGBLED();
if ( mode2 != 0 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 1)DHTxx();
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) {
noTone(buzzer);
}
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if(mode2 == 5)openmenu();
}
void menu(){
unsigned long duration = pulseIn(button, HIGH ,2500000);
if (duration != 0) {
Serial.print("duration: ");
Serial.print(duration);
Serial.println(" microseconds");
mode1 = mode1 + 1;
} else {
Serial.println("timeout");
mode2 = mode2 + 1;
}
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == (menu1N+1) ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 0 ~ 1000 HZ");
}else{
display.println(" 0 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<10){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(2);
display.print(cm);
display.setCursor(50,9);
display.println("CM");
display.setTextSize(1);
display.print("sampling= ");
R = analogRead(potPin);
delaytime = R + 100;
display.print(delaytime);
display.setCursor(100,24);
display.print("ms");
display.display();
display.clearDisplay();
if(mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
}
delay(delaytime);
}
void DHTxx(){
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
display.println("DHT");
display.setTextSize(2);
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.print("C");
display.display();
display.clearDisplay();
delay(delaytime);
}
void motor(){
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 180); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor");
display.setTextSize(2);
display.print("angle ");
display.print(R);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
--------------第二版按鈕不敏銳--------------
int
PulseWire = A2,
Threshold = 550,
potPin = A3,
R = 0,
ds18b20 = 1,
button = 2,
buzzer = 3,
motorPin = 4,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
cm = 0,
menu1N = 4,
menu2N = 6,
delaytime = 100;
int i=0 , j,cm0[6]={0};
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//DS18b20
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//程式庫標頭檔
#include <OneWire.h>
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
Wire.begin();
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
dht.begin();
//motor
myservo.attach(motorPin);
myservo.write(0);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 3)RGBLED();
if ( mode2 != 0 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 1)DHTxx();
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) {
noTone(buzzer);
}
if(mode2 == 0)supersonic();
if(mode2 == 4)motor();
if(mode2 == 5)openmenu();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == (menu1N+1) ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 0 ~ 1000 HZ");
}else{
display.println(" 0 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<10){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("sampling= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
display.print(delaytime);
display.setCursor(100,8);
display.println("ms");
display.setTextSize(2);
if (cm<60&&cm>3){
display.print(cm);
}else{
display.print("??");
}
display.setCursor(45,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=6)i=0;
if(i<6){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(75,16);
display.print(cm0[0]);
display.setCursor(95,16);
display.print(cm0[1]);
display.setCursor(115,16);
display.print(cm0[2]);
display.setCursor(75,23);
display.print(cm0[3]);
display.setCursor(95,23);
display.print(cm0[4]);
display.setCursor(115,23);
display.print(cm0[5]);
i++;
}
}
delay(delaytime);
}
void DHTxx(){
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
display.println("DHT");
display.setTextSize(2);
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.print("C");
display.display();
display.clearDisplay();
delay(delaytime);
}
void motor(){
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 180); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor");
display.setTextSize(2);
display.print("angle ");
display.print(R);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
--------------第三版快完成了--------------
int
PulseWire = A2,
Threshold = 550,
potPin = A3,
R = 0,
button = 2,
buzzer = 3,
motorPin = 4,
ds18b20 = 9,
DHTPin = 10,
ledRPin = 5,
ledGPin = 6,
ledBPin = 11,
cR=0,cG=0,cB=0,
trigPin = 12, //Trig Pin
echoPin = 13, //Echo Pin
menu1N = 4,
menu2N = 6,
delaytime = 100;
float cm = 0.0;
int i=0 , j,cm0[9]={0};
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
//
#include <OneWire.h>
//DS18b20
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
Wire.begin();
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
dht.begin();
//motor
myservo.attach(motorPin);
myservo.write(0);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 0)DHTxx();
if(mode2 == 1)RGBLED();
if ( mode2 != 1 ) {
analogWrite(ledRPin, 0);
analogWrite(ledGPin, 0);
analogWrite(ledBPin, 0);
}
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) noTone(buzzer);
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if ( mode2 != 4 ) myservo.write(0);
if(mode2 == 5)openmenu();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == (menu1N+1) ) mode1 = 1;
}
void openmenu(){
display.setTextSize(1);
display.print("orangeV design");
display.setTextSize(2);
display.setCursor(0,12);
display.print("DCJHS LAB");
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 0 ~ 1000 HZ");
}else{
display.println(" 0 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<10){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 4 ) mode1 = 1;
display.print("SoundSonic 2to60");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("t= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==3) delaytime = delaytime/10;
display.print(delaytime);
display.setCursor(44,8);
display.println("ms");
int cm10=cm*10;
display.setTextSize(2);
if (cm<60&&cm>2){
display.print(cm10/10);
display.setTextSize(1);
display.setCursor(25,23);
display.print(".");
int cm100=cm*100;
display.print(cm100%100/10);
}else{
display.print("err");
}
display.setTextSize(2);
display.setCursor(37,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==2||mode1==3){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=9)i=0;
if(i<9){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(70+20*(i%3),(i/3)*7+10);
display.print("*");
display.setCursor(75,10);
display.print(cm0[0]);
display.setCursor(95,10);
display.print(cm0[1]);
display.setCursor(115,10);
display.print(cm0[2]);
display.setCursor(75,16);
display.print(cm0[3]);
display.setCursor(95,16);
display.print(cm0[4]);
display.setCursor(115,16);
display.print(cm0[5]);
display.setCursor(75,23);
display.print(cm0[6]);
display.setCursor(95,23);
display.print(cm0[7]);
display.setCursor(115,23);
display.print(cm0[8]);
i++;
}
}
delay(delaytime);
}
void DHTxx(){
sensors.requestTemperatures();
float DHThum = dht.readHumidity();
float DHTtemp= dht.readTemperature();
float cc = sensors.getTempCByIndex(0);
display.print("T & H ");
if ( mode1 >= 4 ) mode1 = 1;
display.print("t = ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==3) delaytime = delaytime*10;
display.print(delaytime);
display.println(" ms");
display.print(DHThum,0); //小數點0位
display.print("% ");
display.print(DHTtemp,1); //小數點1位
display.println("C");
display.setTextSize(2);
if(cc>-127){
display.print(cc,1);
}
display.print("C");
if(mode1>=2){
if(i>=6)i=0;
if(i<6){
if (cc>-170){
cm0[i]=cc;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(60+30*(i%2),(i/2)*6+10);
display.print("*");
display.setCursor(65,10);
display.print(cm0[0],1);
display.setCursor(95,10);
display.print(cm0[1],1);
display.setCursor(65,16);
display.print(cm0[2],1);
display.setCursor(95,16);
display.print(cm0[3],1);
display.setCursor(65,22);
display.print(cm0[4],1);
display.setCursor(95,22);
display.print(cm0[5],1);
i++;
}
}
display.display();
display.clearDisplay();
delay(delaytime);
delaytime = 0;
}
void motor(){
R = 0;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 180); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor");
display.setTextSize(2);
display.print("angle ");
display.print(R);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
R = 0;
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
----
參考資料
1.葉難
http://yehnan.blogspot.com/
2.阿簡生物筆記
http://a-chien.blogspot.com/
3.按鍵程式
https://www.geek-workshop.com/thread-28007-1-1.html
4.霍爾感測
https://makerpro.cc/2019/08/the-principles-and-application-of-magnetic-sensors/
5.林宣安老師_創意教具DIY-靜電磁場感測
http://l0930984547.blogspot.com/2017/09/blog-post_30.html
----------------最新版本放在最後,但是接線跟上面的不一樣,所以請202009後的版本再參考這個程式碼,新增開機畫面跟修正顯示文字,雙DHT11、預計擴充規劃------------------------------------
/*
* design by orangeV,taiwan,tainan 台南大成國中 蔡智恆
* need libraries 需要程式庫<Servo.h>,<DHT.h>,<SPI.h>,<Wire.h>,<Adafruit_GFX.h>,<Adafruit_SSD1306.h>,<OneWire.h>,<DallasTemperature.h>
* Acknowledgments: 龍山國中 鄭志鵬、Proman Huang
*/
int
//對應腳位
//0
//1
//2中斷能力腳位,光閘2
//3~PWM中斷能力腳位
motorPin = 4, //4步進馬達
ledRPin = 5, //5~PWM紅LED
ledGPin = 6, //6~PWM綠LED
ds18b20 = 7, //7溫度計ds18b20
//8光閘1
buzzer = 9, //9,蜂鳴器 (蜂鳴器可以讓出來)
button = 10, //10,按鈕
ledBPin = 11, //11~PWM藍LED
trigPin = 12, //12超聲波測距Trig Pin
echoPin = 13, //13超聲波測距Echo Pin
Photo = A0, //A0光敏電阻
DHTPin = 15, //A1溫濕度DHT
DHT2Pin = 16, //A2溫濕度DHT2
potPin = 17, //A3可變電阻旋鈕
//A4
//A5
//A6
//A7
//常數
R = 0,
menu1N = 5,
menu2N = 7,
delaytime = 100;
float cm = 0.0, cm0[9] = {0}, cm1[9];
int i=0 , j;
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
DHT dht2(DHT2Pin, DHT11);
//D18B20
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 32
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
const unsigned char PHYduino_Logo [] PROGMEM = {
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x5f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0x67, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe6,
0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6,
0x66, 0xff, 0xfc, 0xff, 0xfc, 0xe0, 0x38, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x66,
0x66, 0x00, 0x14, 0xa0, 0x14, 0xa0, 0x68, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x66,
0x66, 0xfc, 0x0c, 0xc0, 0x0c, 0xc0, 0x98, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x66,
0x66, 0x0c, 0x0c, 0xc0, 0x00, 0x83, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xa6,
0x66, 0x0c, 0x0c, 0xc0, 0x00, 0x8c, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0xe6,
0x66, 0x0c, 0x1c, 0xe0, 0x00, 0xd0, 0x18, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x06,
0x66, 0xf7, 0xf4, 0xa0, 0x1c, 0xa0, 0x39, 0xff, 0xf7, 0x80, 0x7f, 0xc3, 0xf3, 0x06, 0x00, 0xe6,
0x66, 0xac, 0x18, 0xff, 0xf4, 0xff, 0xe9, 0x40, 0x1e, 0x80, 0x51, 0x42, 0xd3, 0x05, 0xff, 0xa6,
0x66, 0xcc, 0x00, 0x00, 0x0c, 0x00, 0x39, 0x80, 0x19, 0x80, 0xf1, 0xf3, 0x33, 0x06, 0x00, 0x66,
0x66, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x19, 0x80, 0x19, 0x83, 0x31, 0x98, 0x33, 0x06, 0x00, 0x66,
0x66, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x19, 0x80, 0x19, 0x8c, 0x31, 0x8e, 0x33, 0x06, 0x00, 0x66,
0x66, 0x0e, 0x00, 0x00, 0x0c, 0x00, 0x39, 0xc0, 0x39, 0xb0, 0x31, 0x83, 0xb3, 0x92, 0x00, 0x66,
0x66, 0x0a, 0x00, 0x00, 0x14, 0x00, 0x29, 0x40, 0x29, 0x40, 0x51, 0x40, 0xd2, 0xad, 0xff, 0xa6,
0x67, 0x0f, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xf9, 0xc0, 0x73, 0xc6, 0x00, 0xe6,
0x65, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x66,
0x67, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf6,
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e,
0x5f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
Wire.begin();
dht.begin();
dht2.begin();
//motor
myservo.attach(motorPin);
//Photoresistor
pinMode(Photo, INPUT);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.drawBitmap(0, 8, PHYduino_Logo, 128, 64, WHITE);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
display.print("By.orangeV TCJHS.lab");
display.display();
delay(1500);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 6)DHTxx();
if(mode2 == 1)RGBLED();
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) noTone(buzzer);
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if ( mode2 != 4 ) myservo.write(0);
if(mode2 == 5)openmenu();
if(mode2 == 0)Photoresistor();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == menu1N ) mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 31 ~ 1000 HZ");
}else{
display.println(" 31 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<31){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic 2to60");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("t= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==2) delaytime = delaytime/10;
display.print(delaytime);
if(mode1==1) delaytime = delaytime - 90;
if(mode1==2) delaytime = delaytime - 10;
display.setCursor(44,8);
display.println("ms");
int cm10=cm*10;
display.setTextSize(2);
if (cm<60&&cm>2){
display.print(cm10/10);
display.setTextSize(1);
display.setCursor(25,23);
display.print(".");
int cm100=cm*100;
display.print(cm100%100/10);
}else{
display.print("err");
}
display.setTextSize(2);
display.setCursor(37,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==1||mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=9)i=0;
if(i<9){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(70+20*(i%3),(i/3)*7+10);
display.print("*");
display.setCursor(75,10);
display.print(cm0[0],0);
display.setCursor(95,10);
display.print(cm0[1],0);
display.setCursor(115,10);
display.print(cm0[2],0);
display.setCursor(75,16);
display.print(cm0[3],0);
display.setCursor(95,16);
display.print(cm0[4],0);
display.setCursor(115,16);
display.print(cm0[5],0);
display.setCursor(75,23);
display.print(cm0[6],0);
display.setCursor(95,23);
display.print(cm0[7],0);
display.setCursor(115,23);
display.print(cm0[8],0);
i++;
}
}
delay(delaytime);
}
void DHTxx() {
sensors.requestTemperatures();
float DHThum = dht.readHumidity();
float DHTtemp = dht.readTemperature();
float cc = sensors.getTempCByIndex(0);
float cc1 = sensors.getTempCByIndex(1);
float DHT2hum = dht2.readHumidity();
float DHT2temp = dht2.readTemperature();
display.print("T & H ");
if ( mode1 >= 5 ) mode1 = 1;
display.print("t = ");
delaytime = R / 100 * 100 + 100;
display.print(delaytime + 400);
display.print(" ms");
display.print(" m");
display.println(mode1);
if (mode1 == 1) {
display.print(DHThum, 0); //小數點0位
display.print("% ");
display.print(DHTtemp, 1); //小數點1位
display.println("C");
display.setTextSize(2);
if (cc > -127) {
display.print(cc, 1);
}
display.print("C");
if (i >= 6)i = 0;
if (i < 6) {
if (cc > -170) {
cm0[i] = cc;
} else {
cm0[i] = 0;
}
display.setTextSize(1);
display.setCursor(60 + 30 * (i % 2), (i / 2) * 6 + 10);
display.print("*");
display.setCursor(65, 10);
display.print(cm0[0], 1);
display.setCursor(95, 10);
display.print(cm0[1], 1);
display.setCursor(65, 16);
display.print(cm0[2], 1);
display.setCursor(95, 16);
display.print(cm0[3], 1);
display.setCursor(65, 22);
display.print(cm0[4], 1);
display.setCursor(95, 22);
display.print(cm0[5], 1);
i++;
}
}
if (mode1 == 2) {
if (i < 5) {
if (DHThum >= 0) {
cm0[i] = DHThum;
} else {
cm0[i] = 0;
}
if (i < 5) {
if (DHT2hum >= 0) {
cm1[i] = DHT2hum;
} else {
cm1[i] = 0;
}
if (i >= 4)i = 0;
display.setTextSize(1);
display.setCursor(5, 10);
display.print(" H1 H2");
display.setCursor(65, 10);
display.print(" H1 H2");
display.setCursor(60 * (i % 2), (i / 2) * 4 + 16);
display.print("*");
display.setCursor(8, 16);
display.print(cm0[1], 0);
display.print("% ");
display.print(cm1[1], 0);
display.print("%");
display.setCursor(68, 16);
display.print(cm0[2], 0);
display.print("% ");
display.print(cm1[2], 0);
display.print("%");
display.setCursor(8, 22);
display.print(cm0[3], 0);
display.print("% ");
display.print(cm1[3], 0);
display.print("%");
display.setCursor(68, 22);
display.print(cm0[4], 0);
display.print("% ");
display.print(cm1[4], 0);
display.print("%");
}
}
i++;
}
display.display();
display.clearDisplay();
delay(delaytime);
delaytime = 0;
}
void motor(){
R = 0;
R = analogRead(potPin);
R = map(R, 0, 1023, 0, 175); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor 5 ~ 180");
display.setTextSize(2);
display.print("angle ");
display.print(R+5);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
int cR = 0, cG = 0, cB = 0;
R = 0;
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR*0.8);//調弱輸出電壓,保護LED燈跟眼睛
analogWrite(ledGPin, cG*0.8);
analogWrite(ledBPin, cB*0.8);
}
void openmenu(){
display.setTextSize(1);
display.print("By.orangeV TCJHS.lab");
display.drawBitmap(0, 8, PHYduino_Logo, 128, 64, WHITE);
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Photoresistor(){
int cR = 0, cG = 0, cB = 0;
int pr = analogRead(Photo);
if ( mode1 >= 5 ) mode1 = 1;
display.print("Photo");
if(mode1==1){
display.println(" W");
cR=200;
cG=200;
cB=200;
}else if(mode1==2){
display.println(" R");
cR=200;
}else if(mode1==3){
display.println(" G");
cG=200;
}else if(mode1==4){
display.println(" B");
cB=200;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
Serial.println(pr);
display.setTextSize(2);
display.print("Light ");
display.print(pr);
delay(15);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);//調弱輸出電壓,保護LED燈跟眼睛
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
* design by orangeV,taiwan,tainan 台南大成國中 蔡智恆
* need libraries 需要程式庫<Servo.h>,<DHT.h>,<SPI.h>,<Wire.h>,<Adafruit_GFX.h>,<Adafruit_SSD1306.h>,<OneWire.h>,<DallasTemperature.h>
* Acknowledgments: 龍山國中 鄭志鵬、Proman Huang
*/
int
//對應腳位
//0
//1
//2中斷能力腳位,光閘2
//3~PWM中斷能力腳位
motorPin = 4, //4步進馬達
ledRPin = 5, //5~PWM紅LED
ledGPin = 6, //6~PWM綠LED
ds18b20 = 7, //7溫度計ds18b20
//8光閘1
buzzer = 9, //9,蜂鳴器 (蜂鳴器可以讓出來)
button = 10, //10,按鈕
ledBPin = 11, //11~PWM藍LED
trigPin = 12, //12超聲波測距Trig Pin
echoPin = 13, //13超聲波測距Echo Pin
Photo = A0, //A0光敏電阻
DHTPin = 15, //A1溫濕度DHT
DHT2Pin = 16, //A2溫濕度DHT2
potPin = 17, //A3可變電阻旋鈕
//A4
//A5
//A6
//A7
//常數
R = 0,
menu1N = 5,
menu2N = 7,
delaytime = 100;
float cm = 0.0, cm0[9] = {0}, cm1[9];
int i=0 , j;
//motor
#include <Servo.h>
Servo myservo;
//DHT11/22
#include <DHT.h>
DHT dht(DHTPin, DHT11);
DHT dht2(DHT2Pin, DHT11);
//D18B20
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);
//OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
//OLEDsetting
#define LOGO16_GLCD_HEIGHT 32
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
const unsigned char PHYduino_Logo [] PROGMEM = {
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x5f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0x67, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe6,
0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6,
0x66, 0xff, 0xfc, 0xff, 0xfc, 0xe0, 0x38, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x66,
0x66, 0x00, 0x14, 0xa0, 0x14, 0xa0, 0x68, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x66,
0x66, 0xfc, 0x0c, 0xc0, 0x0c, 0xc0, 0x98, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x66,
0x66, 0x0c, 0x0c, 0xc0, 0x00, 0x83, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xa6,
0x66, 0x0c, 0x0c, 0xc0, 0x00, 0x8c, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0xe6,
0x66, 0x0c, 0x1c, 0xe0, 0x00, 0xd0, 0x18, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x06,
0x66, 0xf7, 0xf4, 0xa0, 0x1c, 0xa0, 0x39, 0xff, 0xf7, 0x80, 0x7f, 0xc3, 0xf3, 0x06, 0x00, 0xe6,
0x66, 0xac, 0x18, 0xff, 0xf4, 0xff, 0xe9, 0x40, 0x1e, 0x80, 0x51, 0x42, 0xd3, 0x05, 0xff, 0xa6,
0x66, 0xcc, 0x00, 0x00, 0x0c, 0x00, 0x39, 0x80, 0x19, 0x80, 0xf1, 0xf3, 0x33, 0x06, 0x00, 0x66,
0x66, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x19, 0x80, 0x19, 0x83, 0x31, 0x98, 0x33, 0x06, 0x00, 0x66,
0x66, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x19, 0x80, 0x19, 0x8c, 0x31, 0x8e, 0x33, 0x06, 0x00, 0x66,
0x66, 0x0e, 0x00, 0x00, 0x0c, 0x00, 0x39, 0xc0, 0x39, 0xb0, 0x31, 0x83, 0xb3, 0x92, 0x00, 0x66,
0x66, 0x0a, 0x00, 0x00, 0x14, 0x00, 0x29, 0x40, 0x29, 0x40, 0x51, 0x40, 0xd2, 0xad, 0xff, 0xa6,
0x67, 0x0f, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xf9, 0xc0, 0x73, 0xc6, 0x00, 0xe6,
0x65, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x66,
0x67, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf6,
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e,
0x5f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//menu
int mode1=1, mode2=0;
void setup() {
Serial.begin(9600);
//buzzer
pinMode(buzzer,OUTPUT);
//soundsonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//ds18b20
sensors.begin();
//button
pinMode(button, INPUT_PULLUP);
//DHT
Wire.begin();
dht.begin();
dht2.begin();
//motor
myservo.attach(motorPin);
//Photoresistor
pinMode(Photo, INPUT);
//OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.drawBitmap(0, 8, PHYduino_Logo, 128, 64, WHITE);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
display.print("By.orangeV TCJHS.lab");
display.display();
delay(1500);
display.clearDisplay();
}
void loop() {
int sw = digitalRead(button);
R = analogRead(potPin);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
if(sw == HIGH)menu();
if(mode2 == 6)DHTxx();
if(mode2 == 1)RGBLED();
if(mode2 == 2)Buzzer();
if ( mode2 != 2 ) noTone(buzzer);
if(mode2 == 3)supersonic();
if(mode2 == 4)motor();
if ( mode2 != 4 ) myservo.write(0);
if(mode2 == 5)openmenu();
if(mode2 == 0)Photoresistor();
}
void menu(){
int k0,k1,tt=0;
AA1: k0=digitalRead(button); delay(30);
k1 = digitalRead(button);
if (k1==LOW) goto AA1; //判断按键是否低电平,如果低,循环检测,如果高则下一句
AA2: tt++; //对按键高电平计数
k0 = digitalRead(button); delay(10);
k1 = digitalRead(button);
if (k1==HIGH) goto AA2; //判断按键是否高电平,如果高,继续计数,如果低电平,则表示按键结束
if (tt<30) mode1 = mode1 + 1; //如果按键时间小于30*10ms=0.3s
if (tt>80) mode2 = mode2 + 1; //如果按键时间大于80*10ms=0.8s
if ( mode2 == menu2N ) mode2 = 0;
if ( mode1 == menu1N ) mode1 = 1;
}
void Buzzer(){
R = analogRead(potPin);
int R1=0,R10=0;
if ( mode1 >= 3 ) mode1 = 1;
display.print("Sound");
display.setCursor(40,0);
if (mode1==1){
display.println(" 31 ~ 1000 HZ");
}else{
display.println(" 31 ~ 10000 HZ");
}
display.setTextSize(2);
if(R<31){
noTone(buzzer);
display.print(0);
}else{
if(mode1==1){
R1 = R - 10;
tone(buzzer,R1);
display.print(R1);
}else if(mode1==2){
R10 = R*10-100;
tone(buzzer,R10);
display.print(R10);
}
}
display.setCursor(60,12);
display.println("Hz");
display.display();
display.clearDisplay();
delay(100);
}
void supersonic(){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn (echoPin, HIGH),
cm = (duration) / 58.2
;
if ( mode1 >= 3 ) mode1 = 1;
display.print("SoundSonic 2to60");
display.setCursor(100,0);
display.print("mod");
display.println(mode1);
display.setTextSize(1);
display.print("t= ");
R = analogRead(potPin);
delaytime = R/100 * 100+100;
if(mode1==2) delaytime = delaytime/10;
display.print(delaytime);
if(mode1==1) delaytime = delaytime - 90;
if(mode1==2) delaytime = delaytime - 10;
display.setCursor(44,8);
display.println("ms");
int cm10=cm*10;
display.setTextSize(2);
if (cm<60&&cm>2){
display.print(cm10/10);
display.setTextSize(1);
display.setCursor(25,23);
display.print(".");
int cm100=cm*100;
display.print(cm100%100/10);
}else{
display.print("err");
}
display.setTextSize(2);
display.setCursor(37,16);
display.println("CM");
display.display();
display.clearDisplay();
if(mode1==1||mode1==2){
Serial.print(cm);
Serial.print("CM");
Serial.print(delaytime);
Serial.println("ms");
if(i>=9)i=0;
if(i<9){
if (cm<60&&cm>3){
cm0[i]=cm;
}else{
cm0[i]=0;
}
display.setTextSize(1);
display.setCursor(70+20*(i%3),(i/3)*7+10);
display.print("*");
display.setCursor(75,10);
display.print(cm0[0],0);
display.setCursor(95,10);
display.print(cm0[1],0);
display.setCursor(115,10);
display.print(cm0[2],0);
display.setCursor(75,16);
display.print(cm0[3],0);
display.setCursor(95,16);
display.print(cm0[4],0);
display.setCursor(115,16);
display.print(cm0[5],0);
display.setCursor(75,23);
display.print(cm0[6],0);
display.setCursor(95,23);
display.print(cm0[7],0);
display.setCursor(115,23);
display.print(cm0[8],0);
i++;
}
}
delay(delaytime);
}
void DHTxx() {
sensors.requestTemperatures();
float DHThum = dht.readHumidity();
float DHTtemp = dht.readTemperature();
float cc = sensors.getTempCByIndex(0);
float cc1 = sensors.getTempCByIndex(1);
float DHT2hum = dht2.readHumidity();
float DHT2temp = dht2.readTemperature();
display.print("T & H ");
if ( mode1 >= 5 ) mode1 = 1;
display.print("t = ");
delaytime = R / 100 * 100 + 100;
display.print(delaytime + 400);
display.print(" ms");
display.print(" m");
display.println(mode1);
if (mode1 == 1) {
display.print(DHThum, 0); //小數點0位
display.print("% ");
display.print(DHTtemp, 1); //小數點1位
display.println("C");
display.setTextSize(2);
if (cc > -127) {
display.print(cc, 1);
}
display.print("C");
if (i >= 6)i = 0;
if (i < 6) {
if (cc > -170) {
cm0[i] = cc;
} else {
cm0[i] = 0;
}
display.setTextSize(1);
display.setCursor(60 + 30 * (i % 2), (i / 2) * 6 + 10);
display.print("*");
display.setCursor(65, 10);
display.print(cm0[0], 1);
display.setCursor(95, 10);
display.print(cm0[1], 1);
display.setCursor(65, 16);
display.print(cm0[2], 1);
display.setCursor(95, 16);
display.print(cm0[3], 1);
display.setCursor(65, 22);
display.print(cm0[4], 1);
display.setCursor(95, 22);
display.print(cm0[5], 1);
i++;
}
}
if (mode1 == 2) {
if (i < 5) {
if (DHThum >= 0) {
cm0[i] = DHThum;
} else {
cm0[i] = 0;
}
if (i < 5) {
if (DHT2hum >= 0) {
cm1[i] = DHT2hum;
} else {
cm1[i] = 0;
}
if (i >= 4)i = 0;
display.setTextSize(1);
display.setCursor(5, 10);
display.print(" H1 H2");
display.setCursor(65, 10);
display.print(" H1 H2");
display.setCursor(60 * (i % 2), (i / 2) * 4 + 16);
display.print("*");
display.setCursor(8, 16);
display.print(cm0[1], 0);
display.print("% ");
display.print(cm1[1], 0);
display.print("%");
display.setCursor(68, 16);
display.print(cm0[2], 0);
display.print("% ");
display.print(cm1[2], 0);
display.print("%");
display.setCursor(8, 22);
display.print(cm0[3], 0);
display.print("% ");
display.print(cm1[3], 0);
display.print("%");
display.setCursor(68, 22);
display.print(cm0[4], 0);
display.print("% ");
display.print(cm1[4], 0);
display.print("%");
}
}
i++;
}
display.display();
display.clearDisplay();
delay(delaytime);
delaytime = 0;
}
void motor(){
R = 0;
R = analogRead(potPin);
R = map(R, 0, 1023, 0, 175); // 0-1024 -> 0-180
myservo.write(R);
display.println("motor 5 ~ 180");
display.setTextSize(2);
display.print("angle ");
display.print(R+5);
delay(15);
display.display();
display.clearDisplay();
}
void RGBLED(){
int cR = 0, cG = 0, cB = 0;
R = 0;
if ( mode1 >= 4 ) mode1 = 1;
R = analogRead(potPin);
R = map(R, 0, 1020, 0, 255); // 0-1024 -> 0-255
display.print("RGB LED");
if(mode1==1){
display.println(" R");
cR=R;
}else if(mode1==2){
display.println(" G");
cG=R;
}else if(mode1==3){
display.println(" B");
cB=R;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
display.print("R=");
display.println(cR);
display.print("G=");
display.println(cG);
display.print("B=");
display.println(cB);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR*0.8);//調弱輸出電壓,保護LED燈跟眼睛
analogWrite(ledGPin, cG*0.8);
analogWrite(ledBPin, cB*0.8);
}
void openmenu(){
display.setTextSize(1);
display.print("By.orangeV TCJHS.lab");
display.drawBitmap(0, 8, PHYduino_Logo, 128, 64, WHITE);
display.display();
display.clearDisplay();
R = 0;
mode1 = 1;
}
void Photoresistor(){
int cR = 0, cG = 0, cB = 0;
int pr = analogRead(Photo);
if ( mode1 >= 5 ) mode1 = 1;
display.print("Photo");
if(mode1==1){
display.println(" W");
cR=200;
cG=200;
cB=200;
}else if(mode1==2){
display.println(" R");
cR=200;
}else if(mode1==3){
display.println(" G");
cG=200;
}else if(mode1==4){
display.println(" B");
cB=200;
}else{
display.println(" clear");
cR=0;
cG=0;
cB=0;
}
Serial.println(pr);
display.setTextSize(2);
display.print("Light ");
display.print(pr);
delay(15);
display.display();
display.clearDisplay();
analogWrite(ledRPin, cR);//調弱輸出電壓,保護LED燈跟眼睛
analogWrite(ledGPin, cG);
analogWrite(ledBPin, cB);
}
厲害...
回覆刪除看這麼多程式碼,老花又發作了