2019-5-15版本一:
https://www.bilibili.com/video/av52557415/
2019-6-2版本二:
https://www.bilibili.com/video/av54323283/
废话不多说,源码如下
[code]/*
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[5][6]={
{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[i][j]!='\0';j++){
pinMode(Ring[i][j],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);
}
else if((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[4][0],Pvalue);
analogWrite(Ring[4][1],Pvalue);
analogWrite(Ring[4][2],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[0][2],LOW);digitalWrite(Ring[0][0],HIGH);delay(Mytime);
digitalWrite(Ring[0][0],LOW);digitalWrite(Ring[0][1],HIGH);delay(Mytime);
digitalWrite(Ring[0][1],LOW);digitalWrite(Ring[0][2],HIGH);delay(Mytime);
digitalWrite(Ring[0][2],LOW);
digitalWrite(Ring[1][1],LOW);digitalWrite(Ring[1][0],HIGH);delay(Mytime);
digitalWrite(Ring[1][0],LOW);digitalWrite(Ring[1][2],HIGH);delay(Mytime);
digitalWrite(Ring[1][2],LOW);digitalWrite(Ring[1][1],HIGH);delay(Mytime);
digitalWrite(Ring[1][1],LOW);
digitalWrite(Ring[2][4],LOW);digitalWrite(Ring[2][5],LOW);digitalWrite(Ring[2][2],HIGH);digitalWrite(Ring[2][3],HIGH);delay(Mytime);
digitalWrite(Ring[2][2],LOW);digitalWrite(Ring[2][3],LOW);digitalWrite(Ring[2][0],HIGH);digitalWrite(Ring[2][1],HIGH);delay(Mytime);
digitalWrite(Ring[2][0],LOW);digitalWrite(Ring[2][1],LOW);digitalWrite(Ring[2][4],HIGH);digitalWrite(Ring[2][5],HIGH);delay(Mytime);
digitalWrite(Ring[2][4],LOW);digitalWrite(Ring[2][5],LOW);
digitalWrite(Ring[3][2],LOW);digitalWrite(Ring[3][0],HIGH);delay(Mytime);
digitalWrite(Ring[3][0],LOW);digitalWrite(Ring[3][1],HIGH);delay(Mytime);
digitalWrite(Ring[3][1],LOW);digitalWrite(Ring[3][2],HIGH);delay(Mytime);
digitalWrite(Ring[3][2],LOW);
digitalWrite(Ring[4][2],LOW);digitalWrite(Ring[4][0],HIGH);delay(Mytime);
digitalWrite(Ring[4][0],LOW);digitalWrite(Ring[4][1],HIGH);delay(Mytime);
digitalWrite(Ring[4][1],LOW);digitalWrite(Ring[4][2],HIGH);delay(Mytime);
digitalWrite(Ring[4][2],LOW);
}
void Mode6(int Mytime)
{
Ring_Run(0,true);
Ring_Run(4,true);
digitalWrite(Ring[2][4],LOW);digitalWrite(Ring[2][3],HIGH);delay(Mytime);
digitalWrite(Ring[2][3],LOW);digitalWrite(Ring[3][1],HIGH);delay(Mytime);
digitalWrite(Ring[3][1],LOW);digitalWrite(Ring[2][2],HIGH);delay(Mytime);
digitalWrite(Ring[2][2],LOW);digitalWrite(Ring[2][1],HIGH);delay(Mytime);
digitalWrite(Ring[2][1],LOW);digitalWrite(Ring[3][0],HIGH);delay(Mytime);
digitalWrite(Ring[3][0],LOW);digitalWrite(Ring[2][0],HIGH);delay(Mytime);
digitalWrite(Ring[2][0],LOW);digitalWrite(Ring[2][5],HIGH);delay(Mytime);
digitalWrite(Ring[2][5],LOW);digitalWrite(Ring[3][2],HIGH);delay(Mytime);
digitalWrite(Ring[3][2],LOW);digitalWrite(Ring[2][4],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[0][i],HIGH); break;
case 1:
for(int i = 0;i < 3;i++) digitalWrite(Ring[1][i],HIGH); break;
case 2:
for(int i = 0;i < 6;i++) digitalWrite(Ring[2][i],HIGH); break;
case 3:
for(int i = 0;i < 3;i++) digitalWrite(Ring[3][i],HIGH); break;
case 4:
for(int i = 0;i < 3;i++) digitalWrite(Ring[4][i],HIGH); break;
default : break;
}
else switch(n){
case 0:
for(int i = 0;i < 3;i++) digitalWrite(Ring[0][i],LOW); break;
case 1:
for(int i = 0;i < 3;i++) digitalWrite(Ring[1][i],LOW); break;
case 2:
for(int i = 0;i < 6;i++) digitalWrite(Ring[2][i],LOW); break;
case 3:
for(int i = 0;i < 3;i++) digitalWrite(Ring[3][i],LOW); break;
case 4:
for(int i = 0;i < 3;i++) digitalWrite(Ring[4][i],LOW); break;
default : break;
}
}
void Leaf_Run(byte n,boolean k)
{
if(k==true)
switch(n){
case 0:
digitalWrite(Ring[1][0],HIGH);
digitalWrite(Ring[2][0],HIGH);
digitalWrite(Ring[2][1],HIGH);
digitalWrite(Ring[3][0],HIGH);
digitalWrite(Ring[4][0],HIGH);
break;
case 1:
digitalWrite(Ring[1][2],HIGH);
digitalWrite(Ring[2][5],HIGH);
digitalWrite(Ring[2][4],HIGH);
digitalWrite(Ring[3][2],HIGH);
digitalWrite(Ring[4][2],HIGH);
break;
case 2:
digitalWrite(Ring[1][1],HIGH);
digitalWrite(Ring[2][2],HIGH);
digitalWrite(Ring[2][3],HIGH);
digitalWrite(Ring[3][1],HIGH);
digitalWrite(Ring[4][1],HIGH);
break;
default: break;
}
else switch(n){
case 0:
digitalWrite(Ring[1][0],LOW);
digitalWrite(Ring[2][0],LOW);
digitalWrite(Ring[2][1],LOW);
digitalWrite(Ring[3][0],LOW);
digitalWrite(Ring[4][0],LOW);
break;
case 1:
digitalWrite(Ring[1][2],LOW);
digitalWrite(Ring[2][5],LOW);
digitalWrite(Ring[2][4],LOW);
digitalWrite(Ring[3][2],LOW);
digitalWrite(Ring[4][2],LOW);
break;
case 2:
digitalWrite(Ring[1][1],LOW);
digitalWrite(Ring[2][2],LOW);
digitalWrite(Ring[2][3],LOW);
digitalWrite(Ring[3][1],LOW);
digitalWrite(Ring[4][1],LOW);
break;
default: break;
}
}[/code]
网友评论