2017년 6월 29일 목요일

Arduino이용 Step-Motor 제어


//////////////가속도로 정회전


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;
  }
}

2017년 6월 28일 수요일

좌표기억운전


1. 조그운전으로 기억한 위치데이터를 선입선출로 운전하기
- FIWRP의 경우 워드데이터이고, 좌표는 더블워드이므로 주의
- 아래예제는 데이터 한번출력후 소멸
- #D나 GMOV등으로 데이터를 보관하는 방법을 강구해야함





















2. 조그운전으로 X축 현재위치를 바꾸고, X축 기동운전으로 동작
- #D는 D디바이스를 다루므로 직접적으로 K영역의 디바이스 주소를 지정할 수 없다
- 축파라미터와 비교후 PLC읽기로 읽어오면 파라미터 정보가 변경된 것을 확인가능
- X축 스텝 1번주소 K530에서 +10씩 증가(2번주소: K540...)






2017년 6월 22일 목요일

배열이용 CW,CCW함수로 제어


int A = 8;
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}
};

void setup()
{
  int i;
  for (i = 0; i < 4; i++)
    pinMode(A + i, OUTPUT);
  pinMode(6, INPUT);  //SW1
  pinMode(7, INPUT);  //SW2
  pinMode(12, OUTPUT);  //LED1
  pinMode(13, OUTPUT);  //LED2
}

void CW(int rota, int myDelay)
{
  int i, j, k;

  for (i = 0; i < 50 * rota; 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 rota1, int myDelay1)
{
  int i, j, k;

  for (i = 0; i < 50 * rota1; i++)
  {
    for (j = 0; j < 4; j++)
    {
      for (k = 0; k < 4; k++)
        digitalWrite(A+k, step_table[3-j][k]);
      delay(myDelay1);
    }
  }
}

void loop()
{
  int SW1 = digitalRead(6);

  if (SW1 != preSW)
  {
    if (SW1 == 1)
    {
      STATUS++;
      STATUS = STATUS % 2;

      if (STATUS == 1)
      {
        digitalWrite(12, 1);
        CW(3, 10);
        digitalWrite(12, 0);
      }
      else if (STATUS == 0)
      {
        digitalWrite(13, 1);
        CCW(2, 20);
        digitalWrite(13, 0);
      }
    }
    preSW = SW1;
  }
}


2017년 6월 20일 화요일

급탕 온수탱크시스템



























Sub 순환auto()

while 1

a1 = GetTagVal("순환1")
a2 = GetTagVal("순환2")
a3 = GetTagVal("순환자동")


if a3 = 1 then

SetTagVal "순환1",1
SetTagVal "순환2",0
Sleep(5000)

SetTagVal "순환2",1
SetTagVal "순환1",0
Sleep(5000)

end if

Wend

End Sub

//////////////////////////////

Sub 대류auto()

while 1

a1 = GetTagVal("대류1")
a2 = GetTagVal("대류2")
a3 = GetTagVal("대류자동")


if a3 = 1 then

SetTagVal "대류1",1
SetTagVal "대류2",0
Sleep(4000)

SetTagVal "대류2",1
SetTagVal "대류1",0
Sleep(4000)

end if

Wend

End Sub

//////////////////////////////////

Sub 증기auto()

bu7 = GetTagVal("증기자동")
aa2 = GetTagVal("탱크온도")
aa4 = GetTagVal("급탕")

while bu7=1

a = aa4-aa2

if a>5 then
b=100
elseif a<-5 then
b=0
else
b=(a+5)*10
end if

SetTagVal "증기밸브",b
Sleep(100)

bu7 = GetTagVal("증기자동")
aa2 = GetTagVal("탱크온도")
aa4 = GetTagVal("급탕")

wend

if bu7=0 then SetTagVal "증기밸브",0

End Sub 

2017년 6월 15일 목요일

아두이노로 Step-Motor 구동하기


//////////////Step-Motor구동


int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 100;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  }

void loop()
{
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }

//////////////1상--1회전후 5초 Sleep

int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 10;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  }

void loop()
{
  int i;
 
  for(i=0; i<50; i++){
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
  delay(5000);
  }


//////////////2상--1회전(CW)후 5초 Sleep

int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 50;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  }

void loop()
{
  int i;
 
  for(i=0; i<50; i++){
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
  delay(5000);
  }



//////////////2상--CW,CCW 각1회전후 5초 Sleep

int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 20;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  }

void loop()
{
  int i;
 
  for(i=0; i<50; i++){
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
  delay(5000);

  for(i=0; i<50; i++){
  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
  delay(5000);
 
  }


//////////////2상--CW 3회전 3초 Sleep, CCW 2회전 5초 Sleep

int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 10;   //100Hz
int myDelay1 = 20; //50Hz

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  }

void loop()
{
  int i;
 
  for(i=0; i<150; i++){
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
  delay(3000);

  for(i=0; i<100; i++){
  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay1);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay1);
  }
  delay(5000);
 
  }


