#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int irsensor =5;
int irsensor1 =6;
int irsensor2 =7;// choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int counter = 0;
int counter1 = 0;
int counter2 = 0;
int currentState = 0;
int currentState1 = 0;
int currentState2 = 0;
int previousState = 0;
int previousState1 = 0;
int previousState2 = 0;
void testdrawstyles(void) {
display.clearDisplay();
display.setTextSize(1); // 設定文字大小
display.setTextColor(1); // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.setCursor(5,0); // 設定起始座標
display.print("Wong Hak King Bay"); // 要顯示的字串
display.setCursor(10,20); // 設定起始座標
display.print("Gap!Gap!Gap!"); // 要顯示的字串
display.display(); // 要有這行才會把文字顯示出來
delay(3000);
}
void setup() {
pinMode(irsensor, INPUT);
Serial.begin(9600);
// 偵測是否安裝好OLED了
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 一般1306 OLED的位址都是0x3C
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay(); // 清除畫面
testdrawstyles(); // 測試文字
delay(1000);
}
void loop() {
{
display.clearDisplay();
display.setCursor(10,20);
display.print("Red: ");
display.print(counter);
display.setCursor(10,30);
display.print("Blue: ");
display.print(counter1);
display.setCursor(10,40);
display.print("Green: ");
display.print(counter2);
display.setCursor(10,50);
display.print("Total: ");
display.print(counter+counter1+counter2);
display.display();
delay(250);
}
val = digitalRead(irsensor); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
currentState = 1;
}
else {
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
val = digitalRead(irsensor1); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
currentState1 = 1;
}
else {
currentState1 = 0;
}
if(currentState1 != previousState1){
if(currentState1 == 1){
counter1 = counter1 + 1;
Serial.println(counter1);
}
}
previousState1 = currentState1;
val = digitalRead(irsensor2); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
currentState2 = 1;
}
else {
currentState2 = 0;
}
if(currentState2 != previousState2){
if(currentState2 == 1){
counter2 = counter2 + 1;
Serial.println(counter2);
}
}
previousState2 = currentState2;
}Loading
ssd1306
ssd1306
Loading
ssd1306
ssd1306