Programming Method

3.1. I /O method

According to our device, PortB and PortD are used for output while portA is used for input. The C command DDRX = 0xff; is used to set PortX as output.

3.2 methods for LED to light up and toggle

To light up / turn off the LED, the main theory is to set the corresponded pin to high / low. A delay method is used to set the frequency for the LED to change colour. To turn on the LED, the OR logic and the shift method is used. To turn off the LED, the AND logic is used. Below are the common C codes for turn on and turn off the LED.
To turn on pin number x of PORTD: PORTD|= (1<<PDx);
To turn off pin number x of PORTD: PORTD&= (0<<PDx);

3.3 Functions for the LCD display

i) LCD Initialization function The LCD initialization function int_LCD(void) method is defined to initialize the LCD display. The function consists of four parts: function sets, display on/off control, entry mode set and clear display. ii). LCD write function We have defined the function LCD_write (unsigned char data) to display a single character on the display screen.

3.4 The Analogue to Digital method

The power supply voltage we are using delivers 5V. If we connect AVCC to the same supply voltage, the analogue input is capable of delivering digital values corresponding to analog inputs up to 5V. The analogue to digital converter is a 10 bit converter. That means it has 2^10=1024 digital values to convert to 0 to 5V range, 0 corresponding to 0V, 1024 to 5V.

3.5 The main function

The overall logic is our main function. First, the LCD screen is initialized and the analogue-to-digital converter (ADC) is also initialized. After that, the red light is turned on and the delay time is set, for the ADC to capture the voltage values from the phototransistors. The for-loop is used to calculate the average of 50 different values to receive a more trustworthy result. Secondly, red light is turned off and blue light is turned on. The for-loop was gone through again to calculate the value. In the end, the two results are compared against a certain voltage threshold, if both of the results are below the threshold, the screen will display "not recognized". If wither of both of the value are above the threshold, the screen will display the name of the colour which is corresponded to the higher value, e.g. red.