19 August 2011

Poor Man's Colour Detector (Part 2) - The project

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
/* 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

22 comments:

  1. Great project, thank you !!!!
    I have read your ANN for processing and this project, but I have a question, you have this in your code >

    lightLevel=((max_darkness-darkness)+1)/80;

    and send it to PC using USB, in your neural network depending on the top buttons in your sketch will be the inputs right, but regarding the measured value, where did it go ? which part in you procesing sketch of your neural network ? I want to know how you use the measeured values in your neural network, you multiply it by what ? It is a little confusing, the value of the top buttons are inputs, but the inputs of the measured values where did it go ? Can you please answer my question, thanks

    ReplyDelete
    Replies
    1. Hi Anonymous,
      It has been a very long time since I have written this code... so had to refresh my memory on how this was pieced together. And looking through it, I can see how easy it is to get entangled in the code. Knowing what I know now, I probably would have approached it a little differently, but I will try my best to explain what is happening.

      Step 1: You press the top buttons, which sends a signal to the Arduino to shine the RGB LED, and it in turn will send back 3 values. One for the ambient light taken by the photoresistor:

      Serial.println(ambiLight);

      One for the Yellow LED, and one for the Red LED.
      Serial.println(lightLevel);

      Ok - Now processing will be listening for the return of 3 values, which it will place into an array called sensorReadings.
      This is achieved by the "void serialEvent (Serial myPort)" function.
      Once it has all 3 values, it will then process these values in one of 2 ways. If the "detecting" boolean value is true, it will use the neural network to try and detect the colour. If it is false, then it will send these readings to a file "outputFile1", as well as sending the expected results to "outputFile2". The expected results are pre-defined and based on the button pressed.

      So first you need to go through an show it what ambient looks like, what a yellow block looks like, what a green block looks like etc etc by pressing the top buttons. Once these readings are saved in a file. You press the train network button. This loads the values from outputFile1 and 2, into the neural network. Once trained, you can then test it out by trying to detect.
      Bear in mind, you may need to experiment under varying light conditions. For example, if you train the network at night time, it may have trouble during the day, but it is pretty quick to retrain.

      Delete
    2. Hello , I'm a universitary student and I'm trying to implement your neural network on Arduino one. I have a problem, having trained the neural network , when I go to detect colors, stops on the measurement. Also it generates the two output files. Sorry for the little English properly!

      Delete
    3. Hi Gianluca - Sorry - how did you train the neural network without detecting the colours? Sorry I am a bit confused.

      Delete
  2. I need the executable where cruise buttons red, green, yellow etc. or I can explain how I do it?

    ReplyDelete
    Replies
    1. Hi Luis,
      Am not too sure I understand what you are asking. Can you please try again?

      Delete
  3. Sir,

    Good Day I just want to ask how to make this work..

    This is how I do it

    1st I create the circuit

    2nd I input the codes into my arduino..

    The question is how I could add the Processing Code..?

    Please help.. I'm a beginner in using arduino. Thank you for your kind consideration.

    ReplyDelete
    Replies
    1. Hi Ash - you chose an interesting project for a beginner.
      Processing is a program that you run on your computer. The processing program communicates to the Arduino via USB (serial COM port).
      Basically, the Arduino and LEDs are the eyes of this project, and your computer is used as the brain (neural network).
      So step 3 is download the processing IDE from here
      And copy and paste the code from the link here - making sure to select the code tab as indicated in the instructions above, and paste the code into the processing IDE.

      Make sure that your Arduino is connected to the computer via USB.
      Ensure that you close any Serial Monitors that may be open. Do this before you run the processing code.

      Step 4 - train the neural network as shown in the video - you need to place a red coloured megablok over the LEDs when training with the red button, etc etc.

      Step 5:
      Once trained - you can then see how effective your neural network is by trying to detect each of the colours.

      Tip: try an keep the lighting conditions consistent... for example, if you train it in bright sunlight, don't expect it to detect your colours in your room at night. Ambient light is the biggest problem you will face in terms of consistent results.
      But I guess if you train the network under different conditions, you may bet better results. It requires a bit of trial and error,

      This project was one of my first, and was done out of curiosity, which probably explains the lack of detail.

      Regards
      Scott

      Delete
  4. hi Scott, this question my rack your brain a bit, but here goes. Is it possible to adapt the Processing code so it can run on an Arduino ? I would like to keep the NN on my mobile platform, without the need for a PC connection.

    regards, Gert

    ReplyDelete
    Replies
    1. Hi Gert,

      It would be easier to start from scratch than it would be to adapt the code. However, it is possible to develop a NN on an Arduino without Processing. I used the processing language because it allowed me to take advantage of my computer's processing power and memory. It also allowed me to create a GUI without much effort.
      Hopefully there is enough explanation within these tutorials to get you on the right track, however there are many others who have done what you are trying to do... a google search is likely to get you there pretty quickly.
      Good luck

      Delete
  5. Hello project is super great but I have a problem and I need your help please look at that when I run the code prossesig the window that appears to me is a tiny gray buttons bottomless help me it is urgent for my project dime electronics to modify or do it then appears my window with buttons

    ReplyDelete
    Replies
    1. Hi Merquiades,
      I made this tutorial in 2011. It is very likely that the Processing version was different back then, and may not work with the current version. The processing website has many tutorials on how to create buttons etc etc, and once you have the GUI set up, the rest should be easy to adapt.

      Delete
  6. Hello Scott, your projects are great and i love them. I am new with processing/arduino. I have problem with processing code> line 240> class BufferedWriter bw=null;> reason:unexpected token bw. I would be grateful for any assistance.

    Regards, Angel

    ReplyDelete
    Replies
    1. Not sure where you are seeing this line in this tutorial ?

      Delete
  7. Hey Scott, everything works perfect except detecting colours, where do u put colour objects in photocell or on leds? always output is unknown colour.

    ReplyDelete
    Replies
    1. MegaBlock covers the LEDs. Needs to be trained before it will work.

      Delete
  8. Hi Scott,
    i made project like ur sketch but only rgb works, when i click to read red/yellow button, red/yellow leds doesnt activate to send signals to photocell, where did i make mistake? TNX

    ReplyDelete
    Replies
    1. Hi Scott, can you tell me how can I run your project on new Processing? I got a few errors, and after I tried to deal with them I got some strange characters on buttons. I'm used to CPP, so I think with a little help I will be able to run this project. The understanding of your project is crucial for me, because I need to build something simillar to what you did. Best regards, and looking forward to hearing from you.

      Delete
    2. Hi Unknown,

      I have not looked at this project for about 5 years...
      There are two options....
      1) Use an older version of Processing
      2) Post your new code (with edits etc) into a new thread in my Forum - you will see the link to the forum at the top of this page. Post what you have done, and show with pictures, screen shots or just describe what you are experiencing with the code you have... we can then work through the issues...

      Due to my limited time - it may take me a while to respond... but we will get there.

      Delete
  9. hey dude i have one question
    what are the Poor Man's Colour Detector Program have a neural network logic ?
    i can find the logic in the program

    ReplyDelete
    Replies
    1. Hi Muhammad,
      Not sure what the question is exactly.
      But yes - there is a neural network. And that code is within the Processing Sketch. You need to run the Processing sketch within the Processing IDE on your computer as described above. The processing sketch will communicate with the Arduino.
      See the video for the project in action.

      Delete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked in the ArduinoBasics forum.

Comments are moderated due to large amount of spam.