วันจันทร์ที่ 18 ธันวาคม พ.ศ. 2560

กฏการอินดี้

คำสั่ง attachInterrupt() เป็นคำสั่งกำหนด และสร้างอินเตอร์รัพท์ โดยมีรูปแบบการใช้งานดังนี้
รูปแบบคำสั่ง attachInterrupt(interrupt, ISR, mode)
  • interrupt คือลำดับขาอินเตอร์รัพท์ตามที่ได้ดูไปในตารางที่ผ่านมา ใน Arduino Due สามารถใส่หมายเลขขาลงไปได้เลย
  • ISR คือชื่อฟังก์ชั่นที่จะไปถูกเรียกขึ้นมาเมื่อเกิดอินเตอร์รัพท์
  • mode รูปแบบที่จะให้เกิดอินเตอร์รัพท์ มีทั้งหมด 4 รูปแบบดังนี้
    • LOW จะเกิดอินเตอร์รัพท์ต่อเมื่อพอร์ตที่กำหนดไว้มีสถานะเป็น LOW
    • CHANGE จะเกิดอินเตอร์รัพท์เมื่อพอร์ตที่กำหนดไว้มีการเปลี่ยนสถานะ เช่น จากสถานะ HIGH เป็น LOW หรือจาก LOW เป็น HIGH
    • RISING จะเกิดอินเตอร์รัพท์เมื่อพอร์ตที่กำหนดไว้มีการเปลี่ยนสถานะจาก LOW เป็น HIGH
    • FALLING จะเกิดอินเตอร์รัพท์เมื่อพอร์ตที่กำหนดไว้มีการเปลี่ยนสถานะจาก HIGH เป็น LOW
    • HIGH จะเกิดอินเตอร์รัพท์ต่อเมื่อพอร์ตที่กำหนดไว้มีสถานะเป็น HIGH

อ้าวเรา v.3

/*  NETPIE ESP8266 basic sample                            */
/*  More information visit : https://netpie.io             */

#include <ESP8266WiFi.h>
#include <MicroGear.h>

const char* ssid     = "Tony24HoursEX";
const char* password = "123456789123";

#define APPID   "Tony24Hours3"
#define KEY     "hUqvkEgoRp5xCRM"
#define SECRET  "7a8d2U9QQut3SOInwYwFoBIdt"
#define ALIAS   "Node2"

#include <Wire.h>                   // Include library
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 OLED(OLED_RESET);  // New object OLED

WiFiClient client;

char str[32];
int vr    = 0;
int timer = 0;
int vr1   = 0;
MicroGear microgear(client);

void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen)
{
  Serial.print("Incoming message --> ");
  msg[msglen] = '\0';
  Serial.println((char *)msg); 
  vr1 = atoi((char *)msg);
  Serial.print("Vr1 = ");
  Serial.println(vr1);
}
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen)
{
  Serial.println("Connected to NETPIE...");
  microgear.setAlias(ALIAS);
}
void setup()
{
  OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  pinMode(A0, INPUT);
  /* Add Event listeners */
  /* Call onMsghandler() when new message arraives */
  microgear.on(MESSAGE, onMsghandler);
  /* Call onConnected() when NETPIE connection is established */
  microgear.on(CONNECTED, onConnected);

  Serial.begin(115200);
  Serial.println("Starting...");

  /* Initial WIFI, this is just a basic method to configure WIFI on ESP8266.                       */
  /* You may want to use other method that is more complicated, but provide better user experience */
 unsigned long timeoutconnect = millis();
  if (WiFi.begin(ssid, password))
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
      if (millis() - timeoutconnect > 8000)
      {
        break;
      }
    }
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  /* Initial with KEY, SECRET and also set the ALIAS here */
  microgear.init(KEY, SECRET, ALIAS);

  /* connect to NETPIE to a specific APPID */
  microgear.connect(APPID);
}

