-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrasonic_sensor_and_button.ino
153 lines (97 loc) · 2.33 KB
/
ultrasonic_sensor_and_button.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 <Servo.h>
Servo headMotor;
Servo rightMotor;
Servo leftMotor;
/*
Morse Code Key:
dot = LED on for 100 ms, then LED off for 100 ms
dash = LED on for 300 ms, then LED off for 100 ms
End of Letter = LED off for 1000 ms
End of Word = LED off for 2000 ms
Challenges:
1) Move head to using the button
2) Check arms are working too
3) Calibrate our robot (arms straight out, head looking forward)
4) Move arms and head when you press the button
5) Combine the lights, buzzer and motors using functions
6) Make your robot dance however you want
*/
int ultrasonic(int trig_pin, int echo_pin) {
long time;
float distance;
//-trigger a sound
// send out trigger signal
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(20);
digitalWrite(trig_pin, LOW);
//- a sound has gone out!!
//- wait for a sound to come back
time = pulseIn(echo_pin, HIGH);
//- calculate the distance in centimeters
distance = 0.01715 * time;
return distance;
}
void setup() {
pinMode(8,INPUT_PULLUP);
pinMode(3,OUTPUT); //trigger
pinMode(4,INPUT); //echo
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(13,OUTPUT);
pinMode(9,OUTPUT); //- head
pinMode(10,OUTPUT); //- right arm
pinMode(11,OUTPUT); //- left arm
headMotor.attach(9);
rightMotor.attach(10);
leftMotor.attach(11);
headMotor.write(90);
rightMotor.write(90);
leftMotor.write(90);
Serial.begin(9600);
}
void on() {
digitalWrite(7,HIGH);
digitalWrite(13,HIGH);
tone(6,261);
delay(100);
tone(6,523);
delay(100);
}
void off() {
digitalWrite(7,LOW);
digitalWrite(13,LOW);
noTone(6);
}
void loop() {
Serial.println(ultrasonic(3,4));
delay(100);
if (ultrasonic(3,4) < 5) {
on();
delay(1000);
rightMotor.write(180);
leftMotor.write(0);
headMotor.write(180);
delay(2000);
rightMotor.write(0);
leftMotor.write(180);
headMotor.write(90);
delay(2000);
}
else if (digitalRead(8) == LOW) {
on();
delay(1000);
rightMotor.write(180);
leftMotor.write(0);
headMotor.write(180);
delay(2000);
rightMotor.write(0);
leftMotor.write(180);
headMotor.write(90);
delay(2000);
}
else {
off();
}
}