M5Stack Core2: 電子ろーそく

M5Stack Core2 を使って電子ろーそくを作成しました。お話会で使います。

```text
電源を入れると、オレンジになります。
真ん中のボタンを押すと、赤、青、黄、緑のどれかの色になります。
右のボタンを押すと、消えます。
左のボタンを押すとオレンジになります。
お話会の最後に色当てゲームをします。勝ち残りで最後に残った人に、消してもらっています。
```


![IMG_20221031_093707.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/60addad8-0b38-edab-d6ae-13388ab0e38f.jpeg)

## プログラム

```text:candle_feb07.ino
// ---------------------------------------------------------------
/*
	candle_feb07.ino

						Feb/07/2023
*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <Adafruit_NeoPixel.h>
#include <AXP192.h>

#define	PROGRAM	"candle_feb07.ino"
#define	VERSION	"2023-2-07 PM 20:29"

#define PIN 25 
#define NUMPIXELS 9 //LEDの数を指定
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int icount = 0;
String color_current;
AXP192 power;

String aStr_before = "";
// ---------------------------------------------------------------
void setup()
{
	pixels.begin();

	M5.begin();
	M5.Lcd.setTextSize(3);

	delay(1000);
	Serial.println("*** setup *** start ***");
	Serial.println(PROGRAM);
	Serial.println(VERSION);

	led_proc ("orange");
	color_current = "orange";
}

// ---------------------------------------------------------------
void loop()
{
	if ((icount % 500) == 0)
	{
	float batt = power.GetBatVoltage();
	Serial.println
  ("*** " + String(icount) + "\t" + color_current + "\t" + String(batt));
	}

	pixels.clear();

	String aStr = serial_receive_proc();

	if (2 < aStr.length())
		{
		led_proc (aStr);
		color_current = aStr;
		}

//	delay(100);
	delay(10);
	aStr = button_monitor();
	if (2 < aStr.length())
		{
		led_proc (aStr);
		color_current = aStr;
		}

	if (0 <= color_current.indexOf("orange"))
		{
		if ((icount % 10) == 0)
			{
			blink_proc(icount / 10);
			}
		}

	icount++;
}

// --------------------------------------------------------------
void blink_proc(int ixx)
{
//	Serial.println("*** blink_proc *** " + String(ixx));

	int ikey = ixx % 8;

	if (4 <= ikey)
		{
		ikey++;
		}

	for (int it=0; it< 9; it++)
		{
		pixels.setPixelColor(it, pixels.Color(255, 165, 0));
		}

	pixels.setPixelColor(ikey, pixels.Color(0xff, 0x9d, 0x30));

	pixels.show();
}

// --------------------------------------------------------------
String define_color_proc()
{
	String aStr = "";
	int time = random(1000);
	int ix = time % 4;
	Serial.printf("time = %d\tix = %d\r\n",time,ix);
	switch (ix)
		{
		case	1:
			aStr = "blue";
			break;

		case	2:
			aStr = "red";
			break;

		case	3:
			aStr = "green";
			break;

		default:
			aStr = "yellow";
			break;
		}

	return	aStr;
}

// --------------------------------------------------------------
String button_monitor()
{
	String aStr = "";

	M5.update();
	if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
		M5.Lcd.print('A');
		aStr = "orange";
	} else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
		M5.Lcd.print('B');


	aStr = define_color_proc();

	while (color_current == aStr)
		{
	//	M5.Lcd.print("*** warning ***");
		Serial.println("*** warning *** " + aStr);
		delay(10);
		aStr = define_color_proc();
		}

	} else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
		M5.Lcd.print('C');
		aStr = "off";
	}

	return aStr;
}

// --------------------------------------------------------------
```

```text:led_proc.ino
// ---------------------------------------------------------------
/*
	led_proc.ino

						Jan/31/2023
*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <Adafruit_NeoPixel.h>

// --------------------------------------------------------------
void led_proc (String aStr)
{
	M5.Lcd.setCursor(0, 0);

	for (int it=0; it<9; it++)
		{
		pixels.setPixelColor(it, pixels.Color(0, 0, 0));    
		}

	if (0 <= aStr.indexOf("red"))
		{
		M5.Lcd.println("LED Red   ");
		M5.Lcd.printf("check: %s     \n",aStr);
		for (int it=0; it< 9; it++)
			{
			pixels.setPixelColor(it, pixels.Color(50, 0, 0));
			}

		delay(500);
		M5.Lcd.fillScreen(RED);
		}
	else if (0 <= aStr.indexOf("blue"))
		{
		M5.Lcd.println("LED Blue  ");
		M5.Lcd.printf("check: %s     \n",aStr);
		for (int it=0; it< 9; it++)
			{
			pixels.setPixelColor(it, pixels.Color(0, 0,150));
			}

		delay(500);
		M5.Lcd.fillScreen(BLUE);
		}	
	else if (0 <= aStr.indexOf("green"))
		{
		M5.Lcd.println("LED Green");
		M5.Lcd.printf("check: %s     \n",aStr);
		for (int it=0; it< 9; it++)
			{
			pixels.setPixelColor(it, pixels.Color(0, 150, 0));
			}

		delay(500);
		M5.Lcd.fillScreen(GREEN);
		}
	else if (0 <= aStr.indexOf("yellow"))
		{
		M5.Lcd.println("LED Yellow");
		M5.Lcd.printf("check: %s     \n",aStr);
		for (int it=0; it< 9; it++)
			{
			pixels.setPixelColor(it, pixels.Color(150,150,0));
			}

		delay(500);
		M5.Lcd.fillScreen(YELLOW);
		}
	else if (0 <= aStr.indexOf("orange"))
		{
		M5.Lcd.println("LED Orange");
		M5.Lcd.printf("check: %s     \n",aStr);

		for (int it=0; it< 9; it++)
			{
			pixels.setPixelColor(it, pixels.Color(255, 165, 0));
			}

		delay(500);
		M5.Lcd.fillScreen(ORANGE);
		}
	else if (0 <= aStr.indexOf("off"))
		{
		M5.Lcd.println("LED Off");
		M5.Lcd.printf("check: %s     \n",aStr);

		for (int it=0; it< 9; it++)
			{
			pixels.setPixelColor(it, pixels.Color(0, 0, 0));
			}

		delay(500);
		M5.Lcd.fillScreen(BLACK);
		}

	pixels.show();
}

// ---------------------------------------------------------------
```

```text:serial_receive.ino
// ---------------------------------------------------------------
/*
	serial_receive.ino

						Oct/31/2022
*/
// ---------------------------------------------------------------
#include <M5Core2.h>

// ---------------------------------------------------------------
String serial_receive_proc()
{
	String aStr = "";  
	if (0 <  Serial.available() )
	{
  
	// 終了文字まで取得
	aStr = Serial.readStringUntil('\n');
	Serial.print("Input:");
	Serial.println(aStr);

	M5.Lcd.setCursor(0, 150);
	M5.Lcd.println("Input:");
	M5.Lcd.setCursor(0, 200);
	M5.Lcd.print(aStr);
	M5.Lcd.println("                         ");
	}

	return aStr;
}

// ---------------------------------------------------------------
```

Arduino IDE 2.3.4

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/4d8ebfd3-fb92-4139-b4f7-73994c3e628f.png)