-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino_program.ino
147 lines (131 loc) · 3.25 KB
/
Arduino_program.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
//
// SPACEAPPS 2019
// A.N.E.M.O.I.
//
//Variables to buzzer_song
#define c3 7634
#define d3 6803
#define e3 6061
#define f3 5714
#define g3 5102
#define a3 4545
#define b3 4049
#define c4 3816 // 261 Hz
#define d4 3401 // 294 Hz
#define e4 3030 // 329 Hz
#define f4 2865 // 349 Hz
#define g4 2551 // 392 Hz
#define a4 2272 // 440 Hz
#define a4s 2146
#define b4 2028 // 493 Hz
#define c5 1912 // 523 Hz
#define d5 1706
#define d5s 1608
#define e5 1517 // 659 Hz
#define f5 1433 // 698 Hz
#define g5 1276
#define a5 1136
#define a5s 1073
#define b5 1012
#define c6 955
#define R 0 // Define a special note, ‘R’, to represent a rest
// Melody 1: Star Wars Imperial March
int melody1[] = {a4, R, a4, R, a4, R, f4, R, c5, R, a4, R, f4, R, c5, R, a4, R, e5, R, e5, R, e5, R, f5, R, c5, R, g5, R, f5, R, c5, R, a4, R};
int beats1[] = {50, 20, 50, 20, 50, 20, 40, 5, 20, 5, 60, 10, 40, 5, 20, 5, 60, 80, 50, 20, 50, 20, 50, 20, 40, 5, 20, 5, 60, 10, 40, 5, 20, 5, 60, 40};
// Melody 2: Star Wars Theme
int melody2[] = {f4, f4, f4, a4s, f5, d5s, d5, c5, a5s, f5, d5s, d5, c5, a5s, f5, d5s, d5, d5s, c5};
int beats2[] = {21, 21, 21, 128, 128, 21, 21, 21, 128, 64, 21, 21, 21, 128, 64, 21, 21, 21, 128};
int MAX_COUNT = sizeof(melody1) / 2;
long tempo = 10000;
int pause = 1000;
int rest_count = 50;
int toneM = 0;
int beat = 0;
long duration = 0;
int potVal = 0;
int speakerOut = 12;
// Variables to buzzer_song
int dataSensor; //datosdel pin analogico
const int MQ135 = A0; //analoge Input
//int digital=4;
int T = 2000; //Time
int rojo = 0;
int amarillo = 3;
int verde = 5;
int buzzer = 12;
void setup()
{
Serial.begin(9600);
pinMode(8, OUTPUT); //azul
pinMode(9, OUTPUT); //Rojo
pinMode(11, OUTPUT); //verde
pinMode(buzzer, OUTPUT);
}
void loop()
{
dataSensor = (analogRead(MQ135)); //Sensor input
if (dataSensor <= 70)
{
Serial.print("Good: ");
Serial.println(verde);
RGB(0, 255, 0);
}
if (dataSensor > 70 && dataSensor <= 100)
{
Serial.print("Less: ");
Serial.println(amarillo);
RGB(255, 120, 0);
}
if (dataSensor > 100)
{
Serial.print("Bad air: ");
Serial.println(rojo);
RGB(255, 0, 0);
while (dataSensor > 17)
{
dataSensor = (analogRead(MQ135)); //Entrada sensor
buzzer_song();
}
}
}
void RGB(int rojo, int verde, int azul)
{
analogWrite(9, rojo); //R
analogWrite(10, verde); //G
analogWrite(8, azul); //B
}
void buzzer_song()
{
//Melody1
for (int i = 0; i < MAX_COUNT; i++)
{
toneM = melody1[i];
beat = beats1[i];
duration = beat * tempo;
playTone();
delayMicroseconds(pause);
}
}
void playTone()
{
long elapsed_time = 0;
if (toneM > 0)
{
while (elapsed_time < duration && dataSensor > 17)
{
digitalWrite(buzzer, HIGH);
delayMicroseconds(toneM / 2);
digitalWrite(buzzer, LOW);
delayMicroseconds(toneM / 2);
elapsed_time += (toneM);
dataSensor = (analogRead(MQ135)); //Entrada sensor
}
}
else
{
for (int j = 0; j < rest_count; j++)
{
delayMicroseconds(duration);
}
}
}