void loop()
{
  if (microgear.connected())
  {
    microgear.loop();
    vr = analogRead(A0);
    if(vr>200)
      {
        vr = 200;
      }
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(50,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1 
      OLED.println("VALUE");
      OLED.setCursor(47,10);
      OLED.setTextSize(2);
      OLED.println(String(vr));             //Type message
      OLED.display();
    if(timer>=1000)
    {         
      Serial.println("connected");   
      sprintf(str,"%d,%d",vr,vr1);
      microgear.publish("/Vr1",str);
      timer = 0;
    }
  else timer += 100;
  }
  else
  {
    Serial.println("connection lost, reconnect...");
    if (timer >= 5000)
    {
      microgear.connect(APPID);
      timer = 0;
    }
    else timer += 100;
  }
  delay(100);
}

อ้าวเห้ย v.2 นิ

/*  NETPIE ESP8266 basic sample                            */
/*  More information visit : https://netpie.io             */

#include <ESP8266WiFi.h>
#include <MicroGear.h>

const char* ssid     = "Tony24HoursEX";
const char* password = "123456789123";

#define APPID   "Tony24Hours3"
#define KEY     "hUqvkEgoRp5xCRM"
#define SECRET  "7a8d2U9QQut3SOInwYwFoBIdt"
#define ALIAS   "Node2"

#include <Wire.h>                   // Include library
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 OLED(OLED_RESET);  // New object OLED

WiFiClient client;

char str[32];
int vr    = 0;
int timer = 0;
int vr1   = 0;
MicroGear microgear(client);

void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen)
{
  Serial.print("Incoming message --> ");
  msg[msglen] = '\0';
  Serial.println((char *)msg); 
  vr1 = atoi((char *)msg);
  Serial.print("Vr1 = ");
  Serial.println(vr1);
}
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen)
{
  Serial.println("Connected to NETPIE...");
  microgear.setAlias(ALIAS);
}
void setup()
{
  OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  pinMode(A0, INPUT);
  /* Add Event listeners */
  /* Call onMsghandler() when new message arraives */
  microgear.on(MESSAGE, onMsghandler);
  /* Call onConnected() when NETPIE connection is established */
  microgear.on(CONNECTED, onConnected);

  Serial.begin(115200);
  Serial.println("Starting...");

  /* Initial WIFI, this is just a basic method to configure WIFI on ESP8266.                       */
  /* You may want to use other method that is more complicated, but provide better user experience */
 unsigned long timeoutconnect = millis();
  if (WiFi.begin(ssid, password))
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
      if (millis() - timeoutconnect > 8000)
      {
        break;
      }
    }
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  /* Initial with KEY, SECRET and also set the ALIAS here */
  microgear.init(KEY, SECRET, ALIAS);

  /* connect to NETPIE to a specific APPID */
  microgear.connect(APPID);
}

void loop()
{
  if (microgear.connected())
  {
    microgear.loop();
    vr = analogRead(A0);
    if(vr>200)
      {
        vr = 200;
      }
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(50,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1   
      OLED.println("VALUE");
      OLED.setCursor(47,10);
      OLED.setTextSize(2);
      OLED.println(String(vr));             //Type message
      OLED.display();
    if(timer>=1000)
    {           
      Serial.println("connected");     
      sprintf(str,"%d,%d",vr,vr1);
      microgear.publish("/Vr1",str);
      timer = 0;
    }
  else timer += 100;
  }
  else
  {
    Serial.println("connection lost, reconnect...");
    if (timer >= 5000)
    {
      microgear.connect(APPID);
      timer = 0;
    }
    else timer += 100;
  }
  delay(100);
}

อ้าว v.1

/*  NETPIE ESP8266 basic sample                            */
/*  More information visit : https://netpie.io             */

#include <ESP8266WiFi.h>
#include <MicroGear.h>

const char* ssid     = "Tony24HoursEX";
const char* password = "123456789123";

#define APPID   "Tony24Hours3"
#define KEY     "hUqvkEgoRp5xCRM"
#define SECRET  "7a8d2U9QQut3SOInwYwFoBIdt"
#define ALIAS   "Node1"

#include <Wire.h>                   // Include library
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 OLED(OLED_RESET);  // New object OLED

WiFiClient client;


int vr    = 0;
int timer = 0;

MicroGear microgear(client);

void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen)
{
  Serial.print("Incoming message --> ");
  msg[msglen] = '\0';
  Serial.println((char *)msg); 
}
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen)
{
  Serial.println("Connected to NETPIE...");
  microgear.setAlias(ALIAS);
}
void setup()
{
  OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  pinMode(A0, INPUT);
  /* Add Event listeners */
  /* Call onMsghandler() when new message arraives */
  microgear.on(MESSAGE, onMsghandler);
  /* Call onConnected() when NETPIE connection is established */
  microgear.on(CONNECTED, onConnected);

  Serial.begin(115200);
  Serial.println("Starting...");

  /* Initial WIFI, this is just a basic method to configure WIFI on ESP8266.                       */
  /* You may want to use other method that is more complicated, but provide better user experience */
 unsigned long timeoutconnect = millis();
  if (WiFi.begin(ssid, password))
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
      if (millis() - timeoutconnect > 8000)
      {
        break;
      }
    }
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  /* Initial with KEY, SECRET and also set the ALIAS here */
  microgear.init(KEY, SECRET, ALIAS);

  /* connect to NETPIE to a specific APPID */
  microgear.connect(APPID);
}

