BASIC4MCU | 질문게시판 | 답변 : 아두이노 블루투스 통신 질문
페이지 정보
작성자 master 작성일2018-10-17 10:01 조회10,191회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=3629
본문
아래 소스는 비상 정지 버튼 기능만 미포함된사실상 전체 소스 코드에 해당하는 원점 이동 및 길이 등분을 한 상태에서다음 버튼을 누를때마다 다음 포인트로 이동이 되도록 설정한 소스 코드입니다.(원하는 포인트로의 이동 및 비상 정지 버튼 구현 소스는 타 PC에 보관 중인데,현재 회사에 두고 온 상태라 위 수준의 소스 코드로 질문을 드립니다.)아래의 소스 코드는 블루투스를 통해<0,0,0,0,0,0,0>처럼<다음에 숫자가 0이면 2개의 스텝 모터 및 볼 스크류에 연결된 장치가 원점으로 이동하는 소스코드입니다.<다음에 숫자가 1이라면,2,3번 위치 숫자에 해당하는 길이만큼 이동하되,4,5번 숫자의 행렬 만큼의 포인트를 이동하지만다음 버튼을 눌러야만 다음 포인트로 이동하는 구조입니다.6,7번은 오프셋 길이로서 둘 다 0일 경우 첫 포인트 지점은 원점부터 시작되는 구조이구요.질문을 드리고 싶은 것은 블루투스로 한 번 명령을 내리고 나면,함수를 다 처리하기 전에는 다른 명령을 내릴 수 없어서 다음 버튼의 경우 푸쉬 버튼을 달아주었는데..하나의 블루투스로 푸쉬 버튼 기능까지도 구현이 가능한지혹,블루투스 장치 2개가 필요하다면 하나의 스마트폰으로 2개의 블루투스를 동시에 제어가 가능한지 알고 싶습니다.(정확히는 동시에 제어하고 있다 느낄 만큼의 약간의 시간차를 두고 제어하는 방식이 되겠지만요..)//#include <Stepper.h>//#include <SoftwareSerial.h>SoftwareSerial BTSerial(0,1);//#include <FreqPeriodCounter.h>const byte counterPin=3;const byte counterInterrupt=1; //=pin 3FreqPeriodCounter counter(counterPin,micros,0);//int pos=0;int i;unsigned long windspeed;volatile boolean next=false;//void setup(){Serial.begin(9600);BTSerial.begin(9600);attachInterrupt(counterInterrupt,counterISR,CHANGE);attachInterrupt(0,nextISR,CHANGE);pinMode(13,OUTPUT); pinMode(12,OUTPUT); pinMode(11,OUTPUT); //EN 핀 //->DIR 핀 //->CP 핀pinMode( 2,OUTPUT); pinMode( 9,OUTPUT); pinMode( 8,OUTPUT); //EN 핀 //->CP 핀 //->DIR 핀pinMode( 7,INPUT); pinMode( 4,INPUT); pinMode( 3,INPUT); pinMode(6,INPUT);}//void loop(){String str="";if(BTSerial.available()){str=BTSerial.readString();//if(str.charAt(0)=='<' and str.charAt(str.length()-1)=='>'){int first=str.indexOf(","); int second=str.indexOf(",",first+1); int third=str.indexOf(",",second+1); int fourth=str.indexOf(",",third+1);int fifth=str.indexOf(",",fourth+1); int sixth=str.indexOf(",",fifth+1); int seventh=str.indexOf(">",sixth);//char cTempData[5]; char cTempData2[5]; char cTempData3[5]; char cTempData4[5]; char cTempData5[5]; char cTempData6[5]; char cTempData7[5];//str.substring(1,first).toCharArray(cTempData,5); str.substring(first+1,second).toCharArray(cTempData2,5);str.substring(second+1,third).toCharArray(cTempData3,5); str.substring(third+1,fourth).toCharArray(cTempData4,5);str.substring(fourth+1,fifth).toCharArray(cTempData5,5); str.substring(fifth+1,sixth).toCharArray(cTempData6,5);str.substring(sixth+1,seventh).toCharArray(cTempData7,5);//float intVal2=atoi(cTempData2); float intVal3=atoi(cTempData3); float X=atoi(cTempData4); float Y=atoi(cTempData5); float X0=atoi(cTempData6); float Y0=atoi(cTempData7);//Serial.print(atoi(cTempData)); Serial.print(","); Serial.print(intVal2); Serial.print(","); Serial.print(intVal3); Serial.print(",");Serial.print(X); Serial.print(","); Serial.print(Y); Serial.print(","); Serial.print(X0); Serial.print(","); Serial.println(Y0);//while(atoi(cTempData)==0){if(digitalRead(4)==1){ digitalWrite(13,0); digitalWrite(11,1); digitalWrite(12,1); }if(digitalRead(7)==1){ digitalWrite(2,0); digitalWrite(9,1); digitalWrite(8,1); }for(int i=0;i<200;i++){if(digitalRead(4)==1)digitalWrite(11,0); delayMicroseconds(5);if(digitalRead(7)==1)digitalWrite( 9,0); delayMicroseconds(5);if(digitalRead(4)==1)digitalWrite(11,1); delayMicroseconds(5);if(digitalRead(7)==1)digitalWrite( 9,1); delayMicroseconds(5);}//if(digitalRead(4)==1|digitalRead(7)==1)continue;break;}//while(atoi(cTempData)==1){Stepper steppermotor(6400,12,11); steppermotor.setSpeed(1000);Stepper steppermotor2(6400,8,9); steppermotor2.setSpeed(1000);int x,y;for(y=0; y<Y; y++){if(y%2==0){for(x=0; x<X; x++){if(x==0&&y==0){if(X0==0&&Y0==0){if(X==1&&Y==1){ for(i=0;i<(intVal2)/2;i++)steppermotor.step(-5120); for(i=0;i<(intVal2)/2;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X==1&&Y!=1){ steppermotor2.step(0); for(i=0;i<(intVal2)/2;i++)steppermotor.step(-5120); next=true; nextISR(); }if(X!=1&&Y==1){ steppermotor.step(0); for(i=0;i<(intVal3)/2;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ steppermotor.step(0); steppermotor2.step(0); next=true; nextISR(); }}if(X0!=0&&Y0==0){if(X==1&&Y==1){ for(i=0;i<X0;i++)steppermotor.step(-5120); for(i=0;i<(intVal3)/2;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X!=1&&Y==1){ for(i=0;i<X0;i++)steppermotor.step(-5120); for(i=0;i<(intVal3)/2;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X==1&&Y!=1){ steppermotor2.step(0); for(i=0;i<X0;i++)steppermotor.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ steppermotor2.step(0); for(i=0;i<X0;i++)steppermotor.step(-5120); next=true; nextISR(); }}if(X0==0&&Y0!=0){if(X==1&&Y==1){ for(i=0;i<(intVal2)/2;i++)steppermotor.step(-5120); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X==1&&Y!=1){ for(i=0;i<(intVal2)/2;i++)steppermotor.step(-5120); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X!=1&&Y==1){ steppermotor.step(0); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ steppermotor.step(0); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=true; nextISR(); }}if(X0!=0&&Y0!=0){ for(i=0;i<X0;i++)steppermotor.step(-5120); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=true; nextISR(); }}else if(y!=0&&x==0){if(Y0==0){if(Y>1){ for(i=0;i<(intVal3)/(Y-1);i++)steppermotor2.step(-5120); next=true; nextISR(); }}if(Y0!=0){if(Y>1){ for(i=0;i<(intVal3-2*Y0)/(Y-1);i++)steppermotor2.step(-5120); next=true; nextISR(); }}}else{if(X0==0&&Y0==0){if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }}if(X0!=0&&Y0==0){if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }}if(X0==0&&Y0!=0){if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }}if(X0!=0&&Y0!=0){if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=true; nextISR(); }}}next=true; nextISR();}}if(y%2==1){for(x=X-1; x>=0; x--){if(x==X-1){if(Y0==0){if(Y>1){ for(i=0;i<(intVal3)/(Y-1);i++)steppermotor2.step(-5120); next=true; nextISR(); }}if(Y0!=0){if(Y>1){ for(i=0;i<(intVal3-2*Y0)/(Y-1);i++)steppermotor2.step(-5120); next=true; nextISR(); }}}else{if(X0==0&&Y0==0){if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }}if(X0!=0&&Y0==0){if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }}if(X0==0&&Y0!=0){if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }}if(X0!=0&&Y0!=0){if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=true; nextISR(); }}}next=true; nextISR();}}}break;}}}}//void counterISR(){ counter.poll(); }//void nextISR(){while(next==true){if(digitalRead(6)!=0){pulse1=pulseIn(pulsePin,1); pulse0=pulseIn(pulsePin,0); pulseTotal=pulse1+pulse0; // Time period of the pulse in microsecondsfrequency=100000/pulseTotal; // Frequency in Hertz(Hz)if(5>=0.259*frequency&&0.259*frequency>=0){total=total-readings[readIndex]; Serial.print(total); Serial.print(","); // subtract the last reading:readings[readIndex]=frequency*0.259; Serial.print(readings[readIndex]); Serial.print(","); // read from the sensor:total=total+readings[readIndex]; Serial.print(total); Serial.print(","); // add the reading to the total:readIndex=readIndex+1; // advance to the next position in the array:if(readIndex>=numReadings){ readIndex=0; } // if we're at the end of the array...//...wrap around to the beginning:float average=(float)total/numReadings; // calculate the average:Serial.print(0.259*frequency); Serial.print(","); Serial.println(average); // send it to the computer as ASCII digitsdelay(500);}continue;}next=false;break;}}
시간이 많이 걸리는 코드를 보면
nextISR() 함수에서 딜레이 500ms를 사용한 것이 하나 있고
나머지는 스텝모터를 1회전 가까이 하는 함수밖에 없습니다.
delay(500);이 딜레이 경우에는
for(int x=0; x<50; x++){delay(10);if(BTSerial.available()){블루투스수신 처리}}딜레이를 반복문으로 나누어서 실행하면서 블루투스 수신을 체크하면 될테고요
steppermotor.step(5120);스텝모터도 한번에 구동하지말고
for(int x=0; x<512; x++){steppermotor.step(10);if(BTSerial.available()){블루투스수신 처리}}적당히 나누어서 구동하면서 블루투스를 체크하면 됩니다.
댓글 0
조회수 10,191등록된 댓글이 없습니다.