ULTRASONIC (KEEP DISTANCE BETWEEN MASTER AND SLAVE AT CERTAIN RANGE)
BELOW IS THE CODE I COMBINED WITH PIXY AND ULTRASONIC SENSOR CODE:
while(millis()<5000){
pixycamera();
area = width * height; //calculate the object area
maxArea = area + 1000;
minArea = area - 1000;
}
pixycamera();
//looking for signature 1****//
if(signature == 1)
{
//calculate the object area****//
newarea = width * height;
//turn left if x position < max x position****//
if (x < Xmin)
{
Left();
}
//turn right if x position > max x position****//
else if (x > Xmax)
{
Right();
}
//go forward if object too small****//
else if(newarea < minArea)
{
if (distance >= 15) {
Forward();
Serial.print("Move forward");
}
if (distance <= 15 && distance > 10) {
Forward();
Serial.print("Move Forward");
}
//Forward();
}
//go backward if object too big****//
else if(newarea > maxArea)
{
if (distance <= 5 && distance > 3) {
Backward();
Serial.print("Move Backward");
}
//Backward();
}
#include <AFMotor.h>
#include <SPI.h>
#include <Pixy.h>
Pixy pixy;
//**********************PIXYCAMERA-PIXYMON********************//
int signature = 0;
int x = 0; //positon x axis
int y = 0; //position y axis
unsigned int width = 0; //object's width (UNSIGNED INT : NO -VE VALUE : 0-65535)
unsigned int height = 0; //object's height
unsigned int area = 0;
unsigned int newarea = 0;
int Xmin = 70; //min x position
int Xmax = 200; //max x position
int maxArea = 0;
int minArea = 0;
static int i = 0;
//**************DEFINES SENSOR PINS NUMBERS********************//
const int trigPin = 38; //10;
const int echoPin = 36; //9;
//**************DEFINES MOTOR PORT********************//
AF_DCMotor motor1(1); // create motor #1, 8KHz pwm
AF_DCMotor motor2(2); // create motor #2, 8KHz pwm
AF_DCMotor motor3(3); // create motor #3, 8KHz pwm
AF_DCMotor motor4(4); // create motor #4, 8KHz pwm
//**************UNIVERSAL DEFINES VARIABLES********************//
long duration;
int distance;
//****************PROGRAM OR CODE RUN ONCE********************//
void setup() {
Serial.begin(9600); //Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test with Ultrasonic Sensor!");
motorspeed();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Stop();
pixy.init();
}
//****************PROGRAM OR CODE RUN REPEATEDLY**************//
void loop() {
//*************ULTRASONIC SENSOR MEASURE DISTANCE BETWEEN MASTER AND SLAVE*************//
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
//Prints the distance on the Serial Monitor
Serial.print("Initial Distance(cm): ");
Serial.println(distance);
//*************MOTOR WILL MOVE ACCORDING PIXY*************//
while(millis()<5000){
pixycamera();
area = width * height; //calculate the object area
maxArea = area + 1000;
minArea = area - 1000;
}
pixycamera();
//**********looking for signature 1**************//
if(signature == 1)
{
//**********calculate the object area**************//
newarea = width * height;
//**********turn left if x position < max x position**************//
if (x < Xmin)
{
Left();
}
//**********turn right if x position > max x position**************//
else if (x > Xmax)
{
Right();
}
//**********go forward if object too small**************//
else if(newarea < minArea)
{
if (distance >= 15) {
Forward();
Serial.print("Move forward");
}
if (distance <= 15 && distance > 10) {
Forward();
Serial.print("Move Forward");
}
//Forward();
}
//**********go backward if object too big**************//
else if(newarea > maxArea)
{
if (distance <= 5 && distance > 3) {
Backward();
Serial.print("Move Backward");
}
//Backward();
}
//**********else stop**************//
else
{
if (distance <= 3){
Stop();
Serial.print("Stop");
}
}
}
else
{
Stop();
}
}
//*************MOTOR FUNCTION (FORWARD, BACKWARD, RIGHT OR LEFT)*************//
void motorspeed(){
motor1.setSpeed(100); // set the speed to 100/255
motor2.setSpeed(100); // set the speed to 100/255
motor3.setSpeed(100); // set the speed to 100/255
motor4.setSpeed(100); // set the speed to 100/255
}
void Forward(){
motorspeed();
motor1.run(FORWARD); // turn it on going forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void Backward(){
motorspeed();
motor1.run(BACKWARD); // turn it on going backward
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void Stop(){
motorspeed();
motor1.run(RELEASE); // stop
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Right(){
motorspeed();
motor1.run(FORWARD); // turn it on going forward
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
void Left(){
motorspeed();
motor1.run(BACKWARD); // turn it on going forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
//******************PIXYCAMERA FUNCTION: DETECT THE WIDHT, HEIGHT,X-Y POSITION, BLOCKS OR OBJECTS****************//
void pixycamera(){
uint16_t blocks;
blocks = pixy.getBlocks(); //receive data from pixy
signature = pixy.blocks[i].signature; //get object's signature
x = pixy.blocks[i].x; //get x position
y = pixy.blocks[i].y; //get y position
width = pixy.blocks[i].width; //get width
height = pixy.blocks[i].height; //get height
}
while(millis()<5000){
pixycamera();
area = width * height; //calculate the object area
maxArea = area + 1000;
minArea = area - 1000;
}
This will only work during the first five seconds from switch on after that it will never be run again. Is this what you want?
Read up on how to use the millis timer. There may well be other problems with your code as well.
Grumpy_Mike:
This will only work during the first five seconds from switch on after that it will never be run again. Is this what you want?
Read up on how to use the millis timer. There may well be other problems with your code as well.
Hi,
I don't think that part cause a problem.
The Pixycam work well and can detect the object colour every 5 seconds.
The one that not function when I'm putting the ultrasonic sonic into the pixy part and the sensor do not keep the distance between the master and slave.