Last active
July 18, 2018 22:27
-
-
Save ShawnHymel/ca3fbfd424442b350ef32820246ea95b to your computer and use it in GitHub Desktop.
Light Spectrogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Qwiic Mux Spectral Demo | |
* Author: Shawn Hymel (SparkFun Electronics) | |
* Date: July 18, 2018 | |
* | |
* Reads from 2 light spectral sensors and shows amount of light | |
* detected in each channel. | |
* | |
* Connect Qwiic Mux and Qwiic OLED to Arduino. Connect Visible | |
* (AS7262) and NIR (AS7263) Spectral Sensor Qwiic Breakout | |
* Boards to the Mux. | |
* | |
* This sketch was written by SparkFun Electronics, with lots of | |
* help from the Arduino community. This code is completely free | |
* for any use. | |
*/ | |
#include <Wire.h> | |
#include <SFE_MicroOLED.h> // https://github.com/sparkfun/SparkFun_Micro_OLED_Arduino_Library | |
#include <AS726X.h> // https://github.com/sparkfun/SparkFun_AS726X_Arduino_Library | |
// Parameters | |
const int MUX_ADDR = 0x70; | |
const int VIS_CHANNEL = 0; | |
const int NIR_CHANNEL = 1; | |
// Pins | |
const int PIN_RESET = 9; | |
const int DC_JUMPER = 1; | |
// Constants | |
const uint16_t MAX_LIGHT = 50000; | |
const uint16_t LIGHT_PER_PIXEL = (MAX_LIGHT / LCDHEIGHT) + 1; | |
// Globals | |
MicroOLED oled(PIN_RESET, DC_JUMPER); | |
AS726X spec; | |
void setup() { | |
// Initialize communcation ports | |
Serial.begin(115200); | |
Wire.begin(); | |
// Initialize OLED | |
delay(100); | |
oled.begin(); | |
oled.clear(ALL); | |
oled.display(); | |
// Initialize visible spectral sensor | |
enableMuxPort(VIS_CHANNEL); | |
spec.begin(); | |
disableMuxPort(VIS_CHANNEL); | |
// Initialize NIR spectral sensor | |
enableMuxPort(NIR_CHANNEL); | |
spec.begin(); | |
disableMuxPort(NIR_CHANNEL); | |
} | |
void loop() { | |
uint16_t light[12]; | |
uint16_t ys[12]; | |
// Take measurements from visible spectral sensor | |
enableMuxPort(VIS_CHANNEL); | |
spec.takeMeasurements(); | |
light[0] = spec.getViolet(); | |
light[1] = spec.getBlue(); | |
light[2] = spec.getGreen(); | |
light[3] = spec.getYellow(); | |
light[4] = spec.getOrange(); | |
light[5] = spec.getRed(); | |
disableMuxPort(VIS_CHANNEL); | |
// Take measurements from NIR spectral sensor | |
enableMuxPort(NIR_CHANNEL); | |
spec.takeMeasurements(); | |
light[6] = spec.getR(); | |
light[7] = spec.getS(); | |
light[8] = spec.getT(); | |
light[9] = spec.getU(); | |
light[10] = spec.getV(); | |
light[11] = spec.getW(); | |
disableMuxPort(NIR_CHANNEL); | |
// Print out all channels | |
for ( int i = 0; i < 12; i++ ) { | |
Serial.print(light[i]); | |
Serial.print(" "); | |
} | |
Serial.println(); | |
// Convert spectral readings to y-axis position | |
for (int i = 0; i < 12; i++ ) { | |
if ( light[i] > MAX_LIGHT ) { | |
light[i] = MAX_LIGHT; | |
} | |
ys[i] = (LCDHEIGHT - 1) - (light[i] / LIGHT_PER_PIXEL); | |
} | |
// Draw spectrum | |
oled.clear(PAGE); | |
drawBar(6, ys[0]); // 450 nm | |
drawBar(12, ys[1]); // 500 nm | |
drawBar(18, ys[2]); // 550 nm | |
drawBar(21, ys[3]); // 570 nm | |
drawBar(25, ys[4]); // 600 nm | |
drawBar(31, ys[5]); // 650 nm | |
drawBar(26, ys[6]); // 610 nm | |
drawBar(35, ys[7]); // 680 nm | |
drawBar(41, ys[8]); // 730 nm | |
drawBar(45, ys[9]); // 760 nm | |
drawBar(51, ys[10]); // 810 nm | |
drawBar(57, ys[11]); // 860 nm | |
// Draw to OLED | |
oled.display(); | |
} | |
// Draw a pixel and the two pixels next to it | |
void drawBar(uint8_t x, uint8_t y) { | |
// Check for out of bounds | |
if ( (x >= LCDWIDTH) || (y >= LCDHEIGHT) ) { | |
return; | |
} | |
// Draw bar | |
if ( x > 0 ) { | |
oled.pixel(x - 1, y); | |
} | |
oled.pixel(x, y); | |
if ( x < LCDWIDTH - 1 ) { | |
oled.pixel(x + 1, y); | |
} | |
} | |
//Enables a specific port number | |
boolean enableMuxPort(byte portNumber) | |
{ | |
if(portNumber > 7) portNumber = 7; | |
//Read the current mux settings | |
Wire.requestFrom(MUX_ADDR, 1); | |
if(!Wire.available()) return(false); //Error | |
byte settings = Wire.read(); | |
//Set the wanted bit to enable the port | |
settings |= (1 << portNumber); | |
Wire.beginTransmission(MUX_ADDR); | |
Wire.write(settings); | |
Wire.endTransmission(); | |
return(true); | |
} | |
//Disables a specific port number | |
boolean disableMuxPort(byte portNumber) | |
{ | |
if(portNumber > 7) portNumber = 7; | |
//Read the current mux settings | |
Wire.requestFrom(MUX_ADDR, 1); | |
if(!Wire.available()) return(false); //Error | |
byte settings = Wire.read(); | |
//Clear the wanted bit to disable the port | |
settings &= ~(1 << portNumber); | |
Wire.beginTransmission(MUX_ADDR); | |
Wire.write(settings); | |
Wire.endTransmission(); | |
return(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment