#include <stdio.h>
//スイッチとLED用
byte LED_PIN_01=0;
byte LED_PIN_02=1;
byte LED_PIN_03=2;
byte LED_PIN_04=3;
byte Switch_PIN_01=4;
byte Switch_PIN_02=5;
byte Switch_PIN_03=6;
byte Switch_PIN_04=7;
byte SOL_PIN_01=10;
byte SOL_PIN_02=11;
byte SOL_PIN_03=12;
byte SOL_PIN_04=13;
int NowSwitch=-1;
byte SolenoidOn=0;
bool WaitFlag=false;
bool before_switch_01=false;
bool switch_01=false;
bool before_switch_02=false;
bool switch_02=false;
bool before_switch_03=false;
bool switch_03=false;
bool before_switch_04=false;
bool switch_04=false;
byte SwitchAry[4]={0};
//シフトレジスタ用ピン指定
byte RCK_PIN=14;
byte SCK_PIN=15;
byte SI_PIN=16;
//機嫌
int BeforeMood=0;
int Mood=0;
//時間カウント用
unsigned long OnStartTime;
unsigned long OffStartTime;
unsigned long MoodStartTime;
//関数宣言
void OpeningShiftRegister();//起動時のLEDバーの点灯パターン
void LightingShiftRegister(byte Pattern);//LEDバーを一本だけ点灯させる
void ShiftRegister();//Moodの値からLEDバーを点灯させる
void LEDInit();//LED初期化
void SwitchInit();//現在のスイッチの状態を変数に格納
void Switch();
void LED();
void Solenoid();
void AddMood(int num);
void setup()
{
//スイッチ・LEDピン設定
pinMode(LED_PIN_01,OUTPUT);
pinMode(LED_PIN_02,OUTPUT);
pinMode(LED_PIN_03,OUTPUT);
pinMode(LED_PIN_04,OUTPUT);
pinMode(Switch_PIN_01,INPUT);
pinMode(Switch_PIN_02,INPUT);
pinMode(Switch_PIN_03,INPUT);
pinMode(Switch_PIN_04,INPUT);
pinMode(SOL_PIN_01,OUTPUT);
pinMode(SOL_PIN_02,OUTPUT);
pinMode(SOL_PIN_03,OUTPUT);
pinMode(SOL_PIN_04,OUTPUT);
//シフトレジスタピン設定
pinMode(RCK_PIN,OUTPUT);
pinMode(SCK_PIN,OUTPUT);
pinMode(SI_PIN,OUTPUT);
//ソレノイドピン初期化
digitalWrite(SOL_PIN_01,LOW);
digitalWrite(SOL_PIN_02,LOW);
digitalWrite(SOL_PIN_03,LOW);
digitalWrite(SOL_PIN_04,LOW);
//機嫌変数初期化
Mood=0;
//スイッチ初期状態読み取り
SwitchInit();
//LED初期動作
LEDInit();
//LEDバー初期化
ShiftRegister();
//シフトレジスタ―の最初の動き
OpeningShiftRegister();
while(1)
{
int judge=0;
SwitchInit();
LEDInit();
for(int i=0;i<4;i++)
{
judge=judge+SwitchAry[i];
}
if(judge==0)
{
SolenoidOn=rand()%4;
delay(500);
break;
}
delay(50);
}
//時間初期化
OnStartTime=millis();
OffStartTime=millis();
MoodStartTime=millis();
}
void loop() //メインルーチン
{
BeforeMood=Mood;
//スイッチ処理
Switch();
//LED点灯
LED();
//5秒間隔でLEDバーの点灯を一つずつを減らしていく
if(millis()>=(MoodStartTime+5000))
{
if(Mood==0)
{
Mood=0;
}else AddMood(-1);
MoodStartTime=millis();
}else if(MoodStartTime>millis())//millis()オーバーフロー用
{
MoodStartTime=millis();
}
//LEDバー動作
if(BeforeMood!=Mood)
{
ShiftRegister();//シフトレジスタ処理は機嫌に変化があった時のみ
}
if(WaitFlag==true)
{
if(Mood<=5)
{
delay(1000);
WaitFlag=false;
}else if(Mood>5 && Mood<=8)
{
delay(500);
WaitFlag=false;
}
}
//ソレノイド動作
Solenoid();
delay(50);
}
/////////////////////////////////////////////
//自作関数定義ここから
/////////////////////////////////////////////
void Switch()
{
before_switch_01=switch_01;
before_switch_02=switch_02;
before_switch_03=switch_03;
before_switch_04=switch_04;
if(digitalRead(Switch_PIN_01)==HIGH)
{
SwitchAry[0]=1;
switch_01=true;
}else
{
SwitchAry[0]=0;
switch_01=false;
}
if(digitalRead(Switch_PIN_02)==HIGH)
{
SwitchAry[1]=1;
switch_02=true;
}else
{
SwitchAry[1]=0;
switch_02=false;
}
if(digitalRead(Switch_PIN_03)==HIGH)
{
SwitchAry[2]=1;
switch_03=true;
}else
{
SwitchAry[2]=0;
switch_03=false;
}
if(digitalRead(Switch_PIN_04)==HIGH)
{
SwitchAry[3]=1;
switch_04=true;
}else
{
SwitchAry[3]=0;
switch_04=false;
}
bool trigger=false;
if(switch_01==false && before_switch_01==true)
{
MoodStartTime=millis();
AddMood(1);
NowSwitch=0;
trigger=true;
}else if(switch_02==false && before_switch_02==true)
{
MoodStartTime=millis();
AddMood(1);
NowSwitch=1;
trigger=true;
}else if(switch_03==false && before_switch_03==true)
{
MoodStartTime=millis();
AddMood(1);
NowSwitch=2;
trigger=true;
}else if(switch_04==false && before_switch_04==true)
{
MoodStartTime=millis();
AddMood(1);
NowSwitch=3;
trigger=true;
}
int judge_count=0;
for(int i=0;i<4;i++)
{
judge_count=judge_count+SwitchAry[i];
}
if(judge_count<3 && trigger==true)
{
WaitFlag=true;
int buf=0;
while(1)
{
buf=rand()%4;
if(buf!=NowSwitch && SwitchAry[buf]==0)
{
SolenoidOn=buf;
break;
}
}
}else if(judge_count>=3)
{
SolenoidOn=-1;
NowSwitch=-1;
}
}
void LED()
{
if(digitalRead(Switch_PIN_01)==HIGH)
{
digitalWrite(LED_PIN_01,HIGH);
}else digitalWrite(LED_PIN_01,LOW);
if(digitalRead(Switch_PIN_02)==HIGH)
{
digitalWrite(LED_PIN_02,HIGH);
}else digitalWrite(LED_PIN_02,LOW);
if(digitalRead(Switch_PIN_03)==HIGH)
{
digitalWrite(LED_PIN_03,HIGH);
}else digitalWrite(LED_PIN_03,LOW);
if(digitalRead(Switch_PIN_04)==HIGH)
{
digitalWrite(LED_PIN_04,HIGH);
}else digitalWrite(LED_PIN_04,LOW);
}
void Solenoid()
{
switch(SolenoidOn)
{
case 0:
if(digitalRead(Switch_PIN_01)==LOW)
{
digitalWrite(SOL_PIN_01,HIGH);
}else digitalWrite(SOL_PIN_01,LOW);
break;
case 1:
if(digitalRead(Switch_PIN_02)==LOW)
{
digitalWrite(SOL_PIN_02,HIGH);
}else digitalWrite(SOL_PIN_02,LOW);
break;
case 2:
if(digitalRead(Switch_PIN_03)==LOW)
{
digitalWrite(SOL_PIN_03,HIGH);
}else digitalWrite(SOL_PIN_03,LOW);
break;
case 3:
if(digitalRead(Switch_PIN_04)==LOW)
{
digitalWrite(SOL_PIN_04,HIGH);
}else digitalWrite(SOL_PIN_04,LOW);
break;
default:
break;
}
if(digitalRead(SOL_PIN_01)==HIGH && digitalRead(Switch_PIN_01)==HIGH) digitalWrite(SOL_PIN_01,LOW);
if(digitalRead(SOL_PIN_02)==HIGH && digitalRead(Switch_PIN_02)==HIGH) digitalWrite(SOL_PIN_02,LOW);
if(digitalRead(SOL_PIN_03)==HIGH && digitalRead(Switch_PIN_03)==HIGH) digitalWrite(SOL_PIN_03,LOW);
if(digitalRead(SOL_PIN_04)==HIGH && digitalRead(Switch_PIN_04)==HIGH) digitalWrite(SOL_PIN_04,LOW);
}
void SwitchInit()
{
if(digitalRead(Switch_PIN_01)==HIGH)
{
SwitchAry[0]=1;
}else SwitchAry[0]=0;
if(digitalRead(Switch_PIN_02)==HIGH)
{
SwitchAry[1]=1;
}else SwitchAry[1]=0;
if(digitalRead(Switch_PIN_03)==HIGH)
{
SwitchAry[2]=1;
}else SwitchAry[2]=0;
if(digitalRead(Switch_PIN_04)==HIGH)
{
SwitchAry[3]=1;
}else SwitchAry[3]=0;
}
void LEDInit()
{
if(digitalRead(Switch_PIN_01)==HIGH)
{
digitalWrite(LED_PIN_01,HIGH);
}else digitalWrite(LED_PIN_01,LOW);
if(digitalRead(Switch_PIN_02)==HIGH)
{
digitalWrite(LED_PIN_02,HIGH);
}else digitalWrite(LED_PIN_02,LOW);
if(digitalRead(Switch_PIN_03)==HIGH)
{
digitalWrite(LED_PIN_03,HIGH);
}else digitalWrite(LED_PIN_03,LOW);
if(digitalRead(Switch_PIN_04)==HIGH)
{
digitalWrite(LED_PIN_04,HIGH);
}else digitalWrite(LED_PIN_04,LOW);
}
void OpeningShiftRegister()
{
for(int j=0;j<3;j++)
{
for(int i=9;i>=0;i--)
{
LightingShiftRegister(i);
if(i!=0) delay(50);
}
for(int i=0;i<10;i++)
{
LightingShiftRegister(i);
if(i!=9) delay(50);
}
}
Mood=10;
ShiftRegister();
delay(500);
Mood=0;
ShiftRegister();
}
void LightingShiftRegister(byte Pattern)
{
int LED_bit[10]={};
if(Pattern>9) Pattern=9;
LED_bit[9-Pattern]=1;
digitalWrite(SI_PIN,LOW);
digitalWrite(SCK_PIN,LOW);
digitalWrite(RCK_PIN,LOW);
for(int i=0;i<10;i++)
{
if(LED_bit[i]>0)
{
digitalWrite(SI_PIN,HIGH);
}
else
{
digitalWrite(SI_PIN,LOW);
}
//データ順次ビット送り
digitalWrite(SCK_PIN,LOW);
digitalWrite(SCK_PIN,HIGH);
}
//LED出力
digitalWrite(RCK_PIN,HIGH);
digitalWrite(RCK_PIN,LOW);
//初期化
digitalWrite(SI_PIN,LOW);
digitalWrite(SCK_PIN,LOW);
digitalWrite(RCK_PIN,LOW);
}
void ShiftRegister()
{
digitalWrite(SI_PIN,LOW);
digitalWrite(SCK_PIN,LOW);
digitalWrite(RCK_PIN,LOW);
unsigned char buf=Mood;
for(int i=0;i<10;i++)
{
if(buf>0)
{
digitalWrite(SI_PIN,HIGH);
buf=buf-1;
}
else
{
digitalWrite(SI_PIN,LOW);
}
//データ順次ビット送り
digitalWrite(SCK_PIN,LOW);
digitalWrite(SCK_PIN,HIGH);
}
//LED出力
digitalWrite(RCK_PIN,HIGH);
digitalWrite(RCK_PIN,LOW);
//初期化
digitalWrite(SI_PIN,LOW);
digitalWrite(SCK_PIN,LOW);
digitalWrite(RCK_PIN,LOW);
}
void AddMood(int num)
{
Mood=Mood+num;
if(Mood>10)
{
Mood=10;
}else if(Mood<0)
{
Mood=0;
}
}
|