질문게시판 > 답변 : 아두이노 블루투스 통신 질문

TODAY22,301 TOTAL9,409,920
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 질문게시판 | 답변 : 아두이노 블루투스 통신 질문

페이지 정보

작성자 master 작성일2018-10-17 10:01 조회10,191회 댓글0건

본문

	

 

아래 소스는 비상 정지 버튼 기능만 미포함된
사실상 전체 소스 코드에 해당하는 원점 이동  길이 등분을  상태에서
다음 버튼을 누를때마다 다음 포인트로 이동이 되도록 설정한 소스 코드입니다.
(원하는 포인트로의 이동  비상 정지 버튼 구현 소스는  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 3
FreqPeriodCounter 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 핀
  pinMode2,OUTPUT); pinMode9,OUTPUT); pinMode8,OUTPUT); //EN 핀 //->CP 핀 //->DIR 핀
  pinMode7,INPUT);  pinMode4,INPUT);  pinMode3,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)digitalWrite9,0); delayMicroseconds(5);
          if(digitalRead(4)==1)digitalWrite(11,1); delayMicroseconds(5);
          if(digitalRead(7)==1)digitalWrite9,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=0y<Yy++){
          if(y%2==0){
            for(x=0x<Xx++){
              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=truenextISR(); }
                  if(X==1&&Y!=1){ steppermotor2.step(0); for(i=0;i<(intVal2)/2;i++)steppermotor.step(-5120);  next=truenextISR(); }
                  if(X!=1&&Y==1){ steppermotor.step(0);  for(i=0;i<(intVal3)/2;i++)steppermotor2.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ steppermotor.step(0); steppermotor2.step(0); next=truenextISR(); }
                }
                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=truenextISR(); }
                  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=truenextISR(); }
                  if(X==1&&Y!=1){ steppermotor2.step(0); for(i=0;i<X0;i++)steppermotor.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ steppermotor2.step(0); for(i=0;i<X0;i++)steppermotor.step(-5120); next=truenextISR(); }
                }
                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=truenextISR(); }
                  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=truenextISR(); }
                  if(X!=1&&Y==1){ steppermotor.step(0); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ steppermotor.step(0); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=truenextISR(); }
                }
                if(X0!=0&&Y0!=0){ for(i=0;i<X0;i++)steppermotor.step(-5120); for(i=0;i<Y0;i++)steppermotor2.step(-5120); next=truenextISR(); }
              }
              else if(y!=0&&x==0){
                if(Y0==0){
                  if(Y>1){ for(i=0;i<(intVal3)/(Y-1);i++)steppermotor2.step(-5120); next=truenextISR(); }
                }
                if(Y0!=0){
                  if(Y>1){ for(i=0;i<(intVal3-2*Y0)/(Y-1);i++)steppermotor2.step(-5120); next=truenextISR(); }
                }
              }
              else{
                if(X0==0&&Y0==0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                }
                if(X0!=0&&Y0==0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                }
                if(X0==0&&Y0!=0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                }
                if(X0!=0&&Y0!=0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(-5120); next=truenextISR(); }
                }
              }
              next=truenextISR();
            }
          }
          if(y%2==1){
            for(x=X-1x>=0x--){
              if(x==X-1){
                if(Y0==0){
                  if(Y>1){ for(i=0;i<(intVal3)/(Y-1);i++)steppermotor2.step(-5120); next=truenextISR(); }
                }
                if(Y0!=0){
                  if(Y>1){ for(i=0;i<(intVal3-2*Y0)/(Y-1);i++)steppermotor2.step(-5120); next=truenextISR(); }
                }
              }
              else{
                if(X0==0&&Y0==0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                }
                if(X0!=0&&Y0==0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                }
                if(X0==0&&Y0!=0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                }
                if(X0!=0&&Y0!=0){
                  if(X!=1&&Y==1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                  if(X!=1&&Y!=1){ for(i=0;i<(intVal2-2*X0)/(X-1);i++)steppermotor.step(5120); next=truenextISR(); }
                }
              }
              next=truenextISR();
            }
          }
        }
        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 microseconds
      frequency=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.259Serial.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 digits
        delay(500);
      }
      continue;
    }
    next=false;
    break;
  }
}

 

시간이 많이 걸리는 코드를 보면

nextISR() 함수에서 딜레이 500ms를 사용한 것이 하나 있고

