In this project we will be detecting the colour of 3 different Mega Blok colours (Red, Yellow and Green). We will be using an Arduino UNO connected to 2 LEDs (one Yellow and one Red LED) as light detectors, and an RGB LED to illuminate the subject. We will use a Photocell to account for varying ambient light levels.
The signals from the LED light sensors will be sent to a Processing.org program via a Serial command. The computer program will make use of my Neural Network to classify the pattern of results and hopefully provide the correct colour "answer". The program should change the colour of the computer screen background to coincide with the colour of the Mega Blok.
The Video
Parts Required:
- Arduino UNO...........x1
- Red LED .................x1
- Yellow LED.............x1
- 330 Ohm resistors... x 5 (for the LEDs)
- Photocell .................x1
- 10K Ohm resistor....x1 (for the Photocell)
- Around 11 wires and a Breadboard (or two) to put it all together
Here is the Fritzing Sketch: (made with Fritzing)
The Arduino Code
Load the following code into the Arduino.
arduino code Arduino: Colour Detector
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Define the pin for the PhotoResistor */
#define PhotoR_Pin 0
/* Define the pins for the Red LED Sensor */
#define Red_LED_Sensor_POS 4
#define Red_LED_Sensor_NEG 5
/* Define the pins for the Yellow LED Sensor */
#define Yellow_LED_Sensor_POS 7
#define Yellow_LED_Sensor_NEG 8
/* Define the pin for the RGB LED torch */
#define RGB_LED_RedPin 11
#define RGB_LED_GreenPin 10
#define RGB_LED_BluePin 9
/* Controls the brightness of the RGB LED */
int intensity=255;
/* Define the maximum cycles/time allowed for each LED to capture light */
long max_darkness=80000;
void setup(){
/* Setup the RED LED Sensor */
pinMode(Red_LED_Sensor_POS,OUTPUT);
digitalWrite(Red_LED_Sensor_POS,LOW);
/* Setup the YELLOW LED Sensor */
pinMode(Yellow_LED_Sensor_POS,OUTPUT);
digitalWrite(Yellow_LED_Sensor_POS,LOW);
/* No need to setup the RGB LED Pins */
/* Turn on Serial Protocol */
Serial.begin(9600);
}
void loop()
{
byte byteRead;
/* check if data has been sent from the computer: */
if (Serial.available()) {
/* read the most recent byte (which will be from 0 to 255): */
byteRead = Serial.read();
if(byteRead==0){
/* Turn off if the byte Read was 0 */
set_RGB_LED(0,0,0,false);
}else{
/* set the brightness of the LED and then take readings: */
set_RGB_LED(0,0,0,false);
photoR_Read();
set_RGB_LED(0,0,0,true);
set_RGB_LED(intensity,0,0,true);
set_RGB_LED(0,intensity,0,true);
set_RGB_LED(0,0,intensity,true);
}
}
}
void photoR_Read(){
int ambiLight = analogRead(PhotoR_Pin);
ambiLight = map(ambiLight, 0, 900, 0, 50);
ambiLight = constrain(ambiLight, 0, 50);
/* Print the Ambient light level to the serial port */
Serial.println(ambiLight);
}
void set_RGB_LED(int redInt, int greenInt, int blueInt, boolean takeReadings ){
/* set the brightness and colour of the RGB LED: */
analogWrite(RGB_LED_RedPin, redInt);
analogWrite(RGB_LED_GreenPin, greenInt);
analogWrite(RGB_LED_BluePin, blueInt);
/* If takeReadings is true - then take Readings. */
if(takeReadings){
/* Read the amount of Yellow light */
read_LED('Y', Yellow_LED_Sensor_NEG);
/* Read the amount of Red light */
read_LED('R', Red_LED_Sensor_NEG);
}
}
void read_LED(char LED_Colour, int LED_Pin){
/* Charge the LED by applying voltage in the opposite direction */
pinMode(LED_Pin,OUTPUT);
digitalWrite(LED_Pin,HIGH);
/* Read the amount of Light coming into the LED sensor */
long darkness=0;
int lightLevel=0;
pinMode(LED_Pin,INPUT);
digitalWrite(LED_Pin,LOW);
while((digitalRead(LED_Pin)!=0) && darkness < max_darkness){
darkness++;
}
lightLevel=((max_darkness-darkness)+1)/80;
/* Print the light level to the serial port */
Serial.println(lightLevel);
}
The Processing Code:
The processing code is very long:
Please visit this link to copy and paste the code into your Processing sketch.
http://www.openprocessing.org/visuals/?visualID=34210
Make sure to select "Source Code" when you get there: (as displayed below)
If you have any problems with accessing the code - please let me know in the comments section of this blog.
This sketch utilises a simple feed forward Neural Network (that I developed from scratch). For more detailed information about this neural network please navigate through my previous blog postings.
Neural Network
So there you go, a simple idea, a simple outcome, and a lot of "stuff" happening in the background.
I am sorry. This project is not basic, but hopefully someone out there will get some use out of it.
Have fun !
ScottC