Brad Martin
Published © GPL3+

MiniSpec

Using an Arduino, light detecting resistors, and RGB LEDs, I made a fun to use, miniature spectrometer.

IntermediateShowcase (no instructions)2,090
MiniSpec

Things used in this project

Story

Read more

Schematics

MiniSpec

Using an Arduino, light detecting resistors, and RGB LEDs, I made a fun to use, miniature spectrometer.

Code

MiniSpec

Arduino
Using an Arduino, light detecting resistors, and RGB LEDs, I made a fun to use, miniature spectrometer.
int buttonVal, toggleVal, cal = 0; 
int sample = 1;
int buzzer = 13;
int toggle = 8; int button = 7;
int redPin = 9; int greenPin = 10; int bluePin = 11; //RGB Led
int goodPin = 5; int statusPin = 6;
int sensorPinOne = A0; //LDR
int sensorPinTwo = A1;
int cellOne[2][41]; int cellTwo[2][41];
double wavelengths[41] = {380, 390, 400, 410, 420, 430, 440, 450, 
						  460, 470, 480, 490, 500, 510, 520, 530, 
						  540, 550, 560, 570, 580, 590, 600, 610, 
						  620, 630, 640, 650, 660, 670, 680, 690, 
						  700, 710, 720, 730, 740, 750, 760, 770, 780};
double colors[41][3] = 
	{	{97,  0,  97}, {121, 0, 141}, {131, 0, 181}, {126, 0, 219}, {106, 0, 255}, {61,  0, 255}, {0,  0,  255}, {0,  70, 255}, 
		{0, 123, 255}, {0, 169, 255}, {0, 213, 255}, {0, 255, 255}, {0, 255, 146}, {0,  255,  0}, {54,  255, 0}, {94,  255, 0}, 
		{129, 255, 0}, {163, 255, 0}, {195, 255, 0}, {218, 255, 0}, {255, 255, 0}, {255, 223, 0}, {255, 190, 0}, {255, 155, 0}, 
		{255, 119, 0}, {255,  79, 0}, {255,  33, 0}, {255,  0,  0}, {232,  0,  0}, {210,  0,  0}, {188,  0,  0}, {165,  0,  0}, 
		{143,  0,  0}, {237,  0,  0}, {219,  0,  0}, {200,  0,  0}, {181,  0,  0}, {161,  0,  0}, {141,  0,  0}, {119,  0,  0}, {97,   0,  0}	};
float transmittance[2]; float absorbance[2];
unsigned long previousMillis = 0; const long statusInterval = 300; //Debouncing

void setup() {
	Serial.begin(9600);
	pinMode(buzzer, OUTPUT);
	pinMode(toggle, INPUT); pinMode(button, INPUT);
	pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);
	pinMode(goodPin, OUTPUT); pinMode(statusPin, OUTPUT);
	pinMode(sensorPinOne, INPUT); pinMode(sensorPinTwo, INPUT);
	Serial.println("--- MiniSpec ---"); Serial.println();
	tone(buzzer, 400, 150);
}

void loop() {
	toggleVal = digitalRead(toggle);
	analogWrite(goodPin, HIGH); analogWrite(statusPin, LOW);
	setColor(0, 0, 0);
	
	if (toggleVal == 0) {
		if (digitalRead(button) == 1) {
			stabalize();
			analogWrite(goodPin, LOW); 
			for (int x=0; x<41; x++) {
				analogWrite(statusPin, HIGH);
				setColor(colors[x][0], colors[x][1], colors[x][2]); delay(333);
				cellOne[0][x] = analogRead(sensorPinOne); cellTwo[0][x] = analogRead(sensorPinTwo); delay(10);
			}
			Serial.println("Calibration Complete"); Serial.println();
			cal++;
		}
	}

	if (toggleVal == 1) {
		if (digitalRead(button) == 1 && cal >= 1) {
			stabalize();
			analogWrite(goodPin, LOW); 
			for (int x=0; x<41; x++) {
				analogWrite(statusPin, HIGH);
				setColor(colors[x][0], colors[x][1], colors[x][2]); delay(333);
				cellOne[1][x] = analogRead(sensorPinOne); cellTwo[1][x] = analogRead(sensorPinTwo); delay(10);
			}
			setColor(0,0,0);
			analysis();
		} else if (digitalRead(button) == 1 && cal == 0) {tone(buzzer, 100, 200);}
	}
}

void stabalize(void) {
	setColor(255,255,255);
	for (int y=0; y<5; y++) {
		analogWrite(goodPin, LOW); delay(300);
		analogWrite(goodPin, HIGH); delay(300);
	}
}

void setColor(double red, double green, double blue) {
	analogWrite(redPin, (int)red);
	analogWrite(greenPin, (int)green);
	analogWrite(bluePin, (int)blue);
}

void analysis(void) {
	Serial.println(); Serial.print("Sample "); Serial.println(sample); Serial.println();
	previousMillis = millis();
	for (int x=0; x<41; x++) {
		transmittance[0] = (float)cellOne[1][x]/(float)cellOne[0][x];
		transmittance[1] = (float)cellTwo[1][x]/(float)cellTwo[0][x];
		absorbance[0] = log10(1/transmittance[0]);
		absorbance[1] = log10(1/transmittance[1]);
		float avgAbsorbance = ((absorbance[0] + absorbance[1])/(float)2);
		Serial.println(avgAbsorbance);
		delay(100);
		if (millis() - previousMillis >= statusInterval) {
			analogWrite(statusPin, LOW);
		}
		if (millis() - previousMillis >= statusInterval*2) {
			analogWrite(statusPin, HIGH);
			previousMillis = millis();
		}
	}
	sample++;
}

Credits

Brad Martin

Brad Martin

2 projects • 2 followers
I like making gizmos

Comments