Code: Select all
#include <Adafruit_MotorShield.h>
#include <Wire.h>
const int pingPin = 12;
unsigned int duration, inches;
int Stay = 11;
char s = 0;
boolean lastState = false;
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
Adafruit_DCMotor *LM = AFMS.getMotor(1);
Adafruit_DCMotor *RM = AFMS.getMotor(2);
/*
IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.
*/
struct InfraredResult
{
byte Direction;
byte Strength;
};
class InfraredSeeker
{
public:
static void Initialize();
static boolean Test();
static void ReadACRaw(byte* buffer);
static void ReadDCRaw(byte* buffer);
static InfraredResult ReadAC();
static InfraredResult ReadDC();
static int DirectionAngle(byte Direction);
private:
static InfraredResult PopulateValues(byte* buffer);
static void ReadValues(byte OffsetAddress, byte* buffer);
static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};
void InfraredSeeker::Initialize()
{
Wire.begin();
Wire.beginTransmission(InfraredSeeker::Address);
Wire.write(0x00);
Wire.endTransmission();
while(Wire.available() > 0)
Wire.read();
}
boolean InfraredSeeker::Test()
{
Wire.beginTransmission(InfraredSeeker::Address);
Wire.write(0x08);
Wire.endTransmission();
Wire.requestFrom(InfraredSeeker::Address, 16);
char Manufacturer_Model[16];
while(Wire.available() < 16);
for(byte i=0; i < 16; i++)
{
Manufacturer_Model[i] = Wire.read();
}
while(Wire.available() > 0)
Wire.read();
return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}
void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
Wire.beginTransmission(InfraredSeeker::Address);
Wire.write(OffsetAddress);
Wire.endTransmission();
Wire.requestFrom(InfraredSeeker::Address, 6);
while(Wire.available() < 6);
for(byte i = 0; i < 6; i++)
{
buffer[i] = Wire.read();
}
while(Wire.available() > 0)
Wire.read();
}
void InfraredSeeker::ReadACRaw(byte* buffer)
{
ReadValues(0x49, buffer);
}
void InfraredSeeker::ReadDCRaw(byte* buffer)
{
ReadValues(0x42, buffer);
}
InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
InfraredResult Data;
Data.Direction = buffer[0];
if(buffer[0] != 0)
{
if(buffer[0] % 2 == 0)
{
Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
}
else
{
Data.Strength = buffer[buffer[0] / 2 + 1];
}
}
else
{
Data.Strength = 0;
}
return Data;
}
InfraredResult InfraredSeeker::ReadAC()
{
byte buffer[6];
ReadACRaw(buffer);
return PopulateValues(buffer);
}
InfraredResult InfraredSeeker::ReadDC()
{
byte buffer[6];
ReadDCRaw(buffer);
return PopulateValues(buffer);
}
int DirectionAngle(byte Direction)
{
return Direction * 30 - 150;
}
void setup()
{
pinMode(Stay,INPUT);
Serial.begin(9600);
Serial.println("HiTechnic IRSeeker V2");
Serial.println();
Serial.println();
Serial.println("Dir\tAngle\tStrength");
Serial.println();
InfraredSeeker::Initialize();
AFMS.begin();
}
void loop()
{
InfraredResult InfraredBall = InfraredSeeker::ReadAC();
Serial.print(InfraredBall.Direction);
Serial.print("\t");
Serial.print(DirectionAngle(InfraredBall.Direction));
Serial.print("\t");
Serial.print(InfraredBall.Strength);
Serial.println();
delay(200); //optional
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = duration / 74 / 2;
Serial.println(inches);
delay(200);
boolean state = digitalRead(Stay) ;
if( state != lastState )
{
if( state == HIGH )
s = !s;
lastState = state;
}
if (s == 1)
{
LM->run(RELEASE);
RM->run(RELEASE);
}
if (s == 0)
{
if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5)
{
LM->run(BACKWARD);
LM->setSpeed(255);
RM->run(FORWARD);
RM->setSpeed(255);
}
if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9)
{
LM->run(FORWARD);
LM->setSpeed(255);
RM->run(BACKWARD);
RM->setSpeed(255);
}
if (InfraredBall.Direction == 5 && inches > 50)
{
LM->run(FORWARD);
LM->setSpeed(255);
RM->run(FORWARD);
RM->setSpeed(255);
}
if (InfraredBall.Direction == 5 && inches < 9)
{
LM->run(BACKWARD);
LM->setSpeed(255);
RM->run(BACKWARD);
RM->setSpeed(255);
}
if (InfraredBall.Direction == 5 && inches <= 50 && inches >= 9)
{
LM->run(RELEASE);
RM->run(RELEASE);
}
if (InfraredBall.Direction == 0)
{
LM->run(RELEASE);
RM->run(RELEASE);
}
}
}
Code: Select all
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2009 Joerg Wunsch
System wide configuration file is "C:\Users\Colin\Desktop\Arduino\hardware/tools/avr/etc/avrdude.conf"
Using Port : \\.\COM10
Using Programmer : arduino
Overriding Baud Rate : 115200
avrdude: Send: 0 [30] [20]
avrdude: Send: 0 [30] [20]
avrdude: Send: 0 [30] [20]
avrdude: Recv: . [00]
avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude done. Thank you.