ampel-firmware.ino 2.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/***
 *       ____ ___ ____       _                         _
 *      / ___/ _ \___ \     / \   _ __ ___  _ __   ___| |
 *     | |  | | | |__) |   / _ \ | '_ ` _ \| '_ \ / _ \ |
 *     | |__| |_| / __/   / ___ \| | | | | | |_) |  __/ |
 *      \____\___/_____| /_/__ \_\_| |_| |_| .__/ \___|_|         _
 *     | | | |/ _|_   _| / ___|| |_ _   _| |_| |_ __ _  __ _ _ __| |_
 *     | |_| | |_  | |   \___ \| __| | | | __| __/ _` |/ _` | '__| __|
 *     |  _  |  _| | |    ___) | |_| |_| | |_| || (_| | (_| | |  | |_
 *     |_| |_|_|   |_|   |____/ \__|\__,_|\__|\__\__, |\__,_|_|   \__|
 *                                               |___/
 */

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Small example, with CO2 sensor and LED ring.
// Required libraries:
//  LED Ring:
#include "src/lib/Adafruit_NeoPixel/Adafruit_NeoPixel.h"
//  CO2 sensor:
#include "src/lib/SparkFun_SCD30_Arduino_Library/src/SparkFun_SCD30_Arduino_Library.h"
#include <Wire.h>

// How many LEDs on the ring?
const int LED_COUNT = 12;
// Where is LED ring connected to micro-controller?
const int NEOPIXELS_PIN = 5;

// Define LED ring and CO2 sensor
Adafruit_NeoPixel led_ring(LED_COUNT, NEOPIXELS_PIN, NEO_GRB + NEO_KHZ800);
SCD30 sensor;

// Define variables
int which_led;
uint16_t co2;
34

Eric Duminil's avatar
Eric Duminil committed
35
// The code in setup() will be run once, at startup.
36
void setup() {
37
38
  // Transmission speed
  Serial.begin(115200);
Eric Duminil's avatar
Eric Duminil committed
39

40
41
42
  // Initialize LED ring.
  led_ring.begin();
  led_ring.setBrightness(255);
Eric Duminil's avatar
Eric Duminil committed
43

44
45
46
  // Initialize sensor (should be connected to D6 & D5), without auto-calibration.
  Wire.begin(12, 14);
  sensor.begin(false);
Eric Duminil's avatar
Eric Duminil committed
47

48
49
  // One measurement every 10s
  sensor.setMeasurementInterval(10);
50
51
}

Eric Duminil's avatar
Eric Duminil committed
52
// The code in loop() will be run as long as the micro-controller is powered up, in an infinite loop.
53
void loop() {
54
55
56
57
58
59
  if (sensor.dataAvailable()) {
    // New measurement is available. Let's display it!
    co2 = sensor.getCO2();
    Serial.print("CO2 : ");
    Serial.print(co2);
    Serial.println(" ppm.");
60
61
  }

62
63
64
65
66
  // Light the next LED
  which_led = which_led + 1;
  // Come back to first LED if needed.
  if (which_led >= LED_COUNT) {
    which_led = 0;
Eric Duminil's avatar
Eric Duminil committed
67
68
  }

69
70
71
72
73
  // First, disable every LED
  led_ring.clear();
  // Check if air is good enough
  if (co2 <= 1000) {
    // Light the LED green if CO2 ppm is low
Eric Duminil's avatar
Eric Duminil committed
74
    //                   position, red, green, blue
75
76
77
    led_ring.setPixelColor(which_led, 0, 255, 0);
  } else {
    // Red otherwise.
Eric Duminil's avatar
Eric Duminil committed
78
    //                   position, red, green, blue
79
    led_ring.setPixelColor(which_led, 255, 0, 0);
80
  }
81
  led_ring.show();
82

83
84
85
  // Wait half a second before next loop.
  // Micro-controller might crash if it has too much work to do. Please don't remove it!
  delay(500);
86
}