Mein erstes eigenes Netduino Projekt. Mit 6 LEDs bilde ich die Funktion eines Würfels nach.

Über den Onboard-Switch wird eine neue Runde eingeleitet. Es werden 2x nacheinander alle LEDs angeschalten um eine kleine Animation zu machen und danach wird das Ergebnis über die LEDs angezeigt. Zusätzlich wird im Debugger ausgegeben was der „Würfel gerade macht“.

Für Vorschläge was man besser / anders machen kann, bin ich natürlich immer offen.

Dice (737 Downloads)

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace _Test
{
    public class Dice
    {
        public static OutputPort led1 = new OutputPort(
            Pins.GPIO_PIN_D1,
            false);
        public static OutputPort led2 = new OutputPort(
            Pins.GPIO_PIN_D2,
            false);
        public static OutputPort led3 = new OutputPort(
            Pins.GPIO_PIN_D3,
            false);
        public static OutputPort led4 = new OutputPort(
            Pins.GPIO_PIN_D4,
            false);
        public static OutputPort led5 = new OutputPort(
            Pins.GPIO_PIN_D5,
            false);
        public static OutputPort led6 = new OutputPort(
            Pins.GPIO_PIN_D6,
            false);
        public static InputPort button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);

        public static int Result;
        public static int Round;

        public static void Main()
        {
            while (true) // do this "forever" :)
            {
                if (!button.Read())
                {
                    Round += 1;
                    Debug.Print("-----------------------");
                    Debug.Print("Round: " + Round);
                    Debug.Print("-----------------------");

                    Debug.Print("Clear all leds...");
                    ClearLeds();

                    Debug.Print("Go around: 1...");
                    LedCircle(100);
                    Debug.Print("Go around: 2...");
                    LedCircle(200);

                    Debug.Print("Dice!!!");
                    DisplayRandomNumber();
                    Debug.Print("Result: " + Result);

                }
            }
        }

        private static void LedCircle(int sleeptime)
        {
            led1.Write(true);
            Thread.Sleep(sleeptime);
            led1.Write(false);
            led2.Write(true);
            Thread.Sleep(sleeptime);
            led2.Write(false);
            led3.Write(true);
            Thread.Sleep(sleeptime);
            led3.Write(false);
            led4.Write(true);
            Thread.Sleep(sleeptime);
            led4.Write(false);
            led5.Write(true);
            Thread.Sleep(sleeptime);
            led5.Write(false);
            led6.Write(true);
            Thread.Sleep(sleeptime);
            led6.Write(false);
        }

        private static void DisplayRandomNumber()
        {
            Random rd = new Random();
            Result = 0;

            while (Result == 0) // 0 not allowed
            {
                Result = rd.Next(7);
            }

            switch (Result)
            {
                case 1:
                    led1.Write(true);
                    break;
                case 2:
                    led1.Write(true);
                    led2.Write(true);
                    break;
                case 3:
                    led1.Write(true);
                    led2.Write(true);
                    led3.Write(true);
                    break;
                case 4:
                    led1.Write(true);
                    led2.Write(true);
                    led3.Write(true);
                    led4.Write(true);
                    break;
                case 5:
                    led1.Write(true);
                    led2.Write(true);
                    led3.Write(true);
                    led4.Write(true);
                    led5.Write(true);
                    break;
                case 6:
                    led1.Write(true);
                    led2.Write(true);
                    led3.Write(true);
                    led4.Write(true);
                    led5.Write(true);
                    led6.Write(true);
                    break;
                default:
                    Debug.Print("Error: " + Result + " is bigger then 6!");
                    break;
            }
        }

        private static void ClearLeds()
        {
            led1.Write(false);
            led2.Write(false);
            led3.Write(false);
            led4.Write(false);
            led5.Write(false);
            led6.Write(false);
        }
    }
}