나머지는 스텝모터를 1회전 가까이 하는 함수밖에 없습니다.

 

delay(500);  

이 딜레이 경우에는

 

for(int x=0x<50x++){
  delay(10);
  if(BTSerial.available()){  
    블루투스수신 처리
  }
}

딜레이를 반복문으로 나누어서 실행하면서 블루투스 수신을 체크하면 될테고요

 

 

steppermotor.step(5120);

스텝모터도 한번에 구동하지말고

 

for(int x=0x<512x++){
  steppermotor.step(10);
  if(BTSerial.available()){  
    블루투스수신 처리
  }
}

적당히 나누어서 구동하면서 블루투스를 체크하면 됩니다.

 

  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 0

조회수 10,191

등록된 댓글이 없습니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.
ATMEGA128PWMLED초음파
아두이노AVR블루투스LCD
UART모터적외선ATMEGA
전체 스위치 센서
질문게시판 목록
제목 작성자 작성일 조회
공지 MCU, AVR, 아두이노 등 전자공학에 관련된 질문은 질문게시판에서만 작성 가능합니다. 스태프 19-01-15 24560
공지 사이트 이용 안내댓글[31] master 17-10-29 39406
질문 ATMEGA128 외부 인터럽트 수행.댓글[2] 이미지첨부파일 소고기카레 25-05-11 299
질문 아두이노 메가 esp32-cam 질문입니다.댓글[1] 문외한사람 25-04-29 286
질문 esp32와 pms7003연결해서 데이터를 받고 싶은데 에러가 떠요댓글[1] 이미지첨부파일 boar 25-03-27 341
질문 DLM-64DCAA 고장나서 수리하고 싶습니다댓글[2] 이미지첨부파일 공부하고싶은콩 25-03-11 713
질문 로봇 얼굴 제작에 대하여 질문드립니다.댓글[4] 카모킹스 25-03-03 2384
질문 모터와 모터드라이브 질문있습니다댓글[1] ponyo 25-02-19 561
질문 공통음극형 FND를 9V로 동작하게 하고 싶어요.댓글[7] 멘토 25-01-21 6332
질문 oled 모듈 연결 질문입니다!댓글[1] 펌린이 24-12-17 655
질문 Atmega 128 타이머 질문있습니다댓글[1] ednda 24-12-11 680
답변 답변글 답변 : Atmega 128 타이머 질문있습니다 master 24-12-11 403
질문 ATmega128 2개로 hc-05,hc-06블루트수 통신 동콩이 24-12-04 510
답변 답변글 답변 : ATmega128 2개로 hc-05,hc-06블루트수 통신댓글[4] master 24-12-04 3499
질문 lm75a 온도센서 관련 질문입니다.댓글[6] 이미지첨부파일 hanmw0707 24-12-01 6691
질문 리얼타임클락 질문이요 ㅠㅠ댓글[1] 이미지 팬케이크 24-11-22 608
질문 가속도센서 2개를 강아지 2마리에 각각 달아서 스마트폰으로 움직임을 보고 싶은데요댓글[1] gainomax 24-11-21 802
질문 라즈베리파이 4B 사용 중 막히는 부분 질문합니다댓글[1] 한마바키 24-11-12 6894
질문 스위치로 PI 모터 제어 바밤 24-11-11 448
답변 답변글 답변 : 스위치로 PI 모터 제어 master 24-11-12 454
질문 아트메가128 마이크로프로세서댓글[3] 옹심이 24-11-05 1679
질문 초음파 센서로 장애물 인식댓글[1] 나난ㄴ 24-10-31 720
질문 이런 투명 LCD는 뭐라고 부르나요?댓글[2] 이미지 펌린이 24-10-29 1065
질문 모터제어 중 RPM 계산 질문입니다.댓글[3] suuuuuuuh 24-10-24 1532
질문 아트메가 128과 블루투스 모듈(HC-06) 연결 질문댓글[1] 메가아트 24-10-24 711
질문 ATmega128 질문 DFplayer mini댓글[1] Miin 24-10-18 7338
질문 스탭모터 3개 회로도 질문.. 삉삉이 24-10-11 566
답변 답변글 답변 : 스탭모터 3개 회로도 질문.. 이미지 master 24-10-12 614
질문 수분수위센서와 멀티플렉서댓글[3] 김고래쓰 24-10-10 1922
질문 ATmegq128 PI 제어기 추가댓글[1] 바밤 24-10-08 743
게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기