const int ser_in = 19; const int CLK = 18; const int Tswitch = 1; const int Pswitch = 0; const int dir[5] = {13, 14, 15, 16, 17}; const int pos[11] = {2,3,4,5,6,7,8,9,10,11,12}; int p=0; //the number of dir, order from right down to right up is 0,1,2,3,4. int moment=0; // although switch is pushed long time, to move only one unit. int avil=1; int countPswitch=0; void setup() { pinMode(ser_in, OUTPUT); digitalWrite(ser_in, HIGH); pinMode(CLK, OUTPUT); for(int i=0; i<5; i++) pinMode(dir[i], OUTPUT); for(int i=0; i<11; i++) pinMode(pos[i], OUTPUT); pinMode(Tswitch, INPUT); pinMode(Pswitch, INPUT); //initialize setting display display_position_setting(p); display_direction_setting(p); } void loop() { //Pwitch if(digitalRead(Pswitch)==HIGH) {avil=1;} //if Pswitch isn't pushed, initialize avail for next Pswitch push. if(digitalRead(Pswitch)==LOW && avil == 1) //because if P Switch is pushed second the series, that is same with Tswitch pushed. { countPswitch++; avil=0; //only for the moment that touch the Pswitch. } //seting the OUTPUT pins moment=0;//to make possible to move right, 'check' have to be initialized. while( digitalRead(Tswitch)==LOW || (digitalRead(Pswitch)&&countPswitch==2) ) { countPswitch=0; if(moment==0) // the moment that the switch is pushed. { moment=1;//if in the state that is pushed long time, move only one unit p++; if(p == 21)//over number, initialize p=1; display_position_setting(p); display_direction_setting(p); } if(p<11) five_pulse(CLK); // if in the state that is pushed long time, the LED shift have to continue. else one_left_pulse(p); } if(p<11) five_pulse(CLK); // if in the state that is pushed long time, the LED shift have to continue. else one_left_pulse(p); } void five_pulse(int CLK) { digitalWrite(ser_in, HIGH); for(int i=0; i<5; i++) { digitalWrite(CLK, HIGH); delay(15); digitalWrite(CLK, LOW); delay(15); } } void display_direction_setting(int p) //setting 74164 chip. { int d = ((p+1)/2)-1; //the variation for direction pin if(d==-1) d=0; if(d>4)//clock direction d=9-d; for(int i=0; i<5; i++) digitalWrite(dir[i], HIGH);//deactivate digitalWrite(dir[d], LOW); // the state of LOW is active on dir LED. } void display_position_setting(int num) //setting connecting wire. { if(num>10) num = 20-num; for(int i=0; i<11; i++) digitalWrite(pos[i], LOW);//deacitivate MultiLED digitalWrite(pos[num], HIGH); //activate } void one_left_pulse(int p) //difficult control 74164 for diffrent direction. { int d = ((p+1)/2)-1; //the variation for direction pin, d is -1~4, 5~9. if(d==-1) d=0; if(d>4) d=9-d; for(int i = 0; i<16; i++) { digitalWrite(dir[d], HIGH); digitalWrite(ser_in, LOW); clock(1); digitalWrite(ser_in, HIGH); clock(i); digitalWrite(ser_in, LOW); clock(15-i); digitalWrite(dir[d], LOW); delay(25); } } void clock(int count)//fast clock { for(int i=0; i<count; i++) { digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); } } |