-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.ino
142 lines (119 loc) · 3.51 KB
/
Robot.ino
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
float timeStep = 0.01;
float yaw = 0;
int out = 0;// these variables to avoid unwanted signals
int data;
int i;
int distance =0;
volatile signed long counter1 = 0; //This variable will increase or decrease depending on the rotation of encoder
volatile signed long counter2 = 0; //This variable will increase or decrease depending on the rotation of encoder
int IR1=4,IR2=5,IR3=6;// IR pins
// connect Encoder 1 to 2,3 pins ,, encoder 2 to 20,21 pins ,, metal detector to 8 pin
void setup() {
Serial.begin(9600);
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
mpu.calibrateGyro();
mpu.setThreshold(5);
pinMode(8,INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(2, INPUT); // set pin to input
pinMode(3, INPUT); // set pin to input// encoder number 1
pinMode(21, INPUT); // set pin to input
pinMode(20, INPUT); // set pin to input// encoder number 2
digitalWrite(2, HIGH); // turn on pullup resistors
digitalWrite(3, HIGH); // turn on pullup resistors
digitalWrite(21, HIGH); // turn on pullup resistors
digitalWrite(20, HIGH); // turn on pullup resistors
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
attachInterrupt(2, ai2, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
attachInterrupt(3, ai3, RISING);
}
void loop() {
Vector norm = mpu.readNormalizeGyro();
yaw = yaw + norm.ZAxis * timeStep;
Serial.println(yaw);
int x = detectorVoid(8);
Serial.print ("Metal detector value =");
Serial.println(x);
distance=0.13083333333*counter1/2;
Serial.print ("counter1:");
Serial.print (counter1);
Serial.print ("-----------");
Serial.print ("counter2:");
Serial.println (counter2);
int irVal1= digitalRead(IR1);
int irVal2= digitalRead(IR2);
int irVal3= digitalRead(IR3);
Serial.print ("IR Values");
Serial.print (irVal1);
Serial.print ("---");
Serial.print (irVal2);
Serial.print ("---");
Serial.println (irVal3);
}
int detectorVoid(int pin){
data = digitalRead(pin);
// put your main code here, to run repeatedly:
if ( data == 1){
for (i=0;i<200;i++)
{delay(1);
data=digitalRead(pin);
if(data==0)
break;}
if(i==200){
out = 1;}
}
else
{
out = 0;
}
return(out);
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
counter1++;
}else{
counter1--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
counter1--;
}else{
counter1++;
}
}
void ai2() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(20)==LOW) {
counter2++;
}else{
counter2--;
}
}
void ai3() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(21)==LOW) {
counter2--;
}else{
counter2++;
}
}