/////////////////////////CW,CCW 제어

int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  }

void CW(int rota, int myDelay)
{
  int i;
 
  for(i=0; i<50*rota; i++)
  {
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
}

void CCW(int rota1, int myDelay1)
{
  int j;
 
  for(j=0; j<50*rota1; j++)
  {
  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay1);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay1);
  }
}

void loop()
{
  CW(5,10);
  delay(3000);
  CCW(3,20);
  delay(5000);
  }



////////////////정역스위치로 CW,CCW 제어


int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  }

void CW(int rota, int myDelay) //정회전함수
{
  int i;
 
  for(i=0; i<50*rota; i++)
  {
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
}

void CCW(int rota1, int myDelay1) //역회전함수
{
  int j;
 
  for(j=0; j<50*rota1; j++)
  {
  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay1);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay1);
  }
}

void loop()
{
  int CWsw = digitalRead(6);
  int CCWsw = digitalRead(7);

  if (CWsw == 1)
  {
    digitalWrite(12,1);
    CW(4,10);         //정방향 4회전,100Hz
  }else{
    digitalWrite(12,0);      
  }

  if (CCWsw == 1)
  {
    digitalWrite(13,1);
    CCW(3,20);        //역방향 3회전,50Hz
  }else{
    digitalWrite(13,0);      
  }
 
  }



//////////////스위치 하나로 CW,CCW 제어



int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int preSW = 0;
int STATUS = 0;

void setup()
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A_NOT,OUTPUT);
  pinMode(B_NOT,OUTPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  }

void CW(int rota, int myDelay)
{
  int i;
 
  for(i=0; i<50*rota; i++)
  {
  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay);

  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay);
  }
}

void CCW(int rota1, int myDelay1)
{
  int j;
 
  for(j=0; j<50*rota1; j++)
  {
  digitalWrite(A,0);
  digitalWrite(B,0);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,1);
  delay(myDelay1);

  digitalWrite(A,0);
  digitalWrite(B,1);
  digitalWrite(A_NOT,1);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,1);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,0);
  delay(myDelay1);

  digitalWrite(A,1);
  digitalWrite(B,0);
  digitalWrite(A_NOT,0);
  digitalWrite(B_NOT,1);
  delay(myDelay1);
  }
}

void loop()
{
  int SW1 = digitalRead(6);


  if (SW1 != preSW)
  {
    if (SW1 == 1)
    {
      STATUS++;
      STATUS = STATUS % 2;

      if (STATUS ==1)
      {
        digitalWrite(12,1);      
        CW(3,10);
        digitalWrite(12,0);
       }
       else if (STATUS == 0)
       {
        digitalWrite(13,1);      
        CCW(2,20);
        digitalWrite(13,0);      
      }
    }
      preSW = SW1;
  }
 
  }


2017년 6월 9일 금요일

미쯔비시PLC_사이먼PC_232연결


1. 미쯔비시(PLC)와 사이먼(PC)측을 연결할 9핀 커넥터를 제작한다.



2. 파라메타를 설정한다.


3. 스위치설정을 맞춘다.









4. IO디바이스종류에서 MITSUBISHI MELSEC Computer Link(AJ71C24)를 선택하고
스테이션,통신블럭,통신포트등을 맞춘다.







5. 태그를 작성한다.





2017년 6월 8일 목요일

4개의 FND display



/////////4개의 FND display


int a[4] = {10,11,12,13}; //FND Control PIN

int fnd_a = 2; //FND의 a핀에 대한 포트값

int table[4][7] = { //FND출력값 테이블
  {0,1,1,0,1,1,1},  //H //2,3,4,5,6,7,8핀
  {1,0,0,1,1,1,1},  //E
  {0,0,0,1,1,1,0},  //L
  {1,1,0,0,1,1,1},  //P
  };

void setup()
{
  int i,j;

  for(i=0; i<7; i++) //FND a핀부터 g핀까지 포트설정
    pinMode(fnd_a+i, OUTPUT);

  for(i=0; j<4; j++) //FND Control PIN
    pinMode(a[j], OUTPUT);

  }

void loop()
  {
   int i,j,k=0;

   for(j=0; j<4; j++){

    for(i=0; i<7; i++){

      digitalWrite(fnd_a+i, table[j][i]); //H,E,L,P순환
      digitalWrite(a[k], LOW); //a[]배열순환
      }

      delay(5);

      digitalWrite(a[k], HIGH);
      k++;
      k = k % 4;
    }
 
  }



////////4자리숫자값을 4개의 FND에 각각표시하기



