|
| 1 | +/** |
| 2 | + * HC-SR04 Demo |
| 3 | + * Demonstration of the HC-SR04 Ultrasonic Sensor |
| 4 | + * Date: August 3, 2016 |
| 5 | + * |
| 6 | + * Description: |
| 7 | + * Connect the ultrasonic sensor to the Arduino as per the |
| 8 | + * hardware connections below. Run the sketch and open a serial |
| 9 | + * monitor. The distance read from the sensor will be displayed |
| 10 | + * in centimeters and inches. |
| 11 | + * |
| 12 | + * Hardware Connections: |
| 13 | + * Arduino | HC-SR04 |
| 14 | + * ------------------- |
| 15 | + * 5V | VCC |
| 16 | + * 7 | Trig |
| 17 | + * 8 | Echo |
| 18 | + * GND | GND |
| 19 | + * |
| 20 | + * License: |
| 21 | + * Public Domain |
| 22 | + */ |
| 23 | +#include <Wire.h> |
| 24 | +#include <Adafruit_GFX.h> |
| 25 | +#include <Adafruit_SSD1306.h> |
| 26 | + |
| 27 | +// Pins |
| 28 | +const int TRIG_PIN = 7; |
| 29 | +const int ECHO_PIN = 8; |
| 30 | + |
| 31 | +// Anything over 400 cm (23200 us pulse) is "out of range" |
| 32 | +const unsigned int MAX_DIST = 23200; |
| 33 | + |
| 34 | +#define OLED_RESET 4 |
| 35 | +Adafruit_SSD1306 display(OLED_RESET); |
| 36 | + |
| 37 | +#define NUMFLAKES 10 |
| 38 | +#define XPOS 0 |
| 39 | +#define YPOS 1 |
| 40 | +#define DELTAY 2 |
| 41 | + |
| 42 | + |
| 43 | +#define LOGO16_GLCD_HEIGHT 16 |
| 44 | +#define LOGO16_GLCD_WIDTH 16 |
| 45 | +static const unsigned char PROGMEM logo16_glcd_bmp[] = |
| 46 | +{ B00000000, B11000000, |
| 47 | + B00000001, B11000000, |
| 48 | + B00000001, B11000000, |
| 49 | + B00000011, B11100000, |
| 50 | + B11110011, B11100000, |
| 51 | + B11111110, B11111000, |
| 52 | + B01111110, B11111111, |
| 53 | + B00110011, B10011111, |
| 54 | + B00011111, B11111100, |
| 55 | + B00001101, B01110000, |
| 56 | + B00011011, B10100000, |
| 57 | + B00111111, B11100000, |
| 58 | + B00111111, B11110000, |
| 59 | + B01111100, B11110000, |
| 60 | + B01110000, B01110000, |
| 61 | + B00000000, B00110000 }; |
| 62 | + |
| 63 | +#if (SSD1306_LCDHEIGHT != 32) |
| 64 | +#error("Height incorrect, please fix Adafruit_SSD1306.h!"); |
| 65 | +#endif |
| 66 | + |
| 67 | +void setup() { |
| 68 | + |
| 69 | + // The Trigger pin will tell the sensor to range find |
| 70 | + pinMode(TRIG_PIN, OUTPUT); |
| 71 | + digitalWrite(TRIG_PIN, LOW); |
| 72 | + |
| 73 | + // We'll use the serial monitor to view the sensor output |
| 74 | + Serial.begin(9600); |
| 75 | + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| 76 | + display.clearDisplay(); |
| 77 | +} |
| 78 | + |
| 79 | +void loop() { |
| 80 | + |
| 81 | + unsigned long t1; |
| 82 | + unsigned long t2; |
| 83 | + unsigned long pulse_width; |
| 84 | + float cm; |
| 85 | + float inches; |
| 86 | + |
| 87 | + // Hold the trigger pin high for at least 10 us |
| 88 | + digitalWrite(TRIG_PIN, HIGH); |
| 89 | + delayMicroseconds(50); |
| 90 | + digitalWrite(TRIG_PIN, LOW); |
| 91 | + |
| 92 | + // Wait for pulse on echo pin |
| 93 | + while ( digitalRead(ECHO_PIN) == 0 ); |
| 94 | + |
| 95 | + // Measure how long the echo pin was held high (pulse width) |
| 96 | + // Note: the micros() counter will overflow after ~70 min |
| 97 | + t1 = micros(); |
| 98 | + while ( digitalRead(ECHO_PIN) == 1); |
| 99 | + t2 = micros(); |
| 100 | + pulse_width = t2 - t1; |
| 101 | + |
| 102 | + // Calculate distance in centimeters and inches. The constants |
| 103 | + // are found in the datasheet, and calculated from the assumed speed |
| 104 | + //of sound in air at sea level (~340 m/s). |
| 105 | + cm = pulse_width / 58.0; |
| 106 | + inches = pulse_width / 148.0; |
| 107 | + |
| 108 | + // Print out results |
| 109 | + if ( pulse_width > MAX_DIST ) { |
| 110 | + Serial.println("Out of range"); |
| 111 | + } else { |
| 112 | + Serial.print(cm); |
| 113 | + Serial.print(" cm \t"); |
| 114 | + Serial.print(inches); |
| 115 | + Serial.println(" in"); |
| 116 | + display.setTextSize(2); |
| 117 | + display.setTextColor(WHITE); |
| 118 | + display.setCursor(10,0); |
| 119 | + display.clearDisplay(); |
| 120 | + display.print(cm); |
| 121 | + display.print("cm"); |
| 122 | + display.display(); |
| 123 | + delay(1); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + // Wait at least 60ms before next measurement |
| 128 | + delay(60); |
| 129 | +} |
| 130 | +void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { |
| 131 | + uint8_t icons[NUMFLAKES][3]; |
| 132 | + |
| 133 | + // initialize |
| 134 | + for (uint8_t f=0; f< NUMFLAKES; f++) { |
| 135 | + icons[f][XPOS] = random(display.width()); |
| 136 | + icons[f][YPOS] = 0; |
| 137 | + icons[f][DELTAY] = random(5) + 1; |
| 138 | + |
| 139 | + Serial.print("x: "); |
| 140 | + Serial.print(icons[f][XPOS], DEC); |
| 141 | + Serial.print(" y: "); |
| 142 | + Serial.print(icons[f][YPOS], DEC); |
| 143 | + Serial.print(" dy: "); |
| 144 | + Serial.println(icons[f][DELTAY], DEC); |
| 145 | + } |
| 146 | + |
| 147 | + while (1) { |
| 148 | + // draw each icon |
| 149 | + for (uint8_t f=0; f< NUMFLAKES; f++) { |
| 150 | + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); |
| 151 | + } |
| 152 | + display.display(); |
| 153 | + delay(200); |
| 154 | + |
| 155 | + // then erase it + move it |
| 156 | + for (uint8_t f=0; f< NUMFLAKES; f++) { |
| 157 | + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); |
| 158 | + // move it |
| 159 | + icons[f][YPOS] += icons[f][DELTAY]; |
| 160 | + // if its gone, reinit |
| 161 | + if (icons[f][YPOS] > display.height()) { |
| 162 | + icons[f][XPOS] = random(display.width()); |
| 163 | + icons[f][YPOS] = 0; |
| 164 | + icons[f][DELTAY] = random(5) + 1; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +void testdrawchar(void) { |
| 172 | + display.setTextSize(1); |
| 173 | + display.setTextColor(WHITE); |
| 174 | + display.setCursor(0,0); |
| 175 | + |
| 176 | + for (uint8_t i=0; i < 168; i++) { |
| 177 | + if (i == '\n') continue; |
| 178 | + display.write(i); |
| 179 | + if ((i > 0) && (i % 21 == 0)) |
| 180 | + display.println(); |
| 181 | + } |
| 182 | + display.display(); |
| 183 | + delay(1); |
| 184 | +} |
| 185 | + |
| 186 | +void testdrawcircle(void) { |
| 187 | + for (int16_t i=0; i<display.height(); i+=2) { |
| 188 | + display.drawCircle(display.width()/2, display.height()/2, i, WHITE); |
| 189 | + display.display(); |
| 190 | + delay(1); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +void testfillrect(void) { |
| 195 | + uint8_t color = 1; |
| 196 | + for (int16_t i=0; i<display.height()/2; i+=3) { |
| 197 | + // alternate colors |
| 198 | + display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2); |
| 199 | + display.display(); |
| 200 | + delay(1); |
| 201 | + color++; |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +void testdrawtriangle(void) { |
| 206 | + for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) { |
| 207 | + display.drawTriangle(display.width()/2, display.height()/2-i, |
| 208 | + display.width()/2-i, display.height()/2+i, |
| 209 | + display.width()/2+i, display.height()/2+i, WHITE); |
| 210 | + display.display(); |
| 211 | + delay(1); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void testfilltriangle(void) { |
| 216 | + uint8_t color = WHITE; |
| 217 | + for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) { |
| 218 | + display.fillTriangle(display.width()/2, display.height()/2-i, |
| 219 | + display.width()/2-i, display.height()/2+i, |
| 220 | + display.width()/2+i, display.height()/2+i, WHITE); |
| 221 | + if (color == WHITE) color = BLACK; |
| 222 | + else color = WHITE; |
| 223 | + display.display(); |
| 224 | + delay(1); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +void testdrawroundrect(void) { |
| 229 | + for (int16_t i=0; i<display.height()/2-2; i+=2) { |
| 230 | + display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE); |
| 231 | + display.display(); |
| 232 | + delay(1); |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +void testfillroundrect(void) { |
| 237 | + uint8_t color = WHITE; |
| 238 | + for (int16_t i=0; i<display.height()/2-2; i+=2) { |
| 239 | + display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color); |
| 240 | + if (color == WHITE) color = BLACK; |
| 241 | + else color = WHITE; |
| 242 | + display.display(); |
| 243 | + delay(1); |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +void testdrawrect(void) { |
| 248 | + for (int16_t i=0; i<display.height()/2; i+=2) { |
| 249 | + display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE); |
| 250 | + display.display(); |
| 251 | + delay(1); |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +void testdrawline() { |
| 256 | + for (int16_t i=0; i<display.width(); i+=4) { |
| 257 | + display.drawLine(0, 0, i, display.height()-1, WHITE); |
| 258 | + display.display(); |
| 259 | + delay(1); |
| 260 | + } |
| 261 | + for (int16_t i=0; i<display.height(); i+=4) { |
| 262 | + display.drawLine(0, 0, display.width()-1, i, WHITE); |
| 263 | + display.display(); |
| 264 | + delay(1); |
| 265 | + } |
| 266 | + delay(250); |
| 267 | + |
| 268 | + display.clearDisplay(); |
| 269 | + for (int16_t i=0; i<display.width(); i+=4) { |
| 270 | + display.drawLine(0, display.height()-1, i, 0, WHITE); |
| 271 | + display.display(); |
| 272 | + delay(1); |
| 273 | + } |
| 274 | + for (int16_t i=display.height()-1; i>=0; i-=4) { |
| 275 | + display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); |
| 276 | + display.display(); |
| 277 | + delay(1); |
| 278 | + } |
| 279 | + delay(250); |
| 280 | + |
| 281 | + display.clearDisplay(); |
| 282 | + for (int16_t i=display.width()-1; i>=0; i-=4) { |
| 283 | + display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); |
| 284 | + display.display(); |
| 285 | + delay(1); |
| 286 | + } |
| 287 | + for (int16_t i=display.height()-1; i>=0; i-=4) { |
| 288 | + display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); |
| 289 | + display.display(); |
| 290 | + delay(1); |
| 291 | + } |
| 292 | + delay(250); |
| 293 | + |
| 294 | + display.clearDisplay(); |
| 295 | + for (int16_t i=0; i<display.height(); i+=4) { |
| 296 | + display.drawLine(display.width()-1, 0, 0, i, WHITE); |
| 297 | + display.display(); |
| 298 | + delay(1); |
| 299 | + } |
| 300 | + for (int16_t i=0; i<display.width(); i+=4) { |
| 301 | + display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE); |
| 302 | + display.display(); |
| 303 | + delay(1); |
| 304 | + } |
| 305 | + delay(250); |
| 306 | +} |
| 307 | + |
| 308 | +void testscrolltext(void) { |
| 309 | + display.setTextSize(2); |
| 310 | + display.setTextColor(WHITE); |
| 311 | + display.setCursor(10,0); |
| 312 | + display.clearDisplay(); |
| 313 | + display.println("scroll"); |
| 314 | + display.display(); |
| 315 | + delay(1); |
| 316 | + |
| 317 | + display.startscrollright(0x00, 0x0F); |
| 318 | + delay(2000); |
| 319 | + display.stopscroll(); |
| 320 | + delay(1000); |
| 321 | + display.startscrollleft(0x00, 0x0F); |
| 322 | + delay(2000); |
| 323 | + display.stopscroll(); |
| 324 | + delay(1000); |
| 325 | + display.startscrolldiagright(0x00, 0x07); |
| 326 | + delay(2000); |
| 327 | + display.startscrolldiagleft(0x00, 0x07); |
| 328 | + delay(2000); |
| 329 | + display.stopscroll(); |
| 330 | +} |
| 331 | + |
0 commit comments