void loop()
{
  if (microgear.connected())
  {
    vr = analogRead(A0);
    if(vr>500)
    {
      vr = 500;
    }
    OLED.clearDisplay();               //Clear display
    OLED.setTextColor(WHITE);          //Set text color
    OLED.setCursor(50,0);               //Set display start position
    OLED.setTextSize(1);               //Set text size x1   
    OLED.println("VALUE");
    OLED.setCursor(50,10);
    OLED.setTextSize(2);
    OLED.println(String(vr));             //Type message
    OLED.display();
    if(timer>=1000)
  {
    microgear.loop();   
    Serial.println("connected");   
    microgear.chat("Node2",vr);
    //microgear.publish("/Vr1",String(vr));
    timer = 0;
  }
  else timer += 100;
  }
  else
  {
    Serial.println("connection lost, reconnect...");
    if (timer >= 5000)
    {
      microgear.connect(APPID);
      timer = 0;
    }
    else timer += 100;
  }
  delay(100);
}

โตนสีเสียงเลือกๆ

#include <Wire.h>                   // Include library
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 OLED(OLED_RESET);  // New object OLED
#include <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);

#define buzzer D8

int sl = 0;
int i  = 0;
int sw = 0;
int t  = 0;
int w  = 0;
int x  = 0;
int y  = 0;
void setup() 
{
  Serial.begin(115200);
  pinMode(A0,INPUT);
  pinMode(D3,INPUT);
  OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  // initialize with the I2C addr 0x3C (for the 128x64)
  SLEDs.begin(); // This initializes the NeoPixel library.
  SLEDs.show(); // Initialize all SLEDs to 'off'
  pinMode(buzzer, OUTPUT);
  tone(buzzer, 1000);
  delay(1000);
}
void loop()
{
  i = 1;
  while(i==1)
  {
    noTone(buzzer); 
    delay(300);
    sl = analogRead(A0);
    sl = map(sl,0,1023,0,3);
    if(sl==0)
    {
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("=> SLED1");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   SLED2");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   SPK");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        i = 2; 
      }
    }
    if(sl==1)
    {
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   SLED1");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("=> SLED2");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   SPK");             //Type message
      OLED.display();
      sw = digitalRead(D3); 
      if(sw==0)
      {
        i = 3; 
      } 
    }
    if(sl==2)
    {
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   SLED1");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   SLED2");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> SPK");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        i = 4; 
      } 
    }
  }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  while(i==2)
  {
    delay(300);
    sl = analogRead(A0);
    sl = map(sl,0,1023,0,5);
    if(sl==0)
    {
      w = 0;
      x = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("=> RED");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   GREEN");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   BLUE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        t = 1; 
      }
      if(t==1)
      {
        SLEDs.setPixelColor(0, SLEDs.Color(255, 0, 0));
        SLEDs.show();
      }
    }
    if(sl==1)
    {
      t = 0;
      x = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("=> GREEN");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   BLUE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   WHITE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        w = 1; 
      }
      if(w==1)
      {
        SLEDs.setPixelColor(0, SLEDs.Color(0, 255, 0));
        SLEDs.show();
      }
    }
    if(sl==2)
    {
      t = 0;
      w = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   GREEN");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("=> BLUE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   WHITE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        x = 1; 
      }
      if(x==1)
      {
        SLEDs.setPixelColor(0, SLEDs.Color(0,0,255));
        SLEDs.show();
      }
    }
    if(sl==3)
    {
      t = 0;
      w = 0;
      x = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   GREEN");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   BLUE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> WHITE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        y = 1;
      }
      if(y==1)
      {
        SLEDs.setPixelColor(0, SLEDs.Color(255, 255, 255));
        SLEDs.show();
      }
    }
    if(sl==4)
    {
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   BLUE");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   WHITE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> EXIT");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        t = 0;
        w = 0;
        x = 0;
        y = 0;
        i = 0;
        sw= 1;
      }
    }
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  while(i==3)
  {
    delay(300);
    sl = analogRead(A0);
    sl = map(sl,0,1023,0,5);
    if(sl==0)
    {
      w = 0;
      x = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("=> RED");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   GREEN");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   BLUE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        t = 1; 
      }
      if(t==1)
      {
        SLEDs.setPixelColor(1, SLEDs.Color(255, 0, 0));
        SLEDs.show();
      }
    }
    if(sl==1)
    {
      t = 0;
      x = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("=> GREEN");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   BLUE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   WHITE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        w = 1; 
      }
      if(w==1)
      {
        SLEDs.setPixelColor(1, SLEDs.Color(0, 255, 0));
        SLEDs.show();
      }
    }
    if(sl==2)
    {
      t = 0;
      w = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   GREEN");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("=> BLUE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   WHITE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        x = 1; 
      }
      if(x==1)
      {
        SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 255));
        SLEDs.show();
      }
    }
    if(sl==3)
    {
      t = 0;
      w = 0;
      x = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   GREEN");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   BLUE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> WHITE");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        y = 1;
      }
      if(y==1)
      {
        SLEDs.setPixelColor(1, SLEDs.Color(255, 255, 255));
        SLEDs.show();
      }
    }
    if(sl==4)
    {
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   BLUE");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   WHITE");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> EXIT");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        t = 0;
        w = 0;
        x = 0;
        y = 0;
        i = 0;
        sw= 1;
      }
    }
  }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  while(i==4)
  {
    delay(300);
    sl = analogRead(A0);
    sl = map(sl,0,1023,0,4);
    if(sl==0)
    {
      w = 0;
      x = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("=> 500Hz");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   1kHz");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   2kHz");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        t = 1; 
      }
      if(t==1)
      {
        tone(buzzer, 500);
      }
    }
    if(sl==1)
    {
      t = 0;
      x = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   500Hz");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("=> 1kHz");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("   2kHz");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        w = 1; 
      }
      if(w==1)
      {
        tone(buzzer, 1000);
      }
    }
    if(sl==2)
    {
      t = 0;
      w = 0;
      y = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   500Hz");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   1kHz");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> 2kHz");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        x = 1; 
      }
      if(x==1)
      {
        tone(buzzer, 2000);
      }
    }
    if(sl==3)
    {
      t = 0;
      w = 0;
      x = 0;
      OLED.clearDisplay();               //Clear display
      OLED.setTextColor(WHITE);          //Set text color
      OLED.setCursor(0,0);               //Set display start position
      OLED.setTextSize(1);               //Set text size x1
      OLED.println("   1kHz");             //Type message
      OLED.setCursor(0,10);             //Set display postion              //Set text size x2
      OLED.println("   2kHz");             //Type message
      OLED.setCursor(0,20);             //Set display postion              //Set text size x2
      OLED.println("=> EXIT");             //Type message
      OLED.display();
      sw = digitalRead(D3);
      if(sw==0)
      {
        noTone(buzzer); 
        t = 0;
        w = 0;
        x = 0;
        y = 0;
        i = 0;
        sw= 1;
      }
    }
  }
}

