-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensor.ino
More file actions
90 lines (73 loc) · 1.8 KB
/
Sensor.ino
File metadata and controls
90 lines (73 loc) · 1.8 KB
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
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
#define LED_PIN 13
#define VOLUME_MEASURE_PIN A1
#define VIN 5
#define DELAY_TIME 2500
String tagId = "Nenhuma";
byte nuidPICC[4];
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
int volume = 1;
unsigned long start_time;
unsigned long timed_event;
unsigned long current_time;
void setup(void) {
Serial.begin(9600);
Serial.println("Sistema inicializado");
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
nfc.begin();
current_time = millis();
start_time = current_time;
}
void loop() {
changeVolume();
receiveMessage();
current_time = millis();
if (current_time - start_time >= DELAY_TIME) {
readNFC();
start_time = current_time;
}
}
void changeVolume() {
int newVolume = analogRead(VOLUME_MEASURE_PIN);
if (newVolume != volume) {
float res = convertVolumeRawValue(newVolume);
Serial.println("volume:" + String(res));
volume = newVolume;
}
}
float convertVolumeRawValue(int raw){
float Vout = float(raw) * (VIN / float(1023));
float phys = (Vout)/VIN;
return phys;
}
void receiveMessage() {
if (Serial.available() > 0) {
byte input = Serial.read();
if (input == 'B') {
digitalWrite(LED_PIN, LOW);
delay(250);
digitalWrite(LED_PIN,HIGH);
}
if (input == 'P') {
digitalWrite(LED_PIN, LOW);
delay(100);
digitalWrite(LED_PIN,HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
digitalWrite(LED_PIN,HIGH);
}
}
}
void readNFC() {
if (nfc.tagPresent()) {
NfcTag tag = nfc.read();
tagId = tag.getUidString();
Serial.println("tag:" + tagId);
}
}