정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

두개의 코딩을 하나로 합치는 방법이 있을까요?
김성진 | 2015-11-24

가스밸브를 잠궈주는 장치를 만들고 있습니다.

가스센서를 달아서 누출되는 가스를 감지하고

모터를 이용해서 밸브를 잠글 수 있도록 장치를 만들고 있는데요,

블루투스 모듈을 통해서 센서값을 휴대폰으로 전송하고 

휴대폰을 통해 모터를 조작하려 합니다.

 

현재 가스 센서값을 읽어오는것과 휴대폰을 통해 모터를 조작하는 각각의 코딩은 성공했습니다만

두개의 코딩을 하나로 합쳐서 시행하고 싶습니다.

 

가스 센서 코딩

const int SENS_PIN = A0;

const int LED_PIN = 13;

int sensorValue = 0;

boolean toggle = true;

void setup()

{ Serial.begin(9600); }

void loop()

{ if (Serial.available())

{ Serial.read();

sensorValue = analogRead(SENS_PIN);

Serial.println(sensorValue);

digitalWrite(LED_PIN, toggle); // toggle the LED

toggle = !toggle;

} }

모터 조작 코딩

int motorPin1 = 3; // pin 2 on L293D IC

int motorPin2 = 4; // pin 7 on L293D IC

int enablePin = 5; // pin 1 on L293D IC

int state;

int flag=0; //makes sure that the serial only prints once the state

 

void setup() {

// sets the pins as outputs:

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

// sets enablePin high so that motor can turn on:

digitalWrite(enablePin, HIGH);

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

}

 

void loop() {

//if some date is sent, reads it and saves in state

if(Serial.available() > 0){

state = Serial.read();

flag=0;

}

// if the state is '0' the DC motor will turn off

if (state == '0') {

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low

digitalWrite(motorPin2, LOW); // set pin 7 on L293D low

if(flag == 0){

Serial.println("Motor: off");

flag=1;

} }

// if the state is '1' the motor will turn right

else if (state == '1') {

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low

digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high

if(flag == 0){

Serial.println("Motor: right");

flag=1;

} }

// if the state is '2' the motor will turn left

else if (state == '2') {

digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high

digitalWrite(motorPin2, LOW); // set pin 7 on L293D low

if(flag == 0){

Serial.println("Motor: left");

flag=1;

} } }

이 두개의 코딩을 하나로 합치고 싶습니다.

그래서 제가 생각해본게

변수 선언부분과 반복문을 각각 합쳐주는 것인데 업로드가 안되더라구요.

두개의 코딩을 하나로 합쳐주는 방법이 있을지 궁금합니다.

const int SENS_PIN = A0;

const int LED_PIN = 13;

int sensorValue = 0;

boolean toggle = true;

 

int motorPin1 = 3; // pin 2 on L293D IC

int motorPin2 = 4; // pin 7 on L293D IC

int enablePin = 5; // pin 1 on L293D IC

int state;

int flag=0; //makes sure that the serial only prints once the state

 

void setup() {

// sets the pins as outputs:

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

// sets enablePin high so that motor can turn on:

digitalWrite(enablePin, HIGH);

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

}

 

void loop()

{ if (Serial.available())

{ Serial.read();

sensorValue = analogRead(SENS_PIN);

Serial.println(sensorValue);

digitalWrite(LED_PIN, toggle); // toggle the LED

toggle = !toggle;

}

//if some date is sent, reads it and saves in state

if(Serial.available() > 0)

{ state = Serial.read();

flag=0; }

// if the state is '0' the DC motor will turn off

if (state == '0')

{

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low

digitalWrite(motorPin2, LOW); // set pin 7 on L293D low

if(flag == 0)

{

Serial.println("Motor: off");

flag=1;

} }

// if the state is '1' the motor will turn right

else if (state == '1')

{

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low

digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high

if(flag == 0)

{

Serial.println("Motor: right");

flag=1;

} }

// if the state is '2' the motor will turn left

else if (state == '2')

{

digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high

digitalWrite(motorPin2, LOW); // set pin 7 on L293D low

if(flag == 0)

{

Serial.println("Motor: left");

flag=1;

} }

}

 

프로필사진

수박쨈 2015-11-25 15:13:06

핸드폰에서 데이터를 받아올때 블루투스를 사용하는 것이 아닌가요?

그럴경우 Serial통신으로 데이터를 받아오지 않을 텐데....

지금은 Serial.Read()와 Serial.write()의 구분이 없습니다.

아두이노에서 핸드폰에서 데이터를 보낼때는 write()을 통해 보내고

핸드폰에서 아두이노로 데이터를 보낼때는 Read()로 읽어옵니다.

 Serial.available() 함수는 데이터를 외부에서 아두이노로 읽어올 때 사용하는 함수 입니다.

 

원리를 생각하면 쉽게 만드실거 같은데

먼저 가스센서의 값은 매 루프마다 analogRead()를 통해 꾸준하게 받아와야 합니다.(받아오는 동시에 데이터 전송)

그리고 특정값 이상(혹은 이하)으로 검출 될 경우 시리얼통신을 통해 핸드폰에서 데이터를 받아와 모터를 제어합니다.

이전글   |    도난방지 관련 어플이요 도와주세요 2015-11-24
다음글   |    서보모터모듈 사용질문입니다. 2015-11-25