1
1
// Combined Demo by Marc MERLIN <marc_soft@merlins.org>
2
+ // Reviewed by Pablo Marquínez <p.marquinez@arduino.cc>
3
+ // This demo use all the devices from the Arduino SensorKit
4
+ // Showing the values on the Display
2
5
3
6
#include " Arduino_SensorKit.h"
7
+
4
8
#define BUZZER 5
9
+ #define BUTTON 4
10
+ #define LED 6
11
+ #define POT A0
12
+ #define MIC A2
13
+ #define LIGHT A3
5
14
6
- uint8_t button = 4 ;
7
- uint8_t led = 6 ;
8
- uint8_t pot = A0;
9
- uint8_t mic = A2;
10
- uint8_t light = A3;
11
-
12
- // From https://playground.arduino.cc/Main/I2cScanner/
13
- // learn more about I2C here
14
- // https://www.seeedstudio.com/blog/2019/09/25/uart-vs-i2c-vs-spi-communication-protocols-and-uses/
15
- // Scanner from https://github.com/RobTillaart/Arduino/blob/master/sketches/MultiSpeedI2CScanner/MultiSpeedI2CScanner.ino
16
- // 30038 25 0x19 V V V V V V V V
17
- // 30133 60 0x3C V V V V V V V V
18
- // 30296 119 0x77 V V V V V V V V
19
- void i2c_scan () {
20
- uint8_t error, address, nDevices;
21
-
22
- Wire.begin ();
23
- Serial.println (" Scanning..." );
24
-
25
- nDevices = 0 ;
26
- for (address = 1 ; address < 127 ; address++ )
27
- {
28
- // The i2c_scanner uses the return value of
29
- // the Write.endTransmisstion to see if
30
- // a device did acknowledge to the address.
31
- Wire.beginTransmission (address);
32
- error = Wire.endTransmission ();
33
-
34
- if (error == 0 )
35
- {
36
- Serial.print (" I2C device found at address 0x" );
37
- if (address<16 )
38
- Serial.print (" 0" );
39
- Serial.print (address,HEX);
40
- Serial.println (" !" );
41
-
42
- nDevices++;
43
- }
44
- else if (error==4 )
45
- {
46
- Serial.print (" Unknown error at address 0x" );
47
- if (address<16 )
48
- Serial.print (" 0" );
49
- Serial.println (address,HEX);
50
- }
51
- }
52
- if (nDevices == 0 )
53
- Serial.println (" No I2C devices found\n " );
54
- else
55
- Serial.println (" done\n " );
56
- }
15
+ int pot_value;
16
+ bool button_state;
17
+ int mic_value;
18
+ int light_value;
57
19
58
20
void setup () {
59
- Serial.begin (115200 );
60
- // Running scan stops the OLED from working
61
- i2c_scan ();
62
-
63
- pinMode (mic , INPUT);
64
- pinMode (light , INPUT);
65
-
66
- pinMode (button , INPUT);
67
- pinMode (led, OUTPUT);
68
- digitalWrite (led, LOW);
69
-
21
+ Serial.begin (9600 );
22
+
23
+ pinMode (MIC , INPUT);
24
+ pinMode (LIGHT , INPUT);
25
+ pinMode (BUTTON , INPUT);
26
+
27
+ pinMode (LED, OUTPUT);
28
+ digitalWrite (LED, LOW);
29
+ pinMode (BUZZER, OUTPUT);
30
+
70
31
Environment.begin ();
71
32
72
33
Oled.begin ();
73
34
Oled.setFlipMode (true );
74
35
75
36
Accelerometer.begin ();
76
37
Pressure.begin ();
77
-
78
- pinMode (BUZZER, OUTPUT);
79
38
}
80
39
81
40
void loop () {
82
-
83
41
Oled.setFont (u8x8_font_amstrad_cpc_extended_r);
84
-
85
- // when using u8g8 instead of u8g2, cursor values
86
- // are in characters, not pixels
42
+
43
+ // cursor values are in characters, not pixels
87
44
Oled.setCursor (0 , 4 );
45
+
88
46
// If accelerometer and altimeter are queried too close to one another
89
47
// this causes a hang, so we read this first.
90
48
Oled.print (" x:" );
@@ -101,29 +59,31 @@ void loop() {
101
59
Oled.setCursor (0 , 0 );
102
60
Oled.print (" But:" );
103
61
104
- uint16_t pot_value = analogRead (pot);
105
- if (digitalRead (button)) {
106
- digitalWrite (led, HIGH);
107
- Oled.print (" 1" );
62
+ pot_value = analogRead (POT);
63
+
64
+ button_state = digitalRead (BUTTON);
65
+ Oled.print (button_state);
66
+
67
+ if (button_state == true ) {
68
+ digitalWrite (LED, HIGH);
108
69
tone (BUZZER, pot_value);
109
70
} else {
110
- digitalWrite (led, LOW);
111
- Oled.print (" 0" );
71
+ digitalWrite (LED, LOW);
112
72
noTone (BUZZER);
113
73
}
114
-
74
+
115
75
Oled.setCursor (0 , 1 );
116
76
Oled.print (" BuzPot: " );
117
77
Oled.print (pot_value);
118
78
Oled.print (" Hz " );
119
79
120
- uint16_t mic_value = analogRead (mic );
80
+ mic_value = analogRead (MIC );
121
81
Oled.setCursor (0 , 2 );
122
82
Oled.print (" Mic: " );
123
83
Oled.print (mic_value);
124
84
Oled.print (" " );
125
85
126
- uint16_t light_value = analogRead (light );
86
+ light_value = analogRead (LIGHT );
127
87
Oled.setCursor (0 , 3 );
128
88
Oled.print (" Light: " );
129
89
Oled.print (light_value);
0 commit comments