int a[4] = {10,11,12,13};
int fnd_a = 2;
int buffer[4] = {0};
int analogValue;
unsigned char table[10][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
  };

void setup()
{
  Serial.begin(9600);
 
  int i,j;

  for(i=fnd_a; i<fnd_a+7; i++)    //FND a핀부터 g핀까지 포트설정
    pinMode(i, OUTPUT);

  for(j=0; j<4; j++)    //FND Control PIN
    pinMode(a[j], OUTPUT);
  }

  void fnd(int STATUS)
{
  int j;
  switch(STATUS){
    case 0: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[0][j]); break;
    case 1: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[1][j]); break;
    case 2: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[2][j]); break;
    case 3: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[3][j]); break;
    case 4: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[4][j]); break;
    case 5: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[5][j]); break;
    case 6: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[6][j]); break;
    case 7: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[7][j]); break;
    case 8: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[8][j]); break;
    case 9: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[9][j]); break;
    }
 }

void loop()
  {
    int i,k = 0;
    int STATUS;
    analogValue = analogRead(A0);
    Serial.println(analogValue);  
     
    buffer[3] = analogValue/1000;
    buffer[2] = (analogValue%1000)/100;
    buffer[1] = (analogValue%100)/10;
    buffer[0] = analogValue%10;

    for(i=0; i<4; i++){
      STATUS = buffer[3-i];
      fnd(STATUS);
      digitalWrite(a[k],LOW);
      delay(5);
      digitalWrite(a[k],HIGH);
      k++;
      k=k % 4;
      }
    }



//////////블루투스숫자입력-FND출력


#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3);

int fnd_a = 5;

unsigned char table[9][7] = {
   
    {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
  };

void setup()
{
  BTSerial.begin(9600);
 // Serial.begin(9600);

  int i,j;

  for(i=fnd_a; i<fnd_a+7; i++)    //FND a핀부터 g핀까지 포트설정
    pinMode(i, OUTPUT);

  }
 
void fnd(int STATUS)
{
  int j;
  switch(STATUS){  
    case 1: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[0][j]); break;
    case 2: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[1][j]); break;
    case 3: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[2][j]); break;
    case 4: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[3][j]); break;
    case 5: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[4][j]); break;
    case 6: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[5][j]); break;
    case 7: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[6][j]); break;
    case 8: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[7][j]); break;
    case 9: for(j=0; j<7; j++) digitalWrite(fnd_a+j, table[8][j]); break;
    }
 }

void loop()
  {
    int STATUS,ch;
   
    if(BTSerial.available())
    {
      STATUS = BTSerial.read();
    //  Serial.println(STATUS);
      ch=(STATUS - '0');
    //  Serial.println(ch);
      fnd(ch);
      delay(10);
      }  
  }

2017년 6월 1일 목요일

Bluetooth 연결테스트



////////////블루투스 통신읽기


#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3);
int analog0;

void setup()
{
//  Serial.begin(9600);
  BTSerial.begin(9600);

  }

void loop()
  {
    analog0 = analogRead(A0);
    BTSerial.print("poten#: ");
    BTSerial.println(analog0);
//    Serial.println(analog0);
         
    delay(500);    
    }


/////////////블루투스 설정변경

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3);

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600);
  }

void loop()
{
  if (BTSerial.available()) {
    Serial.write(BTSerial.read()); }

  if (Serial.available()) {
    BTSerial.write(Serial.read()); }
  }



////////////시리얼 입출력


int inData;
int LED1 = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  }

void loop()
  {
    if(Serial.available()>0)
    {
      inData = Serial.read();
      if (inData == 'A')
        digitalWrite(LED1,HIGH);
      if (inData == 'D')
        digitalWrite(LED1,LOW);
     
       delay(1000);
      }  
  }


////////////////블루투스 입출력


#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3);

int inData;
int LED1 = 8;

void setup()
{
  BTSerial.begin(9600);
  pinMode(LED1,OUTPUT);
  }

void loop()
  {
    if(BTSerial.available())
    {
      inData = BTSerial.read();
      if (inData == 'A')
        digitalWrite(LED1,HIGH);
      if (inData == 'D')
        digitalWrite(LED1,LOW);
     
       delay(1000);
      }  
  }


//////////////빛센서cds 출력


#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3);



void setup()
{
  BTSerial.begin(9600);

  }

void loop()
  {
   int cds = analogRead(A0);
   BTSerial.print("Cds: ");
   BTSerial.println(cds);

   delay(500);
  }


///////////////'0'문자형테스트


int inData;
int cha;
int LED1 = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  }

void loop()
  {
    if(Serial.available())
    {
      inData = Serial.read();
      cha = (inData-'0');
      if (cha == 1)
        digitalWrite(LED1,HIGH);
      if (cha == 0)
        digitalWrite(LED1,LOW);
           
       delay(1000);
      }  
  }