Project Overview

Overview

The aim of this project is to create a working clock using FRDM-KL25Z and necessary components while also exploring the use of distance sensors as input (control pannel) and possible game ideas with the use multiple screens.

Clock

Distance Sensors

Setup

We're using tobeaddedlater as the distance sensor. Using this MaxSonar library, one can quickly get the sensor up and running. The library contains the MaxSonar class with an example of the setup:

MaxSonar Example

#include "mbed.h"
#include "MaxSonar.h "

int main() {
    MaxSonar *range;
    float r;

    // Create and configure object for 3.3V powered LV-series device, 
    // accessed with analog reads (in cm) on p16, triggered by p7.
    range = new MaxSonar(MS_LV, MS_ANALOG, p7, p16);
    range->setVoltage(3.3);
    range->setUnits(MS_CM);

    while(1) {
        // Trigger read, wait 49ms until ranger finder has
        // finished, then read. 
        range->triggerRead();
        wait_ms(49);
        r = range->read();
      
        // Print and delay 0.5s.
        printf("Range: %.3f cm\n", r);
        wait(0.5);
    }
}

Connect Vcc to 3.3V; Vss to ground; Tx to a digital pin on the mbed (this is the trigger pin - p7 in the code above); And the analog pin to an analog pin on the mbed (this p16 in the code above).

This project make use of 2 distance sensors and for ease of use, a function to update the ranges on the two sensor was created.

UpdateRange

void UpdateRange()
{
    // Trigger read, wait 49ms until ranger finder has
    // finished, then read. 
    range1->triggerRead();
    wait_ms(49);
    r1 = range1->read();
        
    range2->triggerRead();
    wait_ms(49);
    r2 = range2->read();
}

This function trigger the two distance sensors and store the readings in and int type variable - r1 and r2.

Distance Sensors as input

This device is crucial to the build in games for the clock. To demonstrate this, lets look at the code for the game option menu and see how one can use two distance senor to select and run the game.

Game Menu

void GameOption()
{
    //Clear all screens (so that all of the screens are blank)
    ClearAllScreen(); 
    
    //Display the selected game on "questionScreen"
    UpdateScreen(questionScreen, "Select a game:  >" + gameName[gameOptionLocation]);
    
    bool gameOption = true;    
    while(gameOption)
    {
        //trigger the distance sensors to take a reading and store them in r1 and r2
        UpdateRange();
        
        if(r1 < 25 && r2 < 25) //select
        {
            UpdateScreen(questionScreen, "Entering " + gameName[gameOptionLocation] + ".....");
            wait(1);
            
            gameOption = false;
        }        
        else if(r1 < 20) //down
        {
            if(gameOptionLocation > 0) { gameOptionLocation--; UpdateScreen(questionScreen, "Select a game:  >" + gameName[gameOptionLocation]); }
        }
        else if(r2 < 20) //up
        {
            if(gameOptionLocation < 1) { gameOptionLocation++; UpdateScreen(questionScreen, "Select a game:  >" + gameName[gameOptionLocation]); }
        }
    }
    
    StartGame(gameOptionLocation);
}

We will ignore most of this code and concentrate only on the conditional statements. The idea is, to navigate the game option - to navigate the list of game in the option menu. We can use distance sensor no.1 to "scroll" up the list when we hover over it and similarly, scroll down with the sensor no.2. Once the desired game has been displayed, we can select the game and enter it by hovering over both of them at the same time.

If you're interested in the ClearAllScreen() function and/or the UpdateScreen() function see the Multiple Screens part of this page.

Multiple Screens

Setup

For this project, we're using ToBoAddedLater for the screen and are making use of this TextLCD library. The library also has a link to a "Hello World" program which can be imported.

Hello World: main.cpp

// Hello World! for the TextLCD
 
#include "mbed.h"
#include "TextLCD.h"
 
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
 
int main() {
    lcd.printf("Hello World!\n");
}

The pin configuration can be found from here.

In this project make use of three screens, and enable pins and three and gates (we actually use a combination of nand and not gates instead) to make multi-screen possible. The screen only update if the enable line is set to high (3.3V), it is therefore possible to select which screen to update by setting the state of the enable line.

A function has been written to make this process easier

UpdateScreen()

void UpdateScreen(DigitalOut screen, string text)
{
    //disable all E pin for all screens
    questionScreen = 0;
    screen1 = 0;
    screen2 = 0;
        
    //enable E pin for the screen that we want to update
    screen = 1;
    
    //convert text to char array
    char text_char_array[1024];
    strcpy(text_char_array, text.c_str());
    //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
    for(int i = 0; i < 10; i++)
    {
        lcd.cls();
        lcd.printf(text_char_array);
    }
}

The first parameter is the screen that we want to update and the second parameter is the text that we want to display on to the screen.

This project also contain another function that clear all of the screen.

ClearAllScreen()

void ClearAllScreen()
{
    questionScreen = 1;
    screen1 = 1;
    screen2 = 1;
    
    lcd.cls();
}

Game Ideas With Multiple Screens

Quiz Game

An obvious game is a quiz, the project contains a game called Multi-Maths. The game generate a maths question on the questionScreen (main screen) as well and two answers - one on screen1 and the other on screen2. To select the answer on screen1, simple hover over the distance sensor next to screen1 (similarly to screen2). The question screen will then tell you if the answer you've choose is correct or incorrent.

You can also return to the game selection menu by hovering over both distance sensor. An "are you sure you want to exit?" type menu will show up and by hovering over one distance sensor or another, you can toggle between "yes" and "no". Hover over both distance sensors again to confirm.

Pong!

Another idea is to have a pong game but with a slight twist. Screen1 and screen2 are positioned such that screen1 is above screen2 and there are some gaps in between them. We could imaging that the pong board is the space on the screen and also the space in-between in the 2 screens. Because of this, you will only see the ball bouncing at the ends of the board and using this limited information, you can guess the position of the ball in the empty space between the screen.

The player "paddle/board" is controlled by two distance sensors. Hovering over the sensor on the left will move the paddle to the left and similarly for the right side.

You can exit this game by hovering over both distance sensors at the same time.


All wikipages