วันจันทร์ที่ 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();
}

อินแอลเอสดี

#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 it;

void setup()
{
    Serial.begin(112500);
    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);
    //delay(500);
    digitalWrite(LED2,HIGH);
    //delay(600);
    digitalWrite(LED3,HIGH);
    //delay(700);
    it = millis();   
    while(digitalRead(LED3)== HIGH ){
        if(millis()-it > 500) digitalWrite(LED1,LOW);
        if(millis()-it > 600) digitalWrite(LED2,LOW);
        if(millis()-it > 700) digitalWrite(LED3,LOW);
        delay(10);   
    }
    delay(300);
}

void INT1() {
   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();
}
void INT2() {
   //SLEDs.setPixelColor(0,  SLEDs.Color(255,  0,  0));    // แดง
   SLEDs.setPixelColor(0,  SLEDs.Color(0,  255,  0));    // เขียว
 
   //SLEDs.setPixelColor(0,  SLEDs.Color(255,  255,  0));    // เหลือง
   SLEDs.show();
}

วนแอลเอสดี

#include <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
#define R_pin D0
#define Y_pin D4
#define G_pin D5
int st_R = 255;
int st_Y = 0;
int st_G = 0;
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);
void setup()

{
  pinMode(R_pin,OUTPUT);
  pinMode(Y_pin,OUTPUT);
  pinMode(G_pin,OUTPUT);
  SLEDs.begin(); // This initializes the NeoPixel library.
  SLEDs.show(); // Initialize all SLEDs to 'off'

}
void loop()
{
 
  for (int i = 0; i < 256; i++)
  {
    st_R = 0;
    st_Y++;
    displayPWM(st_R,st_Y,st_G);
    SLEDs.setPixelColor(0, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color red
    SLEDs.setPixelColor(1, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color blue
    SLEDs.show();
    delay(30);
   
  }
  for (int i = 0; i < 256; i++)
  {
    st_R = 0;
    st_Y++;
    displayPWM(st_R,st_Y,st_G);
     SLEDs.setPixelColor(0, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color red
  SLEDs.setPixelColor(1, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color blue
  SLEDs.show();
    delay(30);
  }
  for (int i = 0; i < 256; i++)
  {
    st_Y--;
    displayPWM(st_R,st_Y,st_G);
     SLEDs.setPixelColor(0, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color red
  SLEDs.setPixelColor(1, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color blue
  SLEDs.show();
    delay(30);
  }
  for (int i = 0; i < 256; i++)
  {
    st_Y = 0;
    st_R++;
    displayPWM(st_R,st_Y,st_G);
     SLEDs.setPixelColor(0, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color red
  SLEDs.setPixelColor(1, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color blue
  SLEDs.show();
    delay(30);
  }
  for (int i = 0; i < 256; i++)
  {
    st_G--;
   displayPWM(st_R,st_Y,st_G);
     SLEDs.setPixelColor(0, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color red
  SLEDs.setPixelColor(1, SLEDs.Color(st_R, st_Y, st_G)); //Adjust brightness, Color blue
  SLEDs.show();
    delay(30);
  }
}
void displayPWM(int R,int Y,int G)
// Drive LED with PWM by using analogWrite function
{
 analogWrite(R_pin,R);
 analogWrite(Y_pin,Y);
 analogWrite(G_pin,G);
}

วุ่มโลวาย

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

const char* ssid     = "Electronics";
const char* password = "Electronics";

#define APPID   "killinter"
#define KEY     "Xf6KHcTL2PPzUDg"
#define SECRET  "orsDQPrSMXDteAbzseXr5scCE"

#define ALIAS   "IQsoHANDSOME"

WiFiClient client;

int timer = 0;
char str[32];


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...");
    /* Set the alias of this microgear ALIAS */
    microgear.setAlias(ALIAS);
}
void setup(){
//dht.begin();
      microgear.on(MESSAGE,onMsghandler);

    /* Call onFoundgear() when new gear appear */
    //microgear.on(PRESENT,onFoundgear);

    /* Call onLostgear() when some gear goes offline */
    //microgear.on(ABSENT,onLostgear);

    /* Call onConnected() when NETPIE connection is established */
    microgear.on(CONNECTED,onConnected);

   
    pinMode(D2, INPUT);
    Serial.begin(115200);
    Serial.println("Starting...");

    if (WiFi.begin(ssid, password)) {
      while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
        }
    }

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

    microgear.init(KEY,SECRET,ALIAS);
    microgear.connect(APPID);
}

void loop(){

    int sensorValue = analogRead(A0);
    Serial.println(sensorValue);
    delay(50);

            sprintf(str,"%d",sensorValue);
            Serial.println(str);                       
            Serial.print("Sending -->");
            microgear.publish("/ldr",str);
            delay(1000);
}

บีเฮสสั่งไฟ4s

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
#define OLED_RESET -1
Adafruit_SSD1306 OLED (OLED_RESET);
int BH1750address = 0x23;
byte buff[2];
int t=0;
int temp1 = 0;
int temp2 = 0;
int changeTime;
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);
void setup()
{
 
  OLED.begin(SSD1306_SWITCHCAPVCC, 0x3c);
  SLEDs.begin(); // This initializes the NeoPixel library.
     // Initialize all SLEDs to 'off'
    Serial.begin(115200);
      SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 0));
      SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
      SLEDs.show();
}
void loop()
{
 
  changeTime = millis();
  uint16_t val = 0;
  BH1750_Init(BH1750address);
  delay(200);
  if (2 == BH1750_Read(BH1750address))
  {
   val = ((buff[0] << 8) | buff[1]) / 1.2;
   OLED.clearDisplay();
   OLED.setTextColor(WHITE);
   OLED.setCursor(0, 0);
   OLED.setTextSize(1);
   OLED.println("BH1750");
   OLED.setCursor(0, 15);
   OLED.println(String(val) + " lux");
   OLED.display();
   Serial.println(val);
 
  }
  if(buff[1]/1.2 < 80)
    {
//      t++;
      t=t+1;
//      delay(500);
      while(buff[1]/1.2 < 80)
      {
        uint16_t val = 0;
        BH1750_Init(BH1750address);             // Initial BH1750
        delay(200);
        BH1750_Read(BH1750address);    // Read and check data from BH1750
        val = ((buff[0] << 8) | buff[1])/1.2;
      }
   
      if(t==1)
    {
      SLEDs.setPixelColor(0, SLEDs.Color(255, 0, 0));
      SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
      SLEDs.show();
      //delay(50);
    }
    if(t==3)
    {
      SLEDs.setPixelColor(0, SLEDs.Color(0, 255, 0));
      SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
      SLEDs.show();
     // delay(50);
    }
     if(t==6)
    {
      SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 255));
      SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
      SLEDs.show();
     
      //delay(50);
    }
   
    //showtime = millis();
 
     if(millis()-changeTime > 4000)
    {
      //Serial.println();
      SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 0));
      SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
      SLEDs.show();
      //delay(50);
      //delay(4000);
      t=0;
    }
//  delay(150);
  Serial.println(t);
}

}
int BH1750_Read(int address)
{
  int i = 0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while (Wire.available())
  {
    buff[i] = Wire.read();
    i++;
  }
  Wire.endTransmission();
  return i;
}
void BH1750_Init(int address)
{
   Wire.beginTransmission(address);
   Wire.write(0x10);
   Wire.endTransmission(); 
}

