//////////////가속도로 정회전
int in_A = 8;
int Speed = 10;
int i, j, cycle;
int step_table[4][4] = {
{1, 0, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}
};
void setup()
{
for (i = 0; i < 4; i++)
pinMode(in_A + i, OUTPUT);
}
void clockwise(int Speed)
{
for (j = 0; j < 4; j++)
{
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[j][i]);
delay(Speed);
}
}
void loop()
{
for (cycle = 0; cycle < 50; cycle++) //1바퀴
{
clockwise(60 - cycle); //가속
}
for (cycle = 0; cycle < 150; cycle++) //3바퀴
{
clockwise(10); //100Hz
}
for (cycle = 0; cycle < 50; cycle++) //1바퀴
{
clockwise(11 + cycle); //감속
}
delay(1000);
}
///////////////포텐시오미터값으로 속도제어
int in_A = 8;
int Speed = 10;
int i, j, cycle;
int step_table[4][4] = {
{1, 0, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}
};
void setup()
{
for (i = 0; i < 4; i++)
pinMode(in_A + i, OUTPUT);
}
void CW(int Speed)
{
for (j = 0; j < 4; j++)
{
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[j][i]);
delay(Speed);
}
}
void CCW(int Speed)
{
for (j = 0; j < 4; j++)
{
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[3-j][i]);
delay(Speed);
}
}
void loop()
{
for (cycle = 0; cycle < 150; cycle++) //3정회전
{
int Speed = analogRead(A0);
Speed = map(Speed, 0, 1023, 10, 100);
CW(Speed);
}
for (cycle = 0; cycle < 100; cycle++) //2역회전
{
int Speed = analogRead(A0);
Speed = map(Speed, 0, 1023, 10, 100);
CCW(Speed);
}
}
//////////////////HALF-STEP방식-1
int in_A = 8;
int Speed = 100;
int i;
int step_table[8][4] = {
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 0},
};
void setup()
{
for (i = 0; i < 4; i++)
pinMode(in_A + i, OUTPUT);
}
void loop()
{
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[0][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[1][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[2][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[3][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[4][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[5][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[6][i]);
delay(Speed);
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[7][i]);
delay(Speed);
}
//////////////////HALF-STEP방식-2
int in_A = 8;
int Speed = 100;
int i, j;
int step_table[8][4] = {
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 0},
};
void setup()
{
for (i = 0; i < 4; i++)
pinMode(in_A + i, OUTPUT);
}
void loop()
{
for (j = 0; j < 8; j++)
{
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[j][i]);
delay(Speed);
}
}
//////////////////HALF-STEP방식-3
int in_A = 8;
int Speed = 10;
int i, j, cycle;
int step_table[8][4] = {
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 0},
};
void setup()
{
for (i = 0; i < 4; i++)
pinMode(in_A + i, OUTPUT);
}
void loop()
{
for (cycle = 0; cycle < 50; cycle++)
{
for (j = 0; j < 8; j++)
{
for (i = 0; i < 4; i++)
digitalWrite(in_A + i, step_table[j][i]);
delay(Speed);
}
}
delay(2000);
}
/////////SW1회ON-CW6회전 및 FND카운트, SW2회ON-CCW4회전 및 FND카운트
int A = 2;
int preSW = 0;
int STATUS = 0;
int step_table[4][4] = {
{1, 0, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}
};
int pin = 6;
int table[16][7] = {
{1,1,1,1,1,1,0}, //0
{0,1,1,0,0,0,0}, //1
{1,1,0,1,1,0,1}, //2
{1,1,1,1,0,0,1}, //3
{0,1,1,0,0,1,1}, //4
{1,0,1,1,0,1,1}, //5
{1,0,1,1,1,1,1}, //6
{1,1,1,0,0,1,0}, //7
{1,1,1,1,1,1,1}, //8
{1,1,1,0,0,1,1}, //9
{1,1,1,0,1,1,1}, //A
{0,0,1,1,1,1,1}, //B
{0,0,0,1,1,0,1}, //C
{0,1,1,1,1,0,1}, //D
{1,0,0,1,1,1,1}, //E
{1,0,0,0,1,1,1} //F
};
void setup()
{
int i;
for (i = 0; i < 4; i++)
pinMode(A + i, OUTPUT);
pinMode(13, INPUT); //SW
pinMode(A0, OUTPUT); //LED1
pinMode(A1, OUTPUT); //LED2
for(i=0; i<7; i++)
pinMode(pin+i, OUTPUT);
}
void CW(int myDelay)
{
int i, j, k;
for (i = 0; i < 50; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
digitalWrite(A+k, step_table[j][k]);
delay(myDelay);
}
}
}
void CCW(int myDelay1)
{
int i, j, k;
for (i = 0; i < 50; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
digitalWrite(A+k, step_table[3-j][k]);
delay(myDelay1);
}
}
}
void fnd(int num)
{
int i,j;
j = num;
for(i=0; i<7; i++)
digitalWrite(pin+i, table[j][i]);
}
void loop()
{
int SW = digitalRead(13);
int i,j;
if (SW != preSW)
{
if (SW == 1)
{
fnd(0);
STATUS++;
STATUS = STATUS % 2;
if (STATUS == 1)
{
digitalWrite(A1, 0);
digitalWrite(A0, 1);
for(i=0; i<6; i++){
CW(10);
fnd(i+1);
}
}
else if (STATUS == 0)
{
digitalWrite(A0, 0);
digitalWrite(A1, 1);
for(j=0; j<4; j++){
CCW(20);
fnd(j+1);
}
}
}
preSW = SW;
}
}