我正在创建操作机器人的代码。它应该告诉机器人何时转动,这取决于它从传感器获得的读数。我尝试了一个if语句来转动它,并且对机器人如何转动感到不满意。我感觉有一个while循环更好,因为它不必经过整个代码来继续检查它是否应该继续转动,并且将保持while循环直到转弯完成。我遇到的问题是代码不会从传感器中读取读数并直接进入while循环并保持在那里。我该如何解决这个问题?
>#include<SoftwareSerial.h> // Import the serial Library
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *FL= AFMS.getMotor(1); //Front left motor
Adafruit_DCMotor *FR= AFMS.getMotor(4); //Front right motor
int left_trig = 8;
int left_echo = 9;
int mid_trig = 5;
int mid_echo = 6;
int right_trig = 3;
int right_echo = 4;
long duration1, duration2, duration3, inches1, inches2, inches3;
void setup() {
Serial.begin(9600);
AFMS.begin(); // create with the default frequency 1.6KHz
//This establishes the sensors as inputs and outputs
pinMode(left_trig,OUTPUT);
pinMode(left_echo,INPUT);
pinMode (mid_trig,OUTPUT);
pinMode(mid_echo,INPUT);
pinMode (right_trig,OUTPUT);
pinMode(right_echo,INPUT);
FL->setSpeed(150);
FL->run(FORWARD);
//FL->run(RELEASE);
FR->setSpeed(150);
FR->run(BACKWARD);
//FR->run(RELEASE);
}
void loop() {
digitalWrite(left_trig, LOW);
delayMicroseconds(2);
digitalWrite(left_trig, HIGH);
delayMicroseconds(10);
duration1 = pulseIn(left_echo,HIGH);
// pinMode (mid_trig,OUTPUT);
digitalWrite(mid_trig, LOW);
delayMicroseconds(2);
digitalWrite(mid_trig, HIGH);
delayMicroseconds(10);
duration2 = pulseIn(mid_echo, HIGH);
// pinMode (right_trig,OUTPUT);
digitalWrite(right_trig, LOW);
delayMicroseconds(2);
digitalWrite(right_trig, HIGH);
delayMicroseconds(10);
duration3 = pulseIn(right_echo, HIGH);
// convert the time into inches
inches1 = microsecondsToInches(duration1);
inches2 = microsecondsToInches(duration2);
inches3 = microsecondsToInches(duration3);
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(BACKWARD);
Serial.print(inches1);
Serial.print("in,\t");
Serial.print(inches2);
Serial.print("in,\t");
Serial.print(inches3);
Serial.print("in");
Serial.println();
while(inches3 <=8 && inches2 <=12){
// Serial.print(inches1);
// Serial.print("win,\t");
// Serial.print(inches2);
// Serial.print("win,\t");
// Serial.print(inches3);
// Serial.print("win");
// Serial.println();
FL->setSpeed(120);
FL->run(BACKWARD);
FR->setSpeed(120);
FR->run(BACKWARD);
}
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(BACKWARD);
delay(50);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
答案 0 :(得分:1)
while循环中的任何内容都不会更改inches3或inches2的值。如果它们分别小于8和12来进入while循环,那么每次它回来检查时它们仍然是那个。所以这是一个无限循环。
答案 1 :(得分:0)
由于在while循环中永远不会更新该值,您应该能够添加检查while循环内传感器的代码,如下所示:
#include<SoftwareSerial.h> // Import the serial Library
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *FL= AFMS.getMotor(1); //Front left motor
Adafruit_DCMotor *FR= AFMS.getMotor(4); //Front right motor
int left_trig = 8;
int left_echo = 9;
int mid_trig = 5;
int mid_echo = 6;
int right_trig = 3;
int right_echo = 4;
long duration1, duration2, duration3, inches1, inches2, inches3;
void setup()
{
Serial.begin(9600);
AFMS.begin(); // create with the default frequency 1.6KHz
//This establishes the sensors as inputs and outputs
pinMode(left_trig,OUTPUT);
pinMode(left_echo,INPUT);
pinMode (mid_trig,OUTPUT);
pinMode(mid_echo,INPUT);
pinMode (right_trig,OUTPUT);
pinMode(right_echo,INPUT);
FL->setSpeed(150);
FL->run(FORWARD);
//FL->run(RELEASE);
FR->setSpeed(150);
FR->run(BACKWARD);
//FR->run(RELEASE);
}
void loop()
{
digitalWrite(left_trig, LOW);
delayMicroseconds(2);
digitalWrite(left_trig, HIGH);
delayMicroseconds(10);
duration1 = pulseIn(left_echo,HIGH);
// pinMode (mid_trig,OUTPUT);
digitalWrite(mid_trig, LOW);
delayMicroseconds(2);
digitalWrite(mid_trig, HIGH);
delayMicroseconds(10);
duration2 = pulseIn(mid_echo, HIGH);
// pinMode (right_trig,OUTPUT);
digitalWrite(right_trig, LOW);
delayMicroseconds(2);
digitalWrite(right_trig, HIGH);
delayMicroseconds(10);
duration3 = pulseIn(right_echo, HIGH);
// convert the time into inches
inches1 = microsecondsToInches(duration1);
inches2 = microsecondsToInches(duration2);
inches3 = microsecondsToInches(duration3);
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(BACKWARD);
Serial.print(inches1);
Serial.print("in,\t");
Serial.print(inches2);
Serial.print("in,\t");
Serial.print(inches3);
Serial.print("in");
Serial.println();
while(inches3 <=8 && inches2 <=12)
{
// convert the time into inches
inches1 = microsecondsToInches(duration1);
inches2 = microsecondsToInches(duration2);
inches3 = microsecondsToInches(duration3);
// Serial.print(inches1);
// Serial.print("win,\t");
// Serial.print(inches2);
// Serial.print("win,\t");
// Serial.print(inches3);
// Serial.print("win");
// Serial.println();
FL->setSpeed(120);
FL->run(BACKWARD);
FR->setSpeed(120);
FR->run(BACKWARD);
}
FL->setSpeed(150);
FL->run(FORWARD);
FR->setSpeed(150);
FR->run(BACKWARD);
delay(50);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}