source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Adafruit_CircuitPlayground/examples/color_sense/color_sense.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 2.6 KB
Line 
1// Circuit Playground Color Sensing Example
2// Use a NeoPixel RGB LED and light sensor on the Circuit Playground board to
3// do basic color detection.  By quickly flashing full red, green, and blue color
4// light from the NeoPixel the light sensor can read the intensity of the
5// reflected light and roughly approximate the color of the object.
6//
7// After uploading the sketch to Circuit Playground you can press and release the
8// left or right button to do a color sense and print the red, green, blue component
9// values to the serial monitor (115200 baud).  In addition all the NeoPixels on
10// the board will be lit up to the detected color.  You should hold a brightly
11// colored object right above the light sensor and NeoPixel #1 (upper
12// left part of board, look for the eye symbol next to the color sensor) when
13// performing the color sense.
14//
15// Author: Limor Fried & Tony DiCola
16// License: MIT License (https://opensource.org/licenses/MIT)
17#include <Adafruit_CircuitPlayground.h>
18#include <Wire.h>
19#include <SPI.h>
20
21void setup() {
22  Serial.begin(115200);
23  // Initialize Circuit Playground library.
24  CircuitPlayground.begin();
25}
26
27void loop() {
28  // Detect if the left or right button is pressed by taking two readings with
29  // a small delay inbetween.  If the button changes state (like it was pressed
30  // or released) then the two readings will be different.
31  bool left_first = CircuitPlayground.leftButton();
32  bool right_first = CircuitPlayground.rightButton();
33  delay(20);
34  bool left_second = CircuitPlayground.leftButton();
35  bool right_second = CircuitPlayground.rightButton();
36  // Now check if either button was released, i.e. changed from a true (pressed)
37  // state to a false (non-pressed) state.
38  if ((left_first && !left_second) || (right_first && !right_second)) {
39    // Button was pressed, perform a color sense.
40    // First turn off all the pixels to make sure they don't interfere with the
41    // reading.
42    CircuitPlayground.clearPixels();
43    // Now take a color reading (the red, green, blue color components will be
44    // returned in the parameters passed in).
45    uint8_t red, green, blue;
46    CircuitPlayground.senseColor(red, green, blue);
47    // Print out the color components.
48    Serial.print("Color: red=");
49    Serial.print(red, DEC);
50    Serial.print(" green=");
51    Serial.print(green, DEC);
52    Serial.print(" blue=");
53    Serial.println(blue, DEC);
54    // Finally set all the pixels to the detected color.
55    for (int i=0; i<10; ++i) {
56      CircuitPlayground.strip.setPixelColor(i, red, green, blue);
57    }
58    CircuitPlayground.strip.show();
59  }
60}
Note: See TracBrowser for help on using the repository browser.