#if defined ( ARDUINO )
#include <Arduino.h>
#endif
// Include this to enable the M5 global instance.
#include <M5Unified.h>
#include <FastLED_NeoPixel.h>
uint8_t brightness = 50;
// Which pin on the Arduino is connected to the LEDs?
#define DATA_PIN 32
// How many LEDs are attached to the Arduino?
#define NUM_LEDS 66
FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB> strip;      // <- FastLED NeoPixel version
int16_t r = 0;
int16_t g = 0;
int16_t b = 0;
const double PWM_Hz = 10000;   // PWM周波数
const uint8_t PWM_level = 8; // PWM分解能 16bit(1~256)
const uint8_t PWM_CH = 1;  // チャンネル
const uint8_t PWM_PIN = 26;
uint8_t duty = 30;
#define PIR_PIN 36  // PIRセンサ用
int16_t timer = 1000000;
int16_t timerLimit = 15 * 5;
uint8_t mode = 1;
void setup() {
  auto cfg = M5.config();
  // begin M5Unified.
  M5.begin(cfg);
  M5.Power.setBatteryCharge(false); // バッテリーを使用しない。
  Serial.begin(115200);
  pinMode(26, OUTPUT); 
  // チャンネルと周波数の分解能を設定
  ledcSetup(PWM_CH, PWM_Hz, PWM_level);
  // モータのピンとチャンネルの設定
  ledcAttachPin(PWM_PIN, PWM_CH);
  // デューティー比でPWM制御
  ledcWrite(PWM_CH,(int)(duty * 256 / 100));
  // PIRセンサ関連
  pinMode(PIR_PIN, INPUT);
  // Get the number of available displays
  M5.Display.setRotation(1);
  M5.Display.setTextSize(1);
  M5.Display.setBrightness(90);
  /*
  int display_count = M5.getDisplayCount();
  for (int i = 0; i < display_count; ++i) {
  // All displays are available in M5.Displays.
  // ※ Note that the order of which displays are numbered is the order in which they are detected, so the order may change.
    int textsize = M5.Displays(i).height() / 60;
    if (textsize == 0) { textsize = 1; }
    M5.Displays(i).setTextSize(textsize);
    M5.Displays(i).printf("No.%d\n", i);
  }
  */
	strip.begin();  // initialize strip (required!)
	strip.setBrightness(brightness);
  strip.fill(strip.Color(255,255,255));
  strip.show();
  Serial.println("started");
}
void loop() {
  M5.update();
  if (M5.BtnA.wasClicked())
  {
    mode = mode == 0 ? 1 : 0;
    if (mode == 1)
    {
      strip.fill(strip.Color(255,255,255));
      strip.show();
    }
    else
    {
      r = g = b = 0;
    }
  }
  if(digitalRead(PIR_PIN) == 1)
  {
    timer = 0;
  }
  if (timer < timerLimit)
  {
    timer++;
    // デューティー比でPWM制御
    ledcWrite(PWM_CH,(int)(duty * 256 / 100));
  }
  else
  {
    ledcWrite(PWM_CH,0);
  }
  M5.Display.setCursor(10, 10);
  M5.Display.printf("duty:%d", duty);
  M5.Display.setCursor(10, 30);
  M5.Display.printf("briht:%d", brightness);
  M5.Display.setCursor(10, 50);
  M5.Display.printf("mode:%d", mode);
  M5.Display.setCursor(10, 65);
  M5.Display.printf("pir:%d", digitalRead(PIR_PIN));  
  M5.Display.setCursor(60, 65);
  M5.Display.printf("remain:%d", timerLimit - timer);  
  if (mode == 0)
  {
    r += 1;
    g += 2;
    b += 3;
  
    if (r > 110) r = 0;
    if (g > 110) g = 0;
    if (b > 110) b = 0;  
    
    uint8_t setR = 200 + abs(r - 55);
    uint8_t setG = 200 + abs(g - 55);
    uint8_t setB = 200 + abs(b - 55);
    strip.fill(strip.Color(setR, setG, setB));
    strip.show();
    M5.Display.setCursor(100, 10);
    M5.Display.printf("r:%d", setR);
    M5.Display.setCursor(100, 30);
    M5.Display.printf("g:%d", setG);
    M5.Display.setCursor(100, 50);
    M5.Display.printf("b:%d", setB);
  }
  M5.delay(200);
}
Comments