You are viewing an older revision! See the latest version

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 contains another function that clear all of the screen.

ClearAllScreen()

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

Game Ideas With Multiple Screens


All wikipages