提问人:shugo 提问时间:11/17/2023 更新时间:11/17/2023 访问量:16
有人可以指出这两个代码之间的主要区别吗?[已结束]
Can someone point out major differences between this two code? [closed]
问:
这是我朋友的代码
//BLUETOOTH + INPUTS
#include "BluetoothSerial.h"
#include <ESP32Servo.h>
#if !defined(CONFIG_BT_ENABLED) !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` and enable it.
#endif
BluetoothSerial SerialBT;
Servo myservo;
int servoPin = 26;
// IR SENSOR (4 PINS)
#define SENSOR_PIN_MID 12 // connected to D0 // mid
#define SENSOR_PIN_RIGHT 13 //right
#define SENSOR_PIN_LEFT 14 // left
// MOTOR
const int mot1right = 15; //motor 1 = backward motor 2 = forward
const int mot2right = 2;
const int mot1left = 17;
const int mot2left = 16;
bool isAuto = false; // false = manually control; true = autonamous
//IR black and white
int remember = 0;
void setup() {
// initialize the ESP's pin for motor as output
pinMode (mot1right, OUTPUT);
pinMode (mot2right, OUTPUT);
// initialize the ESP's pin as an input
pinMode (SENSOR_PIN_MID, INPUT);
pinMode (SENSOR_PIN_RIGHT, INPUT);
pinMode (SENSOR_PIN_LEFT, INPUT);
Serial.begin(115200); //<<<<< check nak 115200 atau 9600
SerialBT.begin("Group 8");
Serial.println("The device started, now you can pair it with Bluetooth!");
myservo.attach(servoPin,500,2400);
}
void loop() {
if (isAuto == false) {
if (SerialBT.available()) {
char inputChar = SerialBT.read();
// Check the received character and send a response over Bluetooth
if (inputChar == 'F') {
Serial.println("You entered 'A'. This is the response for 'A'."); //Go Foward
analogWrite(mot1left, 0);
analogWrite(mot2left, 255);
analogWrite(mot1right, 0);
analogWrite(mot2right, 255);
delay(500);
}
else if (inputChar == 'B') {
Serial.println("You entered 'B'. This is the response for 'B'."); //Go Backward
analogWrite(mot1left, 255);
analogWrite(mot2left, 0);
analogWrite(mot1right, 255);
analogWrite(mot2right, 0);
delay(500);
}
else if (inputChar == 'L') {
Serial.println("You entered 'L'. This is the response for 'L'."); //Go Left (Foward)
analogWrite(mot1left, 0);
analogWrite(mot2left, 255);
analogWrite(mot1right, 255);
analogWrite(mot2right, 0);
delay(500);
}
else if (inputChar == 'R') {
Serial.println("You entered 'R'. This is the response for 'R'."); //Go Right (Foward)
analogWrite(mot1left, 255);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 255);
delay(500);
}
else if (inputChar == 'LB') {
Serial.println("You entered 'LB'. This is the response for 'LB'."); //Go Left Backward
analogWrite(mot1left, 255);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 255);
delay(500);
}
else if (inputChar == 'RB') {
Serial.println("You entered 'RB'. This is the response for 'RB'."); //Go Right Backward
analogWrite(mot1left, 0);
analogWrite(mot2left, 255);
analogWrite(mot1right, 255);
analogWrite(mot2right, 0);
delay(500);
}
else if (inputChar == 'X') {
Serial.println("You entered 'X'. This is the response for 'X'."); //Go Right Backward
analogWrite(mot1left, 0);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 0);
delay(500);
}
else if (inputChar == 'O') { //servo open
myservo.write(90);
}
else if (inputChar == 'C') { //servo close
myservo.write(0);
}
else if (inputChar == 'Y') { //switch to automatic
isAuto = true;
} else {
Serial.println("Unknown input. Please enter 'A' / 'B' / 'R' / 'L' / 'RB' / 'LB' / 'X' to get a response.");
}
}
}
else {
int stateM = digitalRead(SENSOR_PIN_MID);
int stateR = digitalRead(SENSOR_PIN_RIGHT);
int stateL = digitalRead(SENSOR_PIN_LEFT);
if (SerialBT.available()) { // switch back to manual
char inputChar = SerialBT.read();
if (inputChar == 'Y') {
isAuto = false;
}
}
if(stateL == LOW && stateM == HIGH && stateR == LOW) {
mid();
remember = 1;
delay(30);
} else if ((stateL == LOW && stateM == HIGH && stateR == HIGH) (stateL == LOW && stateM == LOW && stateR == HIGH)) {
right();
remember = 2;
delay(37);
} else if ((stateL == HIGH && stateM == HIGH && stateR == LOW) || (stateL == HIGH && stateM == LOW && stateR == LOW)) {
left();
delay(37);
remember = 3;
} else if ((stateL == LOW && stateM == LOW && stateR == LOW)){
/*switch(remember){
case 1: mid(); break;
case 2: left(); break;
case 3: right(); break;
default: break;
}*/
;
}
else {
stopAndShoot();
remember = 0;
}
}
}
void mid() { // IR MID
// read the state of the the input pin: // high = 1 // WHITE surface
Serial.println("The obstacle is present");
analogWrite(mot1left, 50);
analogWrite(mot2left, 255); //stop
analogWrite(mot1right, 50);
analogWrite(mot2right, 255);
}
void left(){
Serial.println("The obstacle is present");
analogWrite(mot1left, 0);
analogWrite(mot2left, 0); //stop
analogWrite(mot1right, 50);
analogWrite(mot2right, 255);
}
void right(){
Serial.println("The obstacle is present");
analogWrite(mot1left, 50);
analogWrite(mot2left, 255); //stop
analogWrite(mot1right, 0);
analogWrite(mot2right, 0);
}
void stopAndShoot(){
Serial.println("The obstacle is present");
analogWrite(mot1left, 0);
analogWrite(mot2left, 0); //stop
analogWrite(mot1right, 0);
analogWrite(mot2right, 0);
}
这是我的
#include "BluetoothSerial.h"
#include <ESP32Servo.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error
#endif
const int mot1right = 13;
const int mot2right = 12;
const int mot1left = 2;
const int mot2left = 15;
const int sensorleft = 16;
const int sensormid = 5;
const int sensorright = 34;
int servoPin1 = 26;
int servoPin2 = 27;
BluetoothSerial SerialBT;
Servo Servo1, Servo2;
bool automaticMode;
void setup() {
Serial.begin(115200);
SerialBT.begin("kete");
pinMode(mot1right, OUTPUT);
pinMode(mot2right, OUTPUT);
pinMode(mot1left, OUTPUT);
pinMode(mot2left, OUTPUT);
pinMode(sensorleft, INPUT);
pinMode(sensormid, INPUT);
pinMode(sensorright, INPUT);
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
}
void move() {
analogWrite(mot1left, 255);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 255);
Serial.println("moto jalan depan");
}
void berhenti() {
analogWrite(mot1left, 0);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 0);
}
void back() {
analogWrite(mot1left, 0);
analogWrite(mot2left, 255);
analogWrite(mot1right, 255);
analogWrite(mot2right, 0);
Serial.println("moto jalan blakang");
}
void kanan() {
analogWrite(mot1left, 0);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 200);
Serial.println("moto jalan kanan");
}
void kiri() {
analogWrite(mot1left, 200);
analogWrite(mot2left, 0);
analogWrite(mot1right, 0);
analogWrite(mot2right, 0);
Serial.println("moto jalan kiri");
}
void toggleMode(char command) {
if (command == 'X') {
automaticMode = true;
Serial.println("Automatic mode activated");
} else if (command == 'Y') {
automaticMode = false;
Serial.println("Remote control mode activated");
}
}
void loop() {
int stateL = digitalRead(sensorleft);
int stateM = digitalRead(sensormid);
int stateR = digitalRead(sensorright);
if (SerialBT.available()) {
char inputChar = SerialBT.read();
toggleMode(inputChar);
if (automaticMode==true) {
if ((stateM == LOW && stateL == HIGH && stateR == HIGH)){
move();
Serial.println("depan");
}
else if ((stateL == LOW && stateM == HIGH && stateR == HIGH)){
kiri();
Serial.println("kiri");
}
else if ((stateR == LOW && stateL == HIGH && stateM == HIGH)){
kanan();
Serial.println("kanan");
}
else if ((stateR == LOW && stateL == HIGH && stateM == LOW)){
kanan();
Serial.println("kanan");
}
else if ((stateR == HIGH && stateL == LOW && stateM == LOW)){
kiri();
Serial.println("kiri");
}
else if ((stateR == LOW && stateL == LOW && stateM == LOW)){
berhenti();
Serial.println("benti");
}
}
else{
switch (inputChar){
case 'F':
move ();
Serial.println(inputChar);
break;
case 'R':
kanan ();
Serial.println(inputChar);
break;
case 'L':
kiri ();
Serial.println(inputChar);
break;
case 'G':
back ();
Serial.println(inputChar);
break;
default:
berhenti();
break;
}
if (inputChar == 'J'){
Serial.println(inputChar);
int angle1 = SerialBT.parseInt();
Servo1.write(angle1);
}
if (inputChar == 'K'){
Serial.println(inputChar);
int angle2 = SerialBT.parseInt();
Servo2.write(angle2);
}
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(1);
}
}
我会解释这个问题。传感器和电机确实可以工作,但除非我按“X”,否则它会保持这种状态。“X”作为提示我在自动和远程控制代码之间切换,所以是的。
无论如何,我将给出上述问题的示例,当我按“X”并且中间传感器没有检测到光时,它会直线移动。即使条件发生变化,它也会保持笔直。例如,如果它已经直线移动,尽管传感器条件匹配,但它不会向左/向右移动。
尽管一周内有考试,但我已经为比赛工作了几天。我不知道该问谁。
提前致谢
答: 暂无答案
评论