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

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

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

ไม่มีความคิดเห็น:

แสดงความคิดเห็น