วุ่มโลเลือกๆ

#include <Wire.h>                 
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);

#include  <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);

int index1 = 0;
int index2 = 0;
int index3 = 0;
int index4 = 0;
int sel = 0;
unsigned long changeTime;

void DisplayOLED(String Line1,String Line2,String Line3)
{
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0);display.print(Line1);
  display.setCursor(0,8);display.print(Line2);
  display.setCursor(0,16);display.print(Line3);
  display.display();
}
int AnalogRead()
{
  int tempRead;
  int anaRead = analogRead(A0);
  delay(100);
  if(anaRead <= 300) tempRead = 0;
  if(anaRead > 300 && anaRead < 600) tempRead = 1;
  if(anaRead > 600) tempRead = 2;
  Serial.println(anaRead);
  return tempRead;
}
void clrOut()
{
  DisplayOLED("OUT"," OUT","  OUT");
  delay(1000);
  sel = 0;
  digitalWrite(D0,LOW);
  digitalWrite(D4,LOW);
  digitalWrite(D5,LOW);
}
void setup()
{
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
  pinMode(D1,INPUT);
  pinMode(D0,OUTPUT);
  pinMode(D4,OUTPUT);
  pinMode(D5,OUTPUT);
  SLEDs.begin();
  SLEDs.show();
}

