Skip to content

Commit b32d82b

Browse files
committed
1 parent 687d40b commit b32d82b

File tree

1 file changed

+97
-102
lines changed

1 file changed

+97
-102
lines changed
Lines changed: 97 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,141 @@
1-
// Combined Demo by Marc MERLIN <marc_soft@merlins.org>
2-
1+
// Combined Demo by Marc MERLIN <marc_soft@merlins.org>
2+
33
#include "Arduino_SensorKit.h"
4-
#define BUZZER 5
5-
4+
#define BUZZER 5
5+
66
uint8_t button = 4;
77
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
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
1616
// 30038 25 0x19 V V V V V V V V
1717
// 30133 60 0x3C V V V V V V V V
1818
// 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-
//Serial.println("I2C scan");
29-
// The i2c_scanner uses the return value of
30-
// the Write.endTransmisstion to see if
31-
// a device did acknowledge to the address.
32-
Wire.beginTransmission(address);
33-
error = Wire.endTransmission();
34-
35-
if (error == 0)
36-
{
37-
Serial.print("I2C device found at address 0x");
38-
if (address<16)
39-
Serial.print("0");
40-
Serial.print(address,HEX);
41-
Serial.println(" !");
42-
43-
nDevices++;
44-
}
45-
else if (error==4)
46-
{
47-
Serial.print("Unknown error at address 0x");
48-
if (address<16)
49-
Serial.print("0");
50-
Serial.println(address,HEX);
51-
}
52-
}
53-
if (nDevices == 0)
54-
Serial.println("No I2C devices found\n");
55-
else
56-
Serial.println("done\n");
57-
}
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+
//Serial.println("I2C scan");
29+
// The i2c_scanner uses the return value of
30+
// the Write.endTransmisstion to see if
31+
// a device did acknowledge to the address.
32+
Wire.beginTransmission(address);
33+
error = Wire.endTransmission();
34+
35+
if (error == 0)
36+
{
37+
Serial.print("I2C device found at address 0x");
38+
if (address<16)
39+
Serial.print("0");
40+
Serial.print(address,HEX);
41+
Serial.println(" !");
42+
43+
nDevices++;
44+
}
45+
else if (error==4)
46+
{
47+
Serial.print("Unknown error at address 0x");
48+
if (address<16)
49+
Serial.print("0");
50+
Serial.println(address,HEX);
51+
}
52+
}
53+
if (nDevices == 0)
54+
Serial.println("No I2C devices found\n");
55+
else
56+
Serial.println("done\n");
57+
}
5858

5959
void setup() {
60-
Serial.begin(115200);
61-
// Running scan stops the OLED from working
62-
//i2c_scan();
63-
60+
Serial.begin(115200);
61+
// Running scan stops the OLED from working
62+
//i2c_scan();
63+
6464
pinMode(mic , INPUT);
6565
pinMode(light , INPUT);
66-
66+
6767
pinMode(button , INPUT);
6868
pinMode(led, OUTPUT);
6969
digitalWrite(led, LOW);
70-
70+
7171
Environment.begin();
72-
72+
7373
Oled.begin();
7474
Oled.setFlipMode(true);
75-
76-
// Enabling any of those 2 stops the OLED from working
77-
//Accelerometer.begin();
78-
//Pressure.begin();
79-
80-
pinMode(BUZZER, OUTPUT);
75+
76+
Accelerometer.begin();
77+
Pressure.begin();
78+
79+
pinMode(BUZZER, OUTPUT);
8180
}
8281

8382
void loop() {
84-
83+
8584
Oled.setFont(u8x8_font_amstrad_cpc_extended_r);
8685

87-
#if 0
88-
Oled.setCursor(0, 0);
89-
Oled.print("x:");
90-
Oled.print(Accelerometer.readX());
91-
Oled.print(" ");
92-
Oled.print("y:");
93-
Oled.print(Accelerometer.readY());
94-
Oled.print(" ");
95-
Oled.print("z:");
96-
Oled.print(Accelerometer.readZ());
97-
// Similar problem with pressure sensor
98-
Oled.print(Pressure.readAltitude());
99-
#endif
100-
101-
// when using u8g8 instead of u8g2, cursor values
102-
// are in characters, not pixels
103-
uint16_t pot_value = analogRead(pot);
104-
10586
Oled.setCursor(0, 0);
10687
Oled.print("But:");
107-
88+
89+
uint16_t pot_value = analogRead(pot);
10890
if (digitalRead(button)) {
10991
digitalWrite(led, HIGH);
11092
Oled.print("1");
111-
tone(BUZZER, pot_value);
93+
tone(BUZZER, pot_value);
11294
} else {
11395
digitalWrite(led, LOW);
11496
Oled.print("0");
115-
noTone(BUZZER);
97+
noTone(BUZZER);
11698
}
117-
99+
118100
Oled.setCursor(0, 1);
119101
Oled.print("BuzPot: ");
120102
Oled.print(pot_value);
121-
Oled.print("Hz ");
122-
123-
uint16_t mic_value = analogRead(mic);
103+
Oled.print("Hz ");
104+
105+
uint16_t mic_value = analogRead(mic);
124106
Oled.setCursor(0, 2);
125107
Oled.print("Mic: ");
126108
Oled.print(mic_value);
127-
Oled.print(" ");
128-
129-
uint16_t light_value = analogRead(light);
109+
Oled.print(" ");
110+
111+
uint16_t light_value = analogRead(light);
130112
Oled.setCursor(0, 3);
131113
Oled.print("Light: ");
132114
Oled.print(light_value);
133-
Oled.print(" ");
134-
135-
Oled.setCursor(0, 6);
136-
Oled.print("Temp:");
137-
Oled.print(Environment.readTemperature());
115+
Oled.print(" ");
116+
117+
// when using u8g8 instead of u8g2, cursor values
118+
// are in characters, not pixels
119+
Oled.setCursor(0, 4);
120+
Oled.print("x:");
121+
Oled.print(Accelerometer.readX());
122+
Oled.print(" y:");
123+
Oled.print(Accelerometer.readY());
124+
Oled.setCursor(0, 5);
125+
Oled.print("z:");
126+
Oled.print(Accelerometer.readZ());
127+
Oled.print(" T:");
128+
Oled.print(Environment.readTemperature());
138129
Oled.print("C");
139-
140-
Oled.setCursor(0, 7);
130+
131+
Oled.setCursor(0, 6);
141132
Oled.print("Hum: ");
142-
Oled.print(Environment.readHumidity());
133+
Oled.print(Environment.readHumidity());
143134
Oled.print("%");
144-
135+
136+
Oled.setCursor(0, 7);
137+
Oled.print("Alt:");
138+
Oled.print(Pressure.readAltitude());
139+
145140
delay(100);
146141
}

0 commit comments

Comments
 (0)