这是追求极致还是死磕?
本帖最后由 Arduino_xyz 于 2019-6-2 09:35 编辑版本一
视频演示:https://www.bilibili.com/video/av52557415/
版本二
视频演示:https://www.bilibili.com/video/av54323283/
/*
2019 © Arduino_Maker
*/
#include <TimerOne.h>
byte m = 0;
byte Pvalue = 0;
boolean BreatheFlag = true;
byte ModeNumber = 0;
//byte oldModeNumber = 0;
boolean Touchflag = false;
unsigned int Time1 = 0;
unsigned int timex = 0;
const byte Ring={
{2,19,13},
{9,14,17},
{4,8,15,6,10,7},
{18,16,12},
{11,5,3}
};
void setup() {
Serial.begin(9600);
for(int i=0;i<5;i++){
for(int j=0;Ring!='\0';j++){
pinMode(Ring,OUTPUT);
}
}
Timer1.initialize( 100000 );
Timer1.attachInterrupt( Timer2Interrupt );
}
void Timer2Interrupt(void){
if((analogRead(A6) < 100)&& (Touchflag==false)){
Touchflag = true;
Time1 = millis();
ModeNumber = ModeNumber + 1;
if(ModeNumber == 2) m = 0;
if(ModeNumber == 7) ModeNumber = 0;
//Serial.println(ModeNumber);
}
elseif((analogRead(A6) > 100)&& (Touchflag==true)){
Touchflag = false;
timex = millis() - Time1;
if(timex > 3000)ModeNumber = 0;
}
}
void loop() {
switch(ModeNumber){
case 0: Mode0(); break;
case 1: Mode1(10); break;
case 2: Mode2(100); break;
case 3: Mode3(100); break;
case 4: Mode4(100); break;
case 5: Mode5(10); break;
case 6: Mode6(60); break;
default:break;
}
}
void Mode0(void)
{
Ring_Run(0,false);
Ring_Run(1,false);
Ring_Run(2,false);
Ring_Run(3,false);
Ring_Run(4,false);
}
void Mode1(int Mytime)
{
analogWrite(Ring,Pvalue);
analogWrite(Ring,Pvalue);
analogWrite(Ring,Pvalue);
if(BreatheFlag==true){
Pvalue = Pvalue + 1;
if(Pvalue == 255) BreatheFlag = false;
}
if(BreatheFlag==false){
Pvalue = Pvalue - 1;
if(Pvalue == 0) BreatheFlag = true;
}
delay(Mytime);
}
void Mode2(int Mytime)
{
switch(m){
case 0: Ring_Run(0,true); break;
case 1: delay(4*Mytime);break;
case 2: Ring_Run(1,true); break;
case 3: delay(3*Mytime);break;
case 4: Ring_Run(2,true); break;
case 5: delay(2*Mytime);break;
case 6: Ring_Run(3,true); break;
case 7: delay(Mytime); break;
case 8: Ring_Run(4,true); break;
case 9: delay(4*Mytime);break;
case 10:delay(4*Mytime);break;
case 11:Ring_Run(4,false);break;
case 12:delay(Mytime); break;
case 13:Ring_Run(3,false);break;
case 14:delay(2*Mytime);break;
case 15:Ring_Run(2,false);break;
case 16:delay(3*Mytime);break;
case 17:Ring_Run(1,false);break;
case 18:delay(4*Mytime);break;
case 19:Ring_Run(0,false);break;
case 20:delay(4*Mytime);break;
case 21:delay(4*Mytime);break;
default: break;
}
m = m + 1;
if(m == 22) m = 0;
}
void Mode3(int Mytime)
{
Ring_Run(0,true);
Ring_Run(1,true);
Ring_Run(2,true);
Ring_Run(3,true);
Ring_Run(4,true);
delay(Mytime);
Ring_Run(0,false);
Ring_Run(1,false);
Ring_Run(2,false);
Ring_Run(3,false);
Ring_Run(4,false);
delay(Mytime);
}
void Mode4(int Mytime)
{
Leaf_Run(2,false);Leaf_Run(0,true);delay(Mytime);
Leaf_Run(0,false);Leaf_Run(1,true);delay(Mytime);
Leaf_Run(1,false);Leaf_Run(2,true);delay(Mytime);
}
void Mode5(int Mytime)
{
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);
}
void Mode6(int Mytime)
{
Ring_Run(0,true);
Ring_Run(4,true);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
digitalWrite(Ring,LOW);digitalWrite(Ring,HIGH);delay(Mytime);
}
void Ring_Run(byte n,boolean k)
{
if(k==true)
switch(n){
case 0:
for(int i = 0;i < 3;i++)digitalWrite(Ring,HIGH);break;
case 1:
for(int i = 0;i < 3;i++)digitalWrite(Ring,HIGH);break;
case 2:
for(int i = 0;i < 6;i++)digitalWrite(Ring,HIGH);break;
case 3:
for(int i = 0;i < 3;i++)digitalWrite(Ring,HIGH);break;
case 4:
for(int i = 0;i < 3;i++)digitalWrite(Ring,HIGH);break;
default : break;
}
elseswitch(n){
case 0:
for(int i = 0;i < 3;i++)digitalWrite(Ring,LOW);break;
case 1:
for(int i = 0;i < 3;i++)digitalWrite(Ring,LOW);break;
case 2:
for(int i = 0;i < 6;i++)digitalWrite(Ring,LOW);break;
case 3:
for(int i = 0;i < 3;i++)digitalWrite(Ring,LOW);break;
case 4:
for(int i = 0;i < 3;i++)digitalWrite(Ring,LOW);break;
default : break;
}
}
void Leaf_Run(byte n,boolean k)
{
if(k==true)
switch(n){
case 0:
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
break;
case 1:
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
break;
case 2:
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
digitalWrite(Ring,HIGH);
break;
default: break;
}
else switch(n){
case 0:
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
break;
case 1:
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
break;
case 2:
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
digitalWrite(Ring,LOW);
break;
default: break;
}
}
加油加油
页:
[1]