Evening guys and gals ![]()
I've been working on a project recently which uses Steve Hobley's Pixart IR Tracking program to control a pan servo to follow an IR blob (code below) However, I would like to be able to use manual controls as well via an analogue joystick (code below). The joystick has a push button, so I was wondering if I could jump between the tracking bit of the code and the manual control bit of the code by pressing and releasing the momentary switch?
Tracking code:
// Sweep
// by BARRAGAN http://barraganstudio.com
#include <Servo.h>
#include <Wire.h>
#include <PVision.h>
Servo myservo2; // create servo object to control a servo
// a maximum of eight servo objects can be created
PVision ircam;
byte result;
int ledPin = 11; // LED connected to digital pin 13
int pos = 0; // variable to store the servo position
void setup()
{
myservo2.attach(6); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
ircam.init();
}
void loop()
{
result = ircam.read();
if (result & BLOB1)
{
Serial.print("BLOB1 detected. X:");
Serial.print(ircam.Blob1.X);
Serial.print(" Y:");
Serial.print(ircam.Blob1.Y);
Serial.print(" Size:");
Serial.println(ircam.Blob1.Size);
pos = ircam.Blob1.X / 5;
myservo2.write(pos);
}
if (result & BLOB2)
{
digitalWrite(ledPin, HIGH); // sets the LED on
Serial.print("BLOB2 detected. X:");
Serial.print(ircam.Blob2.X);
Serial.print(" Y:");
Serial.print(ircam.Blob2.Y);
Serial.print(" Size:");
Serial.println(ircam.Blob2.Size);
}
if (result & BLOB3)
{
Serial.print("BLOB3 detected. X:");
Serial.print(ircam.Blob3.X);
Serial.print(" Y:");
Serial.print(ircam.Blob3.Y);
Serial.print(" Size:");
Serial.println(ircam.Blob3.Size);
}
if (result & BLOB4)
{
Serial.print("BLOB4 detected. X:");
Serial.print(ircam.Blob4.X);
Serial.print(" Y:");
Serial.print(ircam.Blob4.Y);
Serial.print(" Size:");
Serial.println(ircam.Blob4.Size);
}
digitalWrite(ledPin, LOW); // sets the LED on
}
Manual controls:
#include <Servo.h>
Servo panServo;
Servo tiltServo;
int servoPanPosition = 90;
int servoTiltPosition = 90;
int joystickPanPin = A1;
int joystickTiltPin = A0;
int joystickButton = 2;
int joystickPanSpeed = 0;
int joystickTiltSpeed = 0;
int servoPanPin = 6;
int servoTiltPin = 5;
int orangeLED = 10;
int greenLED = 11;
int brightness = 0;
int fadeAmount = 5;
int checkMovement = 0;
void setup()
{
Serial.begin(9600);
pinMode(servoPanPin, OUTPUT);
pinMode(servoTiltPin, OUTPUT);
panServo.attach(servoPanPin);
tiltServo.attach(servoTiltPin);
pinMode(orangeLED, OUTPUT);
pinMode(greenLED, OUTPUT);
}
void loop()
{
joystickPanSpeed = (analogRead(joystickPanPin) - 512) / 150;
joystickTiltSpeed = (analogRead(joystickTiltPin) - 512) / 150;
servoPanPosition = constrain((servoPanPosition + joystickPanSpeed), 1, 170);
servoTiltPosition = constrain((servoTiltPosition + joystickTiltSpeed), 63, 130);
panServo.write(servoPanPosition);
tiltServo.write(servoTiltPosition);
checkMovement = (joystickPanSpeed + joystickTiltSpeed);
if (checkMovement != 0) {
digitalWrite(greenLED, HIGH);
}
checkMovement = (joystickPanSpeed + joystickTiltSpeed);
if (checkMovement = 0) {
digitalWrite(greenLED, LOW);
}
//analogWrite(greenLED, brightness);
//brightness = joystickPanSpeed;
//if (brightness>0) {
//brightness = -joystickPanSpeed ;
//}
digitalWrite(orangeLED, HIGH);
Serial.print(" greenLED "); Serial.print( int(brightness));
Serial.print(" servoTilt "); Serial.print( int(servoTiltPosition ));
Serial.print(" checkMovement "); Serial.print( int(checkMovement ));
Serial.println("");
delay(10);
}