-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
67 lines (64 loc) · 2.1 KB
/
Copy pathcode
File metadata and controls
67 lines (64 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <Servo.h> // Library for servos
#include <Pixy2.h> // Libraries for PIXYCAM
#include <SPI.h>
Pixy2 pixy; // This is the main Pixy object
Servo myservo1; // Servo for RED
Servo myservo2; // Servo for BLUE
const int trigPin = 5;
const int echoPin = 6;
long duration;
int distance;
int pos = 0; //stores servo position
void setup(){
myservo1.attach(9); // Attach servo for RED
myservo1.write(180);
myservo2.attach(3); // Attach servo for BLUE
myservo2.write(180);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
pixy.init(); // Initiate PIXYCAM
}
void loop(){
int i;
pixy.ccc.getBlocks(); // grab blocks!
if (pixy.ccc.numBlocks)// If there are detect blocks, print them!
{
Serial.print("Detected ");
Serial.println(pixy.ccc.numBlocks);
for (i=0; i<pixy.ccc.numBlocks; i++)
digitalWrite(trigPin, LOW); // Clears the trigPin
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; // Converts distance
{
Serial.print(" block ");
Serial.print(i);
Serial.print(": ");
pixy.ccc.blocks[i].print(); // Prints PIXYCAM values
Serial.print("Distance: "); // Prints the distance on the Serial Monitor
Serial.println(distance);
}
if (uint16_t m_signature == 2 && (distance < 25)){ // if color signature is RED and distance is less than the value denoted
{
delay(1000);
myservo1.write(0);
delay(2500);
myservo1.write(90);
delay(500);
}
}
if (uint16_t m_signature == 1 && (distance < 25)){ // if color signature is BLUE and distance is less than the value denoted
{
delay(1000);
myservo2.write(0);
delay(2500);
myservo2.write(90);
delay(500);
}
}
}
}