Main_working2.ino 2.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
const int trig_pin_left = 9;
const int echo_pin_left = 8;

const int trig_pin_mid = 7;
const int echo_pin_mid= 6;

const int trig_pin_right = 5;
const int echo_pin_right = 4;

10
11
12
13
14
15
16
struct Ultrasoundreads{
  long data1, data2, data3
} ultrasound;

Ultrasoundreads data[20];
int index = 0;

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
void setup() {
  // put your setup code here, to run once:
  // add reading of specific pins
  pinMode(trig_pin_left, OUTPUT); 
  pinMode(echo_pin_left, INPUT); 
  
  pinMode(trig_pin_mid, OUTPUT); 
  pinMode(echo_pin_mid, INPUT); 
  
  pinMode(trig_pin_right, OUTPUT); 
  pinMode(echo_pin_right, INPUT); 
  
  Serial.begin(9600); // Starts the serial communication
}

long calculate_distance(long duration) {
  return duration / 58.2; 
}

long read_pin(int trig_pin, int echo_pin, int start_microseconds, int end_microseconds) {
  digitalWrite(trig_pin, LOW);
  delayMicroseconds(start_microseconds);

  digitalWrite(trig_pin, HIGH);
  
  delayMicroseconds(end_microseconds);
  digitalWrite(trig_pin, LOW);

  return pulseIn(echo_pin, HIGH);
}

void read_inputs(long * datapoints) {
  long duration_left = read_pin(trig_pin_left, echo_pin_left, 2, 10);
  datapoints[0] = calculate_distance(duration_left);
  
  long duration_mid = read_pin(trig_pin_mid, echo_pin_mid, 2, 10);
  datapoints[1] = calculate_distance(duration_mid);

  long duration_right = read_pin(trig_pin_right, echo_pin_right, 2, 10);
  datapoints[2] = calculate_distance(duration_right);
57
58
59
60
61
62
  ultrasound.data1 = datapoints[0];
  ultrasound.data2 = datapoints[1];
  ultrasound.data3 = datapoints[2];
  data[(index++)%20] = ultrasound;


63
64
}

65
66
67
void print_sensors(long * reads) {
  Serial.print("Sensor data: ");
  Serial.print(reads[0]);
68
  Serial.print(" ");
69
  Serial.print(reads[1]);
70
  Serial.print(" ");
71
  Serial.print(reads[2]);
72
73
74
75
76
77
78
79
80
81
82
83
  Serial.println();
}

long average(long a, long b) {
  return (a + b) / 2;
}

bool check_against(long a, long b, long comp) {
  long avg = average(a, b);
  return abs(avg - comp) > 30;
}

84
85
86
87
88
89
90
91
92
93
double currspeed(){
  
  long dist1 = (data[19].data1-data[0].data1);
  long dist2 = (data[19].data2-data[0].data2);
  long dist3 = (data[19].data3-data[0].data3);

  double speed = ((double) (dist1+dist2+dist3)*2)/((double) 3);
  return speed;
}

94
95

void loop() {
96
97
98
99
100
101
102
103
104
105
106
107
  long reads[3];
  read_inputs(reads);
  print_sensors(reads);
  bool check_right = check_against(reads[0], reads[1], reads[2]);
  bool check_left = check_against(reads[1], reads[2], reads[0]);
  bool check_mid = check_against(reads[0], reads[2], reads[1]);

  double time = ((double)reads[1])/currspeed();
  //time > 3 <- value can be set
  if ((check_right || check_left || check_mid) && time>3) {


108
109
110
    Serial.println("There seems to be an object in the way...");
  }
}