สั่งไฟผ่านลักซ์

#include <Wire.h>               // Include I2C library
int BH1750address = 0x23;       // Set BH1750 address
byte buff[2];
int t = 0;
int y = 0;
void setup()
{
  Wire.begin();
  Serial.begin(115200);
  pinMode(D7,1);
  pinMode(D6,1);
  pinMode(D5,1);
}

void loop()
{
    uint16_t val = 0;
    BH1750_Init(BH1750address);             // Initial BH1750
    delay(200);
    BH1750_Read(BH1750address);    // Read and check data from BH1750
    val = ((buff[0] << 8) | buff[1])/1.2;
    Serial.print(val + String(" lux    "));  // Show light density unit
    Serial.println(buff[1]/1.2);
    delay(150);
    if(buff[1]/1.2 < 50)
    {
      delay(300);
      t++;
      while(buff[1]/1.2 < 50)
      {
        uint16_t val = 0;
        BH1750_Init(BH1750address);             // Initial BH1750
        delay(200);
        BH1750_Read(BH1750address);    // Read and check data from BH1750
        val = ((buff[0] << 8) | buff[1])/1.2;
      }
    }
    Serial.println(t);
    if(t==1)
    {
      digitalWrite(D7,1);
      digitalWrite(D6,0);
      digitalWrite(D5,0);
    }
    if(t==3)
    {
      digitalWrite(D7,0);
      digitalWrite(D6,1);
      digitalWrite(D5,0);
    }
     if(t==6)
    {
      digitalWrite(D7,0);
      digitalWrite(D6,0);
      digitalWrite(D5,1);
    }
     if(t==10)
    {
      digitalWrite(D7,0);
      digitalWrite(D6,0);
      digitalWrite(D5,0);
      t=0;
    }
}
int BH1750_Read(int address)              // BH1750 read data function
{
  int i = 0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while (Wire.available())
  {
    buff[i] = Wire.read();                // Read one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}
void BH1750_Init(int address)             // BH1750 initial function
{
  Wire.beginTransmission(address);
  Wire.write(0x10);                       // Start operation
  Wire.endTransmission();
}

สวิตกดเอสแอล จำลอง2

#include <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
//int led = 10;
int button1 = 5;
int button2 = 4;
int temp1 = 0;
int temp2 = 0;
int pins[]={D5,D6,D7};
//int pins2[]={D7,D5,D4};
int i;
//int o;
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);
void setup(){
  for(i=0;i<3;i++){
    pinMode(pins[i],OUTPUT);
//    pinMode(pins2[o],OUTPUT);
    //pinMode(led,OUTPUT);
    pinMode(button1,INPUT);
    pinMode(button2,INPUT);
    SLEDs.begin(); // This initializes the NeoPixel library.
    SLEDs.show(); // Initialize all SLEDs to 'off'
    Serial.begin(115200);
}
}
void loop(){
for(i=0;i<3;i++){
    digitalWrite(pins[i],HIGH);
    delay(300);
}
for(i=0;i<4;i++){
     digitalWrite(pins[i],LOW);
     delay(500);
  }

  temp1 = digitalRead(button1);
  temp2 = digitalRead(button2);

  if(temp1 == 0)
 {
//  digitalWrite(led,HIGH);
   SLEDs.setPixelColor(0, SLEDs.Color(200, 0, 0));
   SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
    Serial.println("LED1 TURNNED ON");
    delay(1000);
    SLEDs.show();
  }
  else if(temp2 == 0){
  SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 0));
    SLEDs.setPixelColor(1, SLEDs.Color(0, 200, 0));
    //digitalWrite(led,LOW);
    Serial.println("LED2 TURNED ON");
    delay(1000);
    SLEDs.show();
  }
  else if(temp1 == 1){
  SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 200));
    SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 200));
    //digitalWrite(led,LOW);
    Serial.println("LED1 TURNED OFF");
    delay(1000);
    SLEDs.show();
   
  }
  else if(temp2 == 1){
  SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 200));
    SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 200));
