Digital Dice

10 Jun 2009

Hi there,

I work in the IT dept. Netherhall School and was given an mbed to play with by Ali Wells who I believe met up with some of you a few weeks back. I've been playing for a while so didn't think I would suit the first 5 minutes thread but I could do with some help in getting the Digital Dice example in the Cookbook working if anybody can help?

We have built a circuit with the mbed, 7seg display and triple axis accelerometer on a breadboard that matches the example photo as closely as possible (although i've had to use different pins as my mbed chip is different). I've used the code given with the pin numbers changed and BusOut to make it compile but the output results are completely wrong.

 

#include "mbed.h"
// 7 Segment display characters
// SegChars[n] turns on the segments needed to display n
int SegChars[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71 };
// a=25, b=26, ... , DP=32
int pins[] = {21, 22, 23, 24, 25, 26, 27, 28};
BusOut Seg7(pins);
AnalogIn X(15);
AnalogIn Y(16);
AnalogIn Z(17);
float sample = 0.0;
int Xinc, Yinc, Zinc = 0;
int count = 0;
int main() {
    while(1) {
    // Read the X Channel
    sample = X;
    if ( sample < 0.5)
    {Xinc = ((0.5*100) - (sample*100));}
    else if ( sample > 0.93)
    {Xinc = ((sample*100) - (0.93*100));}
    // Read the Y Channel
    sample = Y;
    if ( sample < 0.5)
    {Yinc = ((0.5*100) - (sample*100));}
    else if ( sample > 0.93)
    {Yinc = ((sample*100) - (0.93*100));}
    // Read the Z Channel, and see if it exceeds some thresholds
    sample = Z;
    if ( sample < 0.4)
    {Zinc = ((0.4*100) - (sample*100));}
    else if ( sample > 0.75)
    {Zinc = ((sample*100) - (0.75*100));}
    // Add the XYZ increments to the global counter cycle by cycle
    if (Xinc > 0) {count++;Xinc--;}
    if (Yinc > 0) {count++;Yinc--;}
    if (Zinc > 0) {count++;Zinc--;}
    Seg7 = SegChars[(count%6)+1];
    // Loop delay
    wait(0.005);
    }
}

 

I'm using analog inputs 15, 16, 17 and Bus outputs 21-28 but the results just light up all 7 segments and the dp on the display with the onboard LEDs flashing in pairs.

Any idea where I've gone wrong? I'm not experienced in C programming at all so all help and/or criticism welcome!

 

thanks,

Joe Kitchen

ICT - Netherhall School

10 Jun 2009

When you say "flashing in pairs" do you mean like here: http://mbed.co.uk/handbook/DebuggingGettingStarted

I checked your programm and it seems that the bug is that the BusOut API has changed too.

That means if you look here http://mbed.co.uk/projects/libraries/svn/mbed/trunk/BusOut.h you can see that there are 2 constructors for the BusOut class:

  • The first one thakes up to 17 arguments, a list of pinns and a name. If not all arguments are given the nongiven pins will be set to NOT_CONNECTED and the name to "".
  • The second constructer demands an array of int with 16 values each a pin number or NOT_CONNECTED

In your progam you uses the second one.

It should work if you change:

int pins[] = {21, 22, 23, 24, 25, 26, 27, 28};

to

int pins[] = {21, 22, 23, 24, 25, 26, 27, 28, NOT_CONNECTED, NOT_CONNECTED, NOT_CONNECTED, NOT_CONNECTED, NOT_CONNECTED, NOT_CONNECTED, NOT_CONNECTED, NOT_CONNECTED};

or if you use the other constructor:

Change

int pins[] = {21, 22, 23, 24, 25, 26, 27, 28};
BusOut Seg7(pins);

to

BusOut Seg7(21, 22, 23, 24, 25, 26, 27, 28);

 

best regards,

Rolf Meyer

10 Jun 2009

Hi,

BusOut Seg7(21, 22, 23, 24, 25, 26, 27, 28);

is the prefered API :)

Simon

 

10 Jun 2009

Thanks very much for your help! The accelerometer seems to be working fine, moving the breadboard changes the display on the 7seg unit. Unfortunately I'm having some difficulty also with displaying the numbers.

At the moment moving the accelerometer makes the display show either a 0. or a backwards 9 with a dp. Is this to do with the maths involved or the order in which the pins have been connected to the display?

I have connected the pins as per my understanding of a low-res german schematic i found:

SegA - pin 21
SegB - pin 22
.....
Seg8(dp) - pin 28

I've tried a few combinations but no luck. I also can't seem to get it to choose between more than two numbers.

10 Jun 2009

Maybe you should just try to figure out how the display is working.

Normaly every bit on the BusOut stands for a LED in the Display.

It should be easy to figure out which bit is which LED, just try something like:

#include "mbed.h"
BusOut Seg7(21, 22, 23, 24, 25, 26, 27, 28);

int main() {
  while(1) {
    printf("1. LED\n");
    Seg7 = 1;
    wait(2);
  
    printf("2. LED\n");
    Seg7 = 2;
    wait(2);

    printf("3. LED\n");
    Seg7 = 4;
    wait(2);

    printf("4. LED\n");
    Seg7 = 8;
    wait(2);

    printf("5. LED\n");
    Seg7 = 16;
    wait(2);

    printf("6. LED\n");
    Seg7 = 32;
    wait(2);

    printf("7. LED\n");
    Seg7 = 64;
    wait(2);

    printf("8. LED\n");
    Seg7 = 128;
    wait(2);
  }
}

To see which led is active just have a look here http://mbed.co.uk/handbook/SerialPC. The printf command will print out the active led over the serial port to your pc.

When you know which LED is which you can simple add the numbers to combine them. For example 7 will be activate the LEDs controled by 1, 2 and 4. Now you can build Numbers from the LEDs and store the values for the number in this line in your program:

int SegChars[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71 }; 

And the math in your program simply defines which number value is passed to the display

best regards

Rolf Meyer