|
1 | | -// Combined Demo by Marc MERLIN <marc_soft@merlins.org> |
2 | | - |
| 1 | +// Combined Demo by Marc MERLIN <marc_soft@merlins.org> |
| 2 | + |
3 | 3 | #include "Arduino_SensorKit.h" |
4 | | -#define BUZZER 5 |
5 | | - |
| 4 | +#define BUZZER 5 |
| 5 | + |
6 | 6 | uint8_t button = 4; |
7 | 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 |
| 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 | 16 | // 30038 25 0x19 V V V V V V V V |
17 | 17 | // 30133 60 0x3C V V V V V V V V |
18 | 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 | | - //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 | +} |
58 | 58 |
|
59 | 59 | 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 | + |
64 | 64 | pinMode(mic , INPUT); |
65 | 65 | pinMode(light , INPUT); |
66 | | - |
| 66 | + |
67 | 67 | pinMode(button , INPUT); |
68 | 68 | pinMode(led, OUTPUT); |
69 | 69 | digitalWrite(led, LOW); |
70 | | - |
| 70 | + |
71 | 71 | Environment.begin(); |
72 | | - |
| 72 | + |
73 | 73 | Oled.begin(); |
74 | 74 | 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); |
81 | 80 | } |
82 | 81 |
|
83 | 82 | void loop() { |
84 | | - |
| 83 | + |
85 | 84 | Oled.setFont(u8x8_font_amstrad_cpc_extended_r); |
86 | 85 |
|
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 | | - |
105 | 86 | Oled.setCursor(0, 0); |
106 | 87 | Oled.print("But:"); |
107 | | - |
| 88 | + |
| 89 | + uint16_t pot_value = analogRead(pot); |
108 | 90 | if (digitalRead(button)) { |
109 | 91 | digitalWrite(led, HIGH); |
110 | 92 | Oled.print("1"); |
111 | | - tone(BUZZER, pot_value); |
| 93 | + tone(BUZZER, pot_value); |
112 | 94 | } else { |
113 | 95 | digitalWrite(led, LOW); |
114 | 96 | Oled.print("0"); |
115 | | - noTone(BUZZER); |
| 97 | + noTone(BUZZER); |
116 | 98 | } |
117 | | - |
| 99 | + |
118 | 100 | Oled.setCursor(0, 1); |
119 | 101 | Oled.print("BuzPot: "); |
120 | 102 | 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); |
124 | 106 | Oled.setCursor(0, 2); |
125 | 107 | Oled.print("Mic: "); |
126 | 108 | 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); |
130 | 112 | Oled.setCursor(0, 3); |
131 | 113 | Oled.print("Light: "); |
132 | 114 | 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()); |
138 | 129 | Oled.print("C"); |
139 | | - |
140 | | - Oled.setCursor(0, 7); |
| 130 | + |
| 131 | + Oled.setCursor(0, 6); |
141 | 132 | Oled.print("Hum: "); |
142 | | - Oled.print(Environment.readHumidity()); |
| 133 | + Oled.print(Environment.readHumidity()); |
143 | 134 | Oled.print("%"); |
144 | | - |
| 135 | + |
| 136 | + Oled.setCursor(0, 7); |
| 137 | + Oled.print("Alt:"); |
| 138 | + Oled.print(Pressure.readAltitude()); |
| 139 | + |
145 | 140 | delay(100); |
146 | 141 | } |
0 commit comments