//    digitalWrite(led,LOW);
    Serial.println("LED2 TURNED OFF");
    delay(1000);
    SLEDs.show();
  }
}

สวิตกด เอสแอล จำลอง1

#include <Adafruit_NeoPixel.h>
#define NUMSLEDs  2
#define PINSLEDs  10
//int led = 10;
int button1 = 5;
int button2 = 4;
int temp1 = 0;
int temp2 = 0;
int pins[]={D5,D6,D7};
//int pins2[]={D7,D5,D4};
int i;
//int o;
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);
void setup(){
  for(i=0;i<3;i++){
    pinMode(pins[i],OUTPUT);
//    pinMode(pins2[o],OUTPUT);
    //pinMode(led,OUTPUT);
    pinMode(button1,INPUT);
    pinMode(button2,INPUT);
    SLEDs.begin(); // This initializes the NeoPixel library.
    SLEDs.show(); // Initialize all SLEDs to 'off'
    Serial.begin(115200);
}
}
void loop(){
for(i=0;i<3;i++){
    digitalWrite(pins[i],HIGH);
    delay(300);
}
for(i=0;i<4;i++){
     digitalWrite(pins[i],LOW);
     delay(200);
  }

  temp1 = digitalRead(button1);
  temp2 = digitalRead(button2);

  if(temp1 == 0)
 {
//  digitalWrite(led,HIGH);
   SLEDs.setPixelColor(0, SLEDs.Color(200, 0, 0));
   SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
    Serial.println("LED1 TURNNED ON");
    delay(1000);
    SLEDs.show();
  }
  else if(temp2 == 0){
  SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 0));
    SLEDs.setPixelColor(1, SLEDs.Color(0, 200, 0));
    //digitalWrite(led,LOW);
    Serial.println("LED2 TURNED ON");
    delay(1000);
    SLEDs.show();
  }
  else if(temp1 == 1){
  SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 200));
    SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 200));
    //digitalWrite(led,LOW);
    Serial.println("LED1 TURNED OFF");
    delay(1000);
    SLEDs.show();
   
  }
  else if(temp2 == 1){
  SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 200));
    SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 200));
