把Pocket製成手錶

Pocketcard是一個可以相容microbit的ESP32開發板,因此,你可以用它來銜接Microbit擴充設備,而不需要再花一筆錢買專屬設備,在本章節中,我們利用Pocket本身的OLED螢幕與按鈕,建置一個穿戴式裝置─手錶,這個手錶主要是利用網路的NTP Server來校時,在使用上,你必須要在有WIFI的環境中,你可以用你的智慧型手機共享網路給Pocketcard。

撰寫程式前,請你要先安裝一些Arduino函式庫,這個可以透過主選單->"安裝"->"安裝Arduino函式庫"來進行安裝。

這時會從網路上面下載相關的函式庫,包括Arduino、ESP8266和ESP32

安裝完畢後,視窗會自動關閉,回到主視窗將Arduino IDE打開。

開啟了Arduino IDE後,放入下面的程式碼。PS.記得修改WIFI帳號跟密碼

#include <WiFi.h>

#include <time.h>

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET -1

#include <Tone32.h>

#define BUZZER_PIN 26

#define BUZZER_CHANNEL 0

#define BUTTONA 14

#define BUTTONB 25

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

int now_page=0;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char* ssid = "crouse";

const char* password = "spilder12";

int timezone = 8 * 3600;

int dst = 0;

int Alarm_time[]={0,0,0,0};

int pos=0;

int pos1[]={0,1,1,1,1};

void setup() {

Serial.begin(9600);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

pinMode(14,INPUT);

pinMode(25,INPUT);

// Clear the buffer.

display.clearDisplay();

display.display();

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0,0);

display.println("Wifi connecting to ");

display.println( ssid );

WiFi.begin(ssid,password);

display.println("\nConnecting");

display.display();

while( WiFi.status() != WL_CONNECTED ){

delay(500);

display.print(".");

display.display();

}

// Clear the buffer.

display.clearDisplay();

display.display();

display.setCursor(0,0);

display.println("Wifi Connected!");

display.print("IP:");

display.println(WiFi.localIP() );

display.display();

configTime(timezone, dst, "pool.ntp.org","time.nist.gov");

display.println("\nWaiting for NTP...");

while(!time(nullptr)){

Serial.print("*");

delay(1000);

}

display.println("\nTime response....OK");

display.display();

delay(1000);

display.clearDisplay();

display.display();

pinMode(14,INPUT);

}

void loop() {

time_t now = time(nullptr);

struct tm* p_tm = localtime(&now);

int r = 35;

// Now draw the clock face

if(now_page==0){

display.drawCircle(display.width()/2, display.height()/2, 2, WHITE);

//hour ticks

for( int z=0; z < 360;z= z + 30 ){

//Begin at 0° and stop at 360°

float angle = z ;

angle=(angle/57.29577951) ; //Convert degrees to radians

int x2=(64+(sin(angle)*r));

int y2=(32-(cos(angle)*r));

int x3=(64+(sin(angle)*(r-5)));

int y3=(32-(cos(angle)*(r-5)));

display.drawLine(x2,y2,x3,y3,WHITE);

}

// display second hand

float angle = p_tm->tm_sec*6 ;

angle=(angle/57.29577951) ; //Convert degrees to radians

int x3=(64+(sin(angle)*(r)));

int y3=(32-(cos(angle)*(r)));

display.drawLine(64,32,x3,y3,WHITE);

//

// display minute hand

angle = p_tm->tm_min * 6 ;

angle=(angle/57.29577951) ; //Convert degrees to radians

x3=(64+(sin(angle)*(r-3)));

y3=(32-(cos(angle)*(r-3)));

display.drawLine(64,32,x3,y3,WHITE);

//

// display hour hand

angle = p_tm->tm_hour * 30 + int((p_tm->tm_min / 12) * 6 );

angle=(angle/57.29577951) ; //Convert degrees to radians

x3=(64+(sin(angle)*(r-11)));

y3=(32-(cos(angle)*(r-11)));

display.drawLine(64,32,x3,y3,WHITE);

display.setTextSize(1);

display.setCursor((display.width()/2)+10,(display.height()/2) - 3);

display.print(p_tm->tm_mday);

// update display with all data

display.display();

delay(100);

display.clearDisplay();

} else if(now_page==1)

{

display.clearDisplay();

display.setTextSize(2);

display.setCursor(0,0);

display.print(String(p_tm->tm_yday+1900-138)+"-"+String(p_tm->tm_mon+1)+"-"+String(p_tm->tm_mday));

display.setCursor(0,30);

display.print(" "+String(p_tm->tm_hour)+":"+String(p_tm->tm_min)+":"+String(p_tm->tm_sec));

// update display with all data

display.display();

delay(100);

} else if(now_page==2)

{

display.clearDisplay();

display.setTextSize(4);

display.setCursor(0,20);

display.setTextColor(pos1[1]);

display.print(Alarm_time[0]);

display.setCursor(28,20);

display.setTextColor(pos1[2]);

display.print(Alarm_time[1]);

display.setCursor(50,20);

display.setTextColor(1);

display.print(":");

display.setCursor(65,20);

display.setTextColor(pos1[3]);

display.print(Alarm_time[2]);

display.setCursor(93,20);

display.setTextColor(pos1[4]);

display.print(Alarm_time[3]);

// update display with all data

display.display();

delay(100);

if (pos!=0)

{

pos1[pos]=(pos1[pos]+1)%2;

}

}

if (digitalRead(14)==0)

{

if (pos==0)

now_page=(now_page+1)%3;

delay(100);

if(pos==1 && now_page==2)

{

Alarm_time[0]=(Alarm_time[0]+1)%3;

}else if(pos==2 && now_page==2)

{

Alarm_time[1]=(Alarm_time[1]+1)%5;

}else if(pos==3 && now_page==2)

{

Alarm_time[2]=(Alarm_time[2]+1)%7;

}else if(pos==4 && now_page==2)

{

Alarm_time[3]=(Alarm_time[3]+1)%10;

}

} else if (digitalRead(25)==0)

{

if(now_page==2)

{

pos=(pos+1)%5;

}

if (pos==2)

{

pos1[1]=1;

}else if (pos==3)

{

pos1[2]=1;

}else if (pos==4)

{

pos1[3]=1;

}else if (pos==0)

{

pos1[4]=1;

}

delay(100);

}

int Alarmhour=Alarm_time[0]*10+Alarm_time[1];

int Alarmmin=Alarm_time[2]*10+Alarm_time[3];

if (p_tm->tm_hour==Alarmhour && p_tm->tm_min==Alarmmin && pos==0)

{

tone(BUZZER_PIN, 73, 500, BUZZER_CHANNEL);

noTone(BUZZER_PIN, BUZZER_CHANNEL);

tone(BUZZER_PIN, 82, 500, BUZZER_CHANNEL);

noTone(BUZZER_PIN, BUZZER_CHANNEL);

tone(BUZZER_PIN, 87, 500, BUZZER_CHANNEL);

noTone(BUZZER_PIN, BUZZER_CHANNEL);

tone(BUZZER_PIN, 98, 500, BUZZER_CHANNEL);

noTone(BUZZER_PIN, BUZZER_CHANNEL);

tone(BUZZER_PIN, 110, 500, BUZZER_CHANNEL);

noTone(BUZZER_PIN, BUZZER_CHANNEL);

tone(BUZZER_PIN, 123, 500, BUZZER_CHANNEL);

noTone(BUZZER_PIN, BUZZER_CHANNEL);

}

}

記得確認開發板有選Pocket以及COM Port有選對歐