-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab03-Slave.ino
90 lines (79 loc) · 1.78 KB
/
Lab03-Slave.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
#include <Wire.h>
const int greenLedPin = 2;
const int redLedPin = 3;
const int yellowLedPin = 4;
int temperature = 0;
int light = 0;
int angle = 0;
boolean isAngleOn = false;
int intervalTime = 0;
unsigned long previousTime = 0, currentTime = 0;
void setup() {
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
Wire.begin(8);
Wire.onReceive(receiveEvent);
while(millis() < 3000);
}
void loop() {
if(temperature > 27) {
digitalWrite(greenLedPin, HIGH);
} else {
digitalWrite(greenLedPin, LOW);
}
// Noise Reduction
if(light < 20) {
light = 0;
} else if (light < 40) {
light = 30;
} else if (light < 60) {
light = 50;
} else if (light < 80) {
light = 70;
} else if (light < 100) {
light = 90;
} else if (light < 120) {
light = 110;
} else if (light < 140) {
light = 130;
} else if (light < 160) {
light = 150;
} else if (light < 180) {
light = 170;
} else if (light < 200) {
light = 190;
} else if (light < 220) {
light = 210;
} else if (light < 240) {
light = 230;
} else {
light = 255;
}
analogWrite(redLedPin, light);
// Potentiometer
currentTime = millis();
intervalTime = map(angle, 0, 180, 200, 2000); // Map Angle to Miliseconds
if (currentTime - previousTime >= intervalTime) {
isAngleOn = !isAngleOn;
if(isAngleOn) {
digitalWrite(yellowLedPin, HIGH);
} else {
digitalWrite(yellowLedPin, LOW);
}
previousTime = currentTime;
}
}
void receiveEvent() {
char type;
while (1 < Wire.available()) {
type = Wire.read(); // Read Type
}
if(type == 'T') {
temperature = Wire.read();
} else if(type == 'L') {
light = Wire.read();
} else if(type == 'A') {
angle = Wire.read();
}
}