8
8
9
9
#include " Modulino.h"
10
10
11
+ // Create objects for all Modulino modules
11
12
ModulinoButtons buttons;
12
13
ModulinoBuzzer buzzer;
13
14
ModulinoPixels leds;
@@ -19,15 +20,14 @@ ModulinoThermo thermo;
19
20
void setup () {
20
21
21
22
Serial.begin (115200 );
23
+ // Initialize Modulino I2C communication
22
24
Modulino.begin ();
23
-
25
+ // Detect and connect to all modules
24
26
distance.begin ();
25
-
26
27
buttons.begin ();
27
28
encoder.begin ();
28
29
buzzer.begin ();
29
30
leds.begin ();
30
-
31
31
imu.begin ();
32
32
thermo.begin ();
33
33
}
@@ -43,14 +43,17 @@ void loop() {
43
43
float x;
44
44
float y;
45
45
float z;
46
-
46
+ // Cycle through LED patterns when encoder is pressed
47
47
if (encoder.isPressed ()) {
48
48
skip = (skip + 1 ) % 5 ;
49
49
}
50
50
51
+ // Calculate pitch by combining encoder position and distance sensor value
51
52
pitch = encoder.get () + (distance.available () ? distance.get () : 0 );
52
53
54
+ // When 's' is received over serial, report sensor data
53
55
if (Serial.available () && Serial.read () == ' s' ) {
56
+ // Update and report accelerometer values
54
57
imu.update ();
55
58
Serial.print (" IMU: x " );
56
59
Serial.print (imu.getX (), 3 );
@@ -59,29 +62,35 @@ void loop() {
59
62
Serial.print (" \t z " );
60
63
Serial.println (imu.getZ (), 3 );
61
64
65
+ // Report temperature and humidity values
62
66
Serial.print (" Humidity: " + String (thermo.getHumidity ()));
63
67
Serial.println (" \t Temperature: " + String (thermo.getTemperature ()));
64
68
}
65
69
70
+ // Check for button presses
66
71
if (buttons.update ()) {
72
+ // Button A: Red LED and 440Hz tone
67
73
if (buttons.isPressed (0 )) {
68
74
leds.set (1 + skip, RED, 100 );
69
75
buzzer.tone (440 + pitch, 1000 );
70
76
} else {
71
77
leds.clear (1 + skip);
72
78
}
79
+ // Button B: Blue LED and 880Hz tone
73
80
if (buttons.isPressed (1 )) {
74
81
leds.set (2 + skip, BLUE, 100 );
75
82
buzzer.tone (880 + pitch, 1000 );
76
83
} else {
77
84
leds.clear (2 + skip);
78
85
}
86
+ // Button C: Green LED and 1240Hz tone
79
87
if (buttons.isPressed (2 )) {
80
88
leds.set (3 + skip, GREEN, 100 );
81
89
buzzer.tone (1240 + pitch, 1000 );
82
90
} else {
83
91
leds.clear (3 + skip);
84
92
}
93
+ // Update the LEDs
85
94
leds.show ();
86
95
}
87
96
}
0 commit comments