Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
arduino机器人的While循环问题_Arduino_Arduino Ide - Fatal编程技术网

arduino机器人的While循环问题

arduino机器人的While循环问题,arduino,arduino-ide,Arduino,Arduino Ide,我正在创建操作机器人的代码。它应该根据传感器的读数来告诉机器人何时转向。我尝试了一个if语句来旋转它,但对机器人的旋转方式不满意。我觉得while循环更好,因为它不必通过整个代码来检查是否应该继续旋转,并且会一直保持while循环直到旋转完成。我遇到的问题是,代码不会从传感器读取读数,而是直接进入while循环并停留在那里。我如何解决这个问题 >#include<SoftwareSerial.h> // Import the serial Library #include &l

我正在创建操作机器人的代码。它应该根据传感器的读数来告诉机器人何时转向。我尝试了一个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;
}
#包括//导入串行库
#包括
#包括
#包括“公用事业/Adafruit_MS_PWMServoDriver.h”
//使用默认I2C地址创建电机屏蔽对象
Adafruit_MotorShield AFMS=Adafruit_MotorShield();
//选择哪个“端口”M1、M2、M3或M4。在这种情况下,M1
Adafruit_DCMotor*FL=AFMS.getMotor(1)//左前电机
Adafruit_DCMotor*FR=AFMS.getMotor(4)//右前马达
int左触发=8;
int left_echo=9;
int mid_trig=5;
int mid_echo=6;
int right_trig=3;
int-right_-echo=4;
长持续时间1,持续时间2,持续时间3,英寸1,英寸2,英寸3;
无效设置(){
Serial.begin(9600);
AFMS.begin();//使用默认频率1.6KHz创建
//这将传感器建立为输入和输出
pinMode(左触发,输出);
pinMode(左回波,输入);
pinMode(中间触发,输出);
pinMode(中置回波,输入);
pinMode(右触发,输出);
pinMode(右回波,输入);
FL->设定速度(150);
FL->运行(前进);
//FL->运行(释放);
FR->设定速度(150);
FR->运行(向后);
//FR->运行(释放);
}
void循环(){
数字写入(左触发,低电平);
延迟微秒(2);
数字写入(左触发,高);
延迟微秒(10);
持续时间1=脉冲信号(左回波,高);
//pinMode(中间触发,输出);
数字写入(中触发,低触发);
延迟微秒(2);
数字写入(中触发,高);
延迟微秒(10);
持续时间2=脉冲强度(中等回声,高);
//pinMode(右触发,输出);
数字写入(右触发,低电平);
延迟微秒(2);
数字写入(右触发,高);
延迟微秒(10);
持续时间3=脉冲信号(右回波,高);
//把时间换算成英寸
英寸1=微秒(持续时间1);
英寸2=微秒(持续时间2);
英寸3=微秒(持续时间3);
FL->设定速度(150);
FL->运行(前进);
FR->设定速度(150);
FR->运行(向后);
串行打印(英寸1);
串行打印(“in,\t”);
串行打印(英寸2);
串行打印(“in,\t”);
串行打印(英寸3);
序列号。打印(“in”);
Serial.println();
while(英寸)3运行(向后);
FR->设定速度(120);
FR->运行(向后);
}
FL->设定速度(150);
FL->运行(前进);
FR->设定速度(150);
FR->运行(向后);
延迟(50);
}
长微秒音程(长微秒){
返回微秒/74/2;
}

while循环中的任何内容都不会改变inches3或inches2的值。如果它们分别小于8和12,则每次返回检查时,它们仍将是该值。因此这是一个无限循环。

由于while循环中的值从未更新,因此您应该能够e添加检查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;
}
#包括//导入串行库
#包括
#包括
#包括“公用事业/Adafruit_MS_PWMServoDriver.h”
//使用默认I2C地址创建电机屏蔽对象
Adafruit_MotorShield AFMS=Adafruit_MotorShield();
//选择哪个“端口”M1、M2、M3或M4。在本例中为M1
Adafruit_DCMotor*FL=AFMS.getMotor(1);//左前电机
Adafruit_DCMotor*FR=AFMS.getMotor(4);//右前电机
int左触发=8;
int left_echo=9;
int mid_trig=5;
int mid_echo=6;
int right_trig=3;
int-right_-echo=4;
长持续时间1,持续时间2,持续时间3,英寸1,英寸2,英寸3;
无效设置()
{
Serial.begin(9600);
AFMS.begin();//使用默认频率1.6KHz创建
//这将传感器建立为输入和输出
pinMode(左触发,输出);
pinMode(左回波,输入);
pinMode(中间触发,输出);
pinMode(中置回波,输入);
pinMode(右触发,输出);
pinMode(右回波,输入);
FL->设定速度(150);
FL->运行(前进);
//FL->运行(释放);
FR->设定速度(150);
FR->运行(向后);
//FR->运行(释放);
}
void循环()
{
数字写入(左触发,低电平);
延迟微秒(2);
数字写入(左触发,高);
延迟微秒(10);
持续时间1=脉冲信号(左回波,高);
//pinMode(中间触发,输出);
数字写入(中触发,低触发);
延迟微秒(2);
数字写入(中触发,高);
延迟微秒(10);
持续时间2=脉冲强度(中等回声,高);
//pinMode(右触发,输出);
数字写入(右触发,低电平);
延迟微秒(2);
数字写入(右触发,高);
延迟微秒(10);
持续时间3=脉冲信号(右回波,高);
//把时间换算成英寸
英寸1=微秒(持续时间1);
英寸2=微秒(持续时间2);
英寸3=微秒(持续时间3);
FL->设定速度(150);
FL->运行(前进);
FR->设定速度(150);
FR->运行(向后);
串行打印(英寸1);
串行打印(“in,\t”);
串行打印(英寸2);
串行打印(“in,\t”);
串行打印(英寸3);
序列号。打印(“in”);
Serial.println();
while(英寸)3运行(向后);
FR->设定速度(120);
FR->运行(向后);
}
FL->设定速度(150);
FL->运行(前进);
FR->设定速度(150);
FR->运行(向后);
延迟(50);
}
长微秒音程(长微秒)
{
返回微秒/74/2;
}

谢谢,我回头看后得出了相同的结论,这就是问题所在。转向仍然不平稳。我担心可能是我正在使用的电机,但我希望这是代码,因为这将是一个更容易解决的问题。仔细看,确实看到了任何可以更好的地方