BASIC4MCU | 질문게시판 | 블루투스에서 아두이노로 신호전달에 대하여
페이지 정보
작성자 밍경 작성일2023-02-13 17:39 조회2,027회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=22319
본문
#include <SoftwareSerial.h>#include <NewPing.h> //for the Ultrasonic sensor function library.//L298N motor control pins#define RightEnable 5#define Right1 A1#define Right2 A0#define LeftEnable 6#define Left1 A2#define Left2 A3//Ultrasonic sensor control pins#define trig_pin 8#define echo_pin 9//IR sensor control pins#define LLeft 4#define CLeft 2#define CRight 3#define RRight 10int max_distance = 1000;int cm = 0;boolean skiptime = false;NewPing sonar(trig_pin, echo_pin, max_distance);SoftwareSerial Bluetooth(11, 12);void setup() {Serial.begin(9600);Bluetooth.begin(9600);pinMode(RightEnable, OUTPUT);pinMode(Right1, OUTPUT);pinMode(Right2, OUTPUT);pinMode(LeftEnable, OUTPUT);pinMode(Left1, OUTPUT);pinMode(Left2, OUTPUT);pinMode(LLeft, INPUT);pinMode(CLeft, INPUT);pinMode(CRight, INPUT);pinMode(RRight, INPUT);digitalWrite(RightEnable, HIGH);digitalWrite(LeftEnable, HIGH);}void loop() {if (Bluetooth.available()) {int c = Bluetooth.read();Serial.write(Bluetooth.read());Serial.println(Bluetooth.read());Serial.println(c);{if (c = 1) {int LC = digitalRead(CLeft);int RC = digitalRead(CRight);int RR = digitalRead(RRight);int LL = digitalRead(LLeft);Serial.println(readPing());if (readPing() < 0) {MotorStop();} else if (RR == 1) {if (!skiptime) {Straight_();skiptime = true;Serial.print("Straight_1");} else if (LL == 1) {MotorStop();Turn();MotorStop_Start_Point();} else {MotorStop();Turn();}} else if (LC == 0 && RC == 1) {Left();Serial.println("Left1");} else if (LC == 1 && RC == 0) {Right();Serial.println("Right1");} else if (LC == 0 && RC == 0) {Straight();Serial.println("Straight1");}}else if (c = 2) {int LC = digitalRead(CLeft);int RC = digitalRead(CRight);int RR = digitalRead(RRight);int LL = digitalRead(LLeft);if (readPing() < 0) {MotorStop();} else if (RR == 1) {if (!skiptime) {Straight_();skiptime = true;Serial.println("Straight_2");} else if (LL == 1) {MotorStop();Turn();MotorStop_Start_Point();} else {MotorStop();Turn();}} else if (LC == 0 && RC == 1) {Left();Serial.println("Left2");} else if (LC == 1 && RC == 0) {Right();Serial.println("Right2");} else if (LC == 0 && RC == 0) {Straight();Serial.println("Straight2");}}}}}// if 처음// loopvoid MotorStop_Start_Point() {analogWrite(Right1, 0);analogWrite(Right2, 0);analogWrite(Left1, 0);analogWrite(Left2, 0);delay(10000);}void MotorStop() {analogWrite(Right1, 0);analogWrite(Right2, 0);analogWrite(Left1, 0);analogWrite(Left2, 0);delay(1000);}void Straight() {analogWrite(RightEnable, 90);analogWrite(LeftEnable, 95);analogWrite(Right1, 200);analogWrite(Right2, 0);analogWrite(Left1, 200);analogWrite(Left2, 0);}void Straight_() {analogWrite(RightEnable, 90);analogWrite(LeftEnable, 90);analogWrite(Right1, 200);analogWrite(Right2, 0);analogWrite(Left1, 200);analogWrite(Left2, 0);delay(300);}void Right() {analogWrite(RightEnable, 90);analogWrite(LeftEnable, 0);analogWrite(Right1, 200);analogWrite(Right2, 0);analogWrite(Left1, 200);analogWrite(Left2, 0);}void Left() {analogWrite(RightEnable, 0);analogWrite(LeftEnable, 90);analogWrite(Right1, 200);analogWrite(Right2, 0);analogWrite(Left1, 200);analogWrite(Left2, 0);}void TurnRight() {analogWrite(RightEnable, 100);analogWrite(LeftEnable, 100);analogWrite(Right1, 200);analogWrite(Right2, 0);analogWrite(Left1, 0);analogWrite(Left2, 0);delay(2350);}void Turn() {analogWrite(RightEnable, 120);analogWrite(LeftEnable, 100);analogWrite(Right1, 200);analogWrite(Right2, 0);analogWrite(Left1, 0);analogWrite(Left2, 200);delay(2500);}void TurnLeft() {analogWrite(RightEnable, 100);analogWrite(LeftEnable, 100);analogWrite(Right1, 0);analogWrite(Right2, 0);analogWrite(Left1, 200);analogWrite(Left2, 0);delay(2250);}int readPing() {delay(50);int cm = sonar.ping_cm();return cm;delay(50);}안녕하세요, 앱인벤터를 통해 블루투스로 아두이노에 신호를 전송하면 라인트레이서가 1이란 신호를 인식하고 c=1의 마지막 명령어인 "Straight1" 만 인식합니다.그리고 c=2 라는 값을 인식해도 2에 해당하는 명령을 실행하지않고 똑같이 "Straight1" 값만 출력됩니다. 1번을 반복적으로 눌리면 눌릴때마다 신호값이 다시 인식되어누르는 당시의 상황 ("Left", "Right" ) 값을 출력해내긴합니다. 1이란 신호값을 한번만 받고도 c=1의 모든 명령을 수행하게하는 방법이 있을까요?그리고 시리얼모니터를 보면 1이나 2라는 신호를 주면 1-1 2-1 이런값으로 계속 입력이됩니다. 이것도 해결방법이 있을까요?도움 주시면 감사하겠습니다.
댓글 0
조회수 2,027등록된 댓글이 없습니다.