//RGBダイオードのRGB比をボタン操作で変えられるおもちゃ
//参考 https://omoroya.com/
#include <LiquidCrystal.h>
LiquidCrystal lcd( 2, 3, 4, 5, 6, 7 );
const int RED = 9; //RED用のアナログピン
const int GREEN = 10; //GREEN用のアナログピン
const int BLUE = 11; //BLUE用のアナログピン
const int tct1 = 8; //点灯ボタン用のピン
const int tct2 = 12; //色選択ボタン用のピン
const int tct3 = 13; //輝度アップボタン用のピン
int redValue = 0; //R,G,Bの輝度の初期設定
int greenValue = 0;
int blueValue = 0;
int oldInput1 = HIGH; //ボタンの連射を防ぐint関数(isPushed)用の変数
int oldInput2 = HIGH;
int oldInput3 = HIGH;
int colorNum = 1; //色選択用の変数
int colorNumMax = 3; //色数
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(tct1, INPUT_PULLUP); //タクトスイッチは押してない間はHIGH
pinMode(tct2, INPUT_PULLUP);
pinMode(tct3, INPUT_PULLUP);
lcd.begin( 16, 2 );
Serial.begin(9600);
}
void loop(){
if(digitalRead(tct1) == LOW){ //tct1スイッチが押されたら点灯
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
}else{ //tct1スイッチを離したら消灯
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
selectColor(); //色選択用の関数
blightUp(); //輝度アップ用の関数
draw(); //lcd表示用の関数
delay(50);
/*
Serial.print("R:");
Serial.print(redValue);
Serial.print(" ");
Serial.print("G:");
Serial.print(greenValue);
Serial.print(" ");
Serial.print("B");
Serial.println(blueValue);
*/
}
void selectColor(){ //tct2を押したときに色選択用の変数を変化させる関数
if(isPushed2() == true){
colorNum ++;
if(colorNum > colorNumMax){
colorNum = 1;
}
}
}
void blightUp(){//tct3スイッチを押すと選択中の色の輝度を5刻みで上げる。255を超えたら0に戻す
if(isPushed3() == true){
if(colorNum == 1){
redValue += 5;
if(redValue > 255){
redValue = 0;
}
}
if(colorNum == 2){
greenValue += 5;
if(greenValue > 255){
greenValue = 0;
}
}
if(colorNum == 3){
blueValue += 5;
if(blueValue > 255){
blueValue = 0;
}
}
}
}
void draw(){
lcd.clear();
lcd.setCursor(0, 0);
if(colorNum == 1){ //colorNumの値に応じて矢印(<)の位置を変える
lcd.print("R< G B");
}else if(colorNum == 2){
lcd.print("R G< B");
}else if(colorNum == 3){
lcd.print("R G B<");
}
lcd.setCursor(0, 1); //各色の輝度を表示
lcd.print(redValue);
lcd.setCursor(5, 1);
lcd.print(greenValue);
lcd.setCursor(10, 1);
lcd.print(blueValue);
}
int isPushed2(){ //押す直前のスイッチの状態を確認してHIGH→LOWのときのみtrueを返す(連射防止)
if(digitalRead(tct2) == LOW){
if(oldInput2 == HIGH){
oldInput2 = LOW;
return true;
}
}else{
oldInput2 = HIGH;
}
return false;
}
int isPushed3(){
if(digitalRead(tct3) == LOW){
if(oldInput3 == HIGH){
oldInput3 = LOW;
return true;
}
}else{
oldInput3 = HIGH;
}
return false;
}