//    digitalWrite(led,LOW);
    Serial.println("LED2 TURNED OFF");
    delay(1000);
    SLEDs.show();
  }
}

สไลด์เดอร์

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

#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

#define APPID "killinter"
#define KEY "Xf6KHcTL2PPzUDg"
#define SECRET "orsDQPrSMXDteAbzseXr5scCE"

#define ALIAS "lediq"


WiFiClient client;

//#define SPEEDPIN D0 //ขาควบคุมความเร็วมอเตอร์
#define LED1PIN D1 //speed no. 1
#define LED2PIN D2 //speed no. 2
#define LED3PIN D3 //speed no. 3
#define LEDONLINEPIN D8 //สถานะเชื่อมต่อ netpie
#define BUTTONPIN D4 //ปุ่มกดเปลี่ยนสถานะ

int state = 0; //สถานะของการทำงานปัจจุบัน
int stateOld = 0; //สถานะของการทำงานก่อนหน้า
long timeout = 1000; //กำหนด timeout ของการกดปุ่ม
long last_change_time = 0; //เก็บเวลาสำหรับเช็ค timeout
int MoterSpeed[] = { 0, 511, 767, 1023 }; //ค่าสัญญาณ analog 0-1023
int lock = 1; //lock คำสั่ง เมื่อมีการอัพเดทสถานะการทำงานของอุปกรณ์ เพื่อสั่งงานอุปกรณ์

MicroGear microgear(client);

void sendSpeed() {
if (state!=stateOld) {
if (state == 1) {
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
digitalWrite(LED3PIN, LOW);
Serial.println("SP1");
} else if (state == 2) {
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
digitalWrite(LED3PIN, LOW);
Serial.println("SP2");
} else if (state == 3) {
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, LOW);
digitalWrite(LED3PIN, HIGH);
Serial.println("SP3");
} else if (state == 0) {
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, LOW);
digitalWrite(LED3PIN, LOW);
Serial.println("SP0");
}

if(state>=0 && state<=3){
stateOld=state;
//analogWrite(SPEEDPIN, MoterSpeed[state]); //กำหนดแรงขับมอเตอร์
microgear.publish("/lediq/state",state);
}
}
// lock=0; //unlock
}

void onButtonPressed() {
int difference = millis()-last_change_time;
if(difference > timeout || last_change_time == 0){
state++;
last_change_time = millis();
}
if(state>=4) state = 0;
lock=1; //lock
}

void onMsghandler(char topic, uint8_t msg, unsigned int msglen) {
char *m = (char *)msg;
m[msglen] = '\0';
int sp = atoi(m);

Serial.print("SPEED : ");
if(sp>=0 && sp<=3){
state = sp;
lock = 1;
Serial.println(m);
}
}

void onConnected(char attribute, uint8_t msg, unsigned int msglen) {
Serial.println("Connected to NETPIE...");
digitalWrite(LEDONLINEPIN, HIGH); //เปลี่ยนสถานะ led เป็นออนไลน์
}

void setup() {

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

//กำหนดโหมดของ pin ต่างๆ
pinMode(BUTTONPIN, INPUT_PULLUP);
// pinMode(SPEEDPIN, OUTPUT);
pinMode(LED1PIN, OUTPUT);
pinMode(LED2PIN, OUTPUT);
pinMode(LED3PIN, OUTPUT);
pinMode(LEDONLINEPIN, OUTPUT);

attachInterrupt( digitalPinToInterrupt(BUTTONPIN), onButtonPressed, RISING ); //ฟังก์ชั่นควบคุมการทำงานมอเตอร์

WiFiManager wifiManager;
wifiManager.setTimeout(180);

//wifiManager.resetSettings();

if (!wifiManager.autoConnect("PieFan")) {
Serial.println("Failed to connect and hit timeout");
delay(3000);
ESP.reset();
delay(5000);
}

microgear.on(MESSAGE, onMsghandler);
microgear.on(CONNECTED, onConnected);

microgear.init(KEY, SECRET, ALIAS);
microgear.connect(APPID); //เชื่อมต่อ netpie
}

void loop() {
if (microgear.connected()) {
microgear.loop();
if(lock) sendSpeed(); //กำหนดความเร็วมอเตอร์ และตอบกลับสถานะของอุปกรณ์ผ่าน netpie
}
else {
Serial.println("connection lost, reconnect...");
microgear.connect(APPID);
if(digitalRead(LEDONLINEPIN)==HIGH)
digitalWrite(LEDONLINEPIN, LOW); //เปลี่ยนสถานะ led เป็นออฟไลน์
}
}