Harry Potter Sorting Hat – Code Overview

Once all of the sensors were integrated into a single circuit, all of the separate test code for the sensors (among other things) needed to be written into a single cohesive program. The sensors and piezo buzzers were controlled by the Arduino microcontroller. As such, much of the code was written in the Arduino IDE.

Serial communication was first established between the Arduino and A3BU boards. The A3BU code would initialize and set itself up using the USART framework. The A3BU code had a continuous loop where it would wait to receive a byte (character) on its RxD pin on J1. If the Arduino sent a ‘G’, the A3BU would then write “Gryffindor” to its LCD screen using the GFX Monochrome library. If it received an ‘S’, it would write “Slytherin,” and etc. There were 4 characters for each of the 4 houses.

In the Arduino code, it first entered a while loop which would iterate until the IR distance sensor detected the hat was placed on a person’s head. Afterwards, the color sensor was triggered. It initialized itself and read in the RGB values (luminance was calculated based on these). It passes the calculated luminance value to a function, which returned a single character corresponding to the house (‘G’ = Gryffindor, ‘S’ = Slytherin, ‘R’ = Ravenclaw, ‘H’ = Hufflepuff). This character was sent to the A3BU board using the Serial.print() function. At this point, it would call a function to trigger the piezo buzzers for play the Harry Potter tune to indicate that the mighty hat had sorted the wearer into a house. The wearer would then take the hat off and read their house on the A3BU’s LCD screen (as well as enjoy that sweet tune played by the piezo buzzers).

A3BU Xplained Code: 

#include <asf.h>
#include <conf_usart_example.h>
#include <delay.h>
#include <gfx_mono.h>
#include <sysfont.h>

int main (void)
{
       board_init();
       sysclk_init();
       gfx_mono_init();

       // USART options.
       static usart_rs232_options_t USART_SERIAL_OPTIONS = {
              .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE,
              .charlength = USART_SERIAL_CHAR_LENGTH,
              .paritytype = USART_SERIAL_PARITY,
              .stopbits = USART_SERIAL_STOP_BIT
       };

       // Initialize USART driver in RS232 mode
       usart_init_rs232(USART_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS);

       gpio_toggle_pin(NHD_C12832A1Z_BACKLIGHT);

     
       uint8_t received_byte;
       char character;

       while(true)
       {
              received_byte = usart_getchar(USART_SERIAL_EXAMPLE);
              character = (int)received_byte;

              gfx_mono_init();

              if(character == 'G')
              {
                     gfx_mono_draw_string("Gryffindor!", 0, 0, &sysfont);
              }else if(character == 'S')
              {
                     gfx_mono_draw_string("Slytherin!", 0, 0, &sysfont);
              }else if(character == 'H')
              {
                     gfx_mono_draw_string("Hufflepuff!", 0, 0, &sysfont);
              }else if(character == 'R')
              {
                     gfx_mono_draw_string("Ravenclaw!", 0, 0, &sysfont);
              }else
              {
                     gfx_mono_draw_string("Hat is confused...", 0, 0, &sysfont);
              }
       }
}

Sample Arduino Uno Code:

void setup() //setup is called automatically
{
    Serial.begin(9600); //Initialize baud rate to 9600
    pinMode(1, OUTPUT); //set TxD pin to output for A3BU communication
}

void loop() //Arduino calls this function automatically continuously (main function)
{
    //Until IR sensor detects the hat is put on, do not go further
    distanceReading = analogRead(2);
    while( distanceReading < 350 ) //350 is the distance from IR sensor to base of boxhat
    {
         distanceReading = analogRead(2); //read value form analog pin 2
    };
    Serial.println("Hat is put on...distance: " + distanceReading);

    
    Serial.println("Activating color sensor...");
    uint16_t luxValue = readColorSensor();    

    char houseCode = determineHouse(luxValue); 
    Serial.print(houseCode); //send the house code to the A3BU

    if(houseCode == 'G' || houseCode == 'S' || houseCode == 'H' || houseCode =='R')
    {
        playHPTune();
    }

    delay(5000);
}
uint16_t readColorSensor()
{
    if (tcs.begin()) 
    {
      Serial.println("Found sensor");
    } else 
    {
      Serial.println("No TCS34725 found ... check your connections");
      while (1);
    }     
    // Now we're ready to get readings!
    delay(1000);

    uint16_t r, g, b, c, colorTemp, lux;

    
    tcs.getRawData(&r, &g, &b, &c);
    colorTemp = tcs.calculateColorTemperature(r, g, b);
    lux = tcs.calculateLux(r, g, b);

    return lux;
}