void loop()
{
  index1 = AnalogRead();
  if(digitalRead(D1) ==0)
  {
    while(digitalRead(D1) ==0)
    {
      delay(10);
    }
    sel = 1;
    if(index1 == 0)
    {
      while(index1 == 0 && sel == 1)
      {
        index2 = AnalogRead();
        if(index2 == 0)
        {
          DisplayOLED("->RED","  GREEN","  BLUE");
          SLEDs.setPixelColor(0,  SLEDs.Color(255,  0,  0));    // แดง
          //SLEDs.setPixelColor(0,  SLEDs.Color(0,  255,  64));    // เขียว
          //SLEDs.setPixelColor(0,  SLEDs.Color(255,  255,  0));    // เหลือง
          SLEDs.show();
        }
        else if(index2 == 1)
        {
          DisplayOLED("  RED","->GREEN","  BLUE");
          SLEDs.setPixelColor(0,  SLEDs.Color(0,  255,  64));
          SLEDs.show();
        }
        else if(index2 == 2)
        {
          DisplayOLED("  RED","  GREEN","->BLUE");
          SLEDs.setPixelColor(0,  SLEDs.Color(0,  0,  255));    // น้ำเงิน
          SLEDs.show();
        }
      }
    }
    else if (index1 == 1)
    {
      while(index1 == 1 && sel == 1)
      {
        index3 = AnalogRead();
        if(index3 == 0)
        {
          DisplayOLED("->RED","  GREEN","  BLUE");
          SLEDs.setPixelColor(1,  SLEDs.Color(255,  0,  0));    // แดง
          SLEDs.show();
        }
        else if(index3 == 1)
        {
          DisplayOLED("  RED","->GREEN","  BLUE");
          SLEDs.setPixelColor(1,  SLEDs.Color(0,  255,  64));
          SLEDs.show();
        }
        else if(index3 == 2)
        {
          DisplayOLED("  RED","  GREEN","->BLUE");
          SLEDs.setPixelColor(1,  SLEDs.Color(0,  0,  255));    // น้ำเงิน
          SLEDs.show();
        }
      }
    }
    else if (index1 == 2)
    {
      while(index1 == 2 && sel == 1)
      {
        index4 = AnalogRead();
        if(index4 == 0)
        {
          DisplayOLED("->LED1","  LED2","  LED3");
          digitalWrite(D0,HIGH); digitalWrite(D5,LOW); digitalWrite(D4,LOW);
        }
        else if(index4 == 1)
        {
          DisplayOLED("  LED1","->LED2","  LED3");
          digitalWrite(D0,LOW); digitalWrite(D5,HIGH); digitalWrite(D4,LOW);
        }
        else if(index4 == 2)
        {
          DisplayOLED("  LED1","  LED2","->LED3");
          digitalWrite(D0,LOW); digitalWrite(D5,LOW); digitalWrite(D4,HIGH);
        }
      }
    }
  }
  else
  {
    if(index1 == 0)DisplayOLED("->SLED1","  SLED2","  LED");
    if(index1 == 1)DisplayOLED("  SLED1","->SLED2","  LED");
    if(index1 == 2)DisplayOLED("  SLED1","  SLED2","->LED");
  }
 
 
}

เช้งคัลเลอร์

#include  <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);

int LED1 = D0;
int LED2 = D4;
int LED3 = D5;
int Sw1 = D6;
int Sw2 = D7;
int changeTime;

void setup()
{
    Serial.begin(9600);
    SLEDs.begin();
    SLEDs.show();
    attachInterrupt(digitalPinToInterrupt(Sw1), INT1, FALLING);
    attachInterrupt(digitalPinToInterrupt(Sw2), INT2, FALLING);
    pinMode(LED1,OUTPUT);
    pinMode(LED2,OUTPUT);
    pinMode(LED3,OUTPUT); 
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,LOW);   
}

void loop()
   {
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,HIGH);
    digitalWrite(LED3,HIGH);
    changeTime = millis();   
    while(digitalRead(LED3)== HIGH ){
        if(millis()-changeTime > 500) digitalWrite(LED1,LOW);
        if(millis()-changeTime > 600) digitalWrite(LED2,LOW);
        if(millis()-changeTime > 700) digitalWrite(LED3,LOW);
        delay(10);   
    }
    delay(300);
}

void INT1() {
   SLEDs.setPixelColor(0,  SLEDs.Color(255,  0,  0));    // read

   //SLEDs.setPixelColor(0,  SLEDs.Color(0,  255,  0));    // green
   //SLEDs.setPixelColor(0,  SLEDs.Color(255,  255,  0));    // yellow
   SLEDs.show();
}
void INT2() {
   //SLEDs.setPixelColor(0,  SLEDs.Color(255,  0,  0));   
   SLEDs.setPixelColor(0,  SLEDs.Color(0,  255,  64));   
 
   //SLEDs.setPixelColor(0,  SLEDs.Color(255,  255,  0)); 
   SLEDs.show();
}