This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
primer_codigo_movimiento.ino
189 lines (150 loc) · 3.41 KB
/
primer_codigo_movimiento.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <Servo.h>
int motor_left[] = {5, 6};
int motor_right[] = {7, 8};
int state = 0;
int ledPin=12;
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
const int buzzerPin = 9;
Servo servo;
// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //Declara pin de Salida
digitalWrite(ledPin, LOW); //Normalmente Apagado
pinMode(buzzerPin, OUTPUT);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
//Setup Servo
servo.attach(10);
}
// --------------------------------------------------------------------------- Loop
void loop() {
//si el modulo a manda dato, guardarlo en estado.
if(Serial.available() > 0){
state = Serial.read();
} // esta parte del código es para solo 1 Carácter o Unidad.
// si el estado es 0 ese sería Apagado “OFF”
if (state == '0') {
digitalWrite(ledPin, LOW);
drive_forward();
}
// de lo contrario si el estado es 1 ese sería Encendido “ON”
if (state == '1') {
digitalWrite(ledPin, HIGH);
drive_backward();
}
if (state == '2') {
turn_left();
}
if (state == '3') {
turn_right();
}
if (state == '4') {
motor_stop();
}
if (state == '5') {
firstSection();
}
if (state == '6') {
servo1();
}
if (state == '7') {
servo2();
}
state=-1;
}
// --------------------------------------------------------------------------- Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void drive_backward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
// --------------------------------------------------------------------------- Buzzer
void beep(int note, int duration)
{
//Play tone on buzzerPin
tone(buzzerPin, note, duration);
delay(duration);
//Stop tone on buzzerPin
noTone(buzzerPin);
delay(50);
}
void firstSection()
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
beep(eH, 500);
beep(eH, 500);
beep(eH, 500);
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
state=-1;
}
// --------------------------------------------------------------------------- Fire
void servo1(){
servo.write(105);
}
void servo2(){
servo.write(0);
}