Arduino IR传感器未调用函数

时间:2016-02-08 05:43:46

标签: arduino

使用在Arduino IDE中编写的代码,我曾经能够根据红外传感器检测到的对象(右,左或两者)来调用函数。我最近对代码进行了一些修改,但我还是继续确保它没有丢失任何使其首先工作的关键组件。我不确定它在我的程序或硬件中是否有错误但是现在,无论传感器检测到什么物体,我的机器人都会略微向左转。

//Include Servo Library
#include <Servo.h>

//Define Pins for the Sensors and LEDs
#define LsensorPin 8
#define RsensorPin 5
#define LeftTransPin 7
#define RightTransPin 6

我使用连续旋转伺服系统,因此使用0和180&是我如何全速控制它们。我一遍又一遍地查看这些数字,但它们似乎不是问题所在。

//Constants for driving servo
const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //0 Speed

//Name the Servos for Left and Right Motor
Servo LMotor;
Servo RMotor;

以下2个功能在调用时,通过发射器发送红外信号。我不认为这与问题有关,因为传感器仍能检测到物体,但我的机器人并没有按照我想要的方式做出反应。

//Take Reading from Left IR Sensor
void LeftIRled(){
  for(int i=0;i<=384;i++){
   digitalWrite(LeftTransPin, HIGH);
   delayMicroseconds(13);
   digitalWrite(LeftTransPin, LOW);
   delayMicroseconds(13);
 }
}

//Take Reading from Right IR Sensor
void RightIRled(){
  for(int i=0;i<=384;i++){
   digitalWrite(RightTransPin, HIGH);
   delayMicroseconds(13);
   digitalWrite(RightTransPin, LOW);
   delayMicroseconds(13);
 } 
}

这些功能控制机器人移动的方向。我已多次检查过,但这可能是其中一个问题。它们基本上只是用先前声明的变量控制每个伺服的旋转。

//Controlling Servo Directions with Functions
void Forward(){
 RMotor.write(RForward);
 LMotor.write(LForward);
}
void Backup(){
 RMotor.write(RBackward);
 LMotor.write(LBackward);
}
void Stop(){
 RMotor.write(RNeutral);
 LMotor.write(LNeutral);
}
void Left(){
 RMotor.write(RForward);
 LMotor.write(LNeutral);
}
void Right(){
 RMotor.write(RNeutral);
 LMotor.write(LForward);
}

//Declare INPUT and OUTPUT for each Pin connected
//to Arduino Uno and set them to LOW
void setup(){
 pinMode(LeftTransPin, OUTPUT);
 digitalWrite(LeftTransPin, LOW);
 pinMode(RightTransPin, OUTPUT);
 digitalWrite(RightTransPin, LOW);

//drive forward
 LMotor.attach(9);
 RMotor.attach(10);
 Forward();
 }

虽然我无法查明问题,但我认为它与循环功能有关。在这里,我发送红外信号,接收它,然后调用控制机器人运动的各种功能。

void loop(){

/*Drive forward until you reach an obstacle on either the 
right side, left side, or both together(something in front of
you).  If the left sensor goes low, he turns right, if the right
sensor goes low, he turns left.  if both turn on at the same time
he stops, backs up, and turns right till he is out of danger
*/
int lstate;
int rstate;

LeftIRled();
 lstate = digitalRead(LsensorPin);
 delay(50);
RightIRled();
 rstate = digitalRead(RsensorPin);
 delay(50);

if (lstate == LOW && rstate == LOW)
{
 Stop();
 delay(100);
 Backup();
 delay(1000);
 Stop();
 delay(100);
 Right();
 delay(500);
}
else if (lstate == LOW)
{
 Stop();
 delay(100);
 Backup();
 delay(1000);
 Right();
 delay(500);
}
else if (rstate == LOW)
{  
 Stop();
 delay(100);
 Backup();
 delay(1000);        
 Left();
 delay(500);
}
else
{
 Forward();
  }
}

非常感谢任何帮助或建议!

0 个答案:

没有答案