/** * File: ColorSensor-Graph.ino * Description: * This code slowly cycles through red-green-blue lights, and measure the reflected * light level. This way one can have an idea about the "black" level of the sensor, * and the sensing levels of the different colors. * * Open "Serial Plotter" with 115200bps in the Arduino IDE for nice visualization. * * Author: Balazs Kelemen * Contact: prampec+arduino@gmail.com * Copyright: 2016 Balazs Kelemen * Copying permission statement: ColorSensor-Graph is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #define RED A3 #define GREEN 2 #define BLUE 3 #define S1 A0 #define S2 A2 #define DELAY_MS 400 #define PRINT_FREQ_MS 10 unsigned long lastPrint = 0; void setup() { Serial.begin(115200); pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); // TODO: make some calibration } void loop() { writeColor(HIGH, LOW, LOW); detectValueFor(DELAY_MS); writeColor(LOW, HIGH, LOW); detectValueFor(DELAY_MS); writeColor(LOW, LOW, HIGH); detectValueFor(DELAY_MS); writeColor(LOW, LOW, LOW); detectValueFor(DELAY_MS); writeColor(LOW, LOW, LOW); detectValueFor(DELAY_MS); writeColor(LOW, LOW, LOW); detectValueFor(DELAY_MS); } void writeColor(boolean red, boolean green, boolean blue) { digitalWrite(RED, red); digitalWrite(GREEN, green); digitalWrite(BLUE, blue); } void detectValueFor(unsigned long ms) { unsigned long startTime = millis(); unsigned long now = startTime; while((now < (startTime + ms)) && (now >= startTime)) { int s1 = analogRead(S1); int s2 = analogRead(S2); if(((lastPrint + PRINT_FREQ_MS) < now) || (now < lastPrint)) { Serial.print(s1); Serial.print(','); Serial.print(s2); Serial.println(); lastPrint = now; } now = millis(); } }