Exemple #1
0
        public static void Main()
        {
            // Outputs:
            // - Buzzer (GPIO D3)
            // - Segment7 (7-Segment display/74HC595 IC) (SPI D4, D7, D8)
            // - Led1 (PWM D5)
            // - Led2 (PWM D6)

            #region "Output definitions"
            // The buzzer is connected directly to GPIO pin D3
            BitBangBuzzer Buzzer = new BitBangBuzzer(Pins.GPIO_PIN_D3);

            // The 7-segment display is connected with a 74HC595 bitshift IC over GPIO pins D4 (MOSI), D7 (CS) and D8 (SCLK)
            Ic74hc595    Mux      = new Ic74hc595(Pins.GPIO_PIN_D8, Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D7);
            SevenSegment Segment7 = new SevenSegment(Mux.CreateParallelOut());
            // The DangerShield has the digits defined differently; these bits are used:
            //    Top = 1
            //    UpperRight = 2
            //    LowerRight = 3
            //    Bottom = 4
            //    LowerLeft = 5
            //    UpperLeft = 6
            //    Middle = 7
            //    Dot = 8
            Segment7.ChangeSignals(new byte[] { //    (87654321)
                0x3f,                           // 0 brights up: 0 1 2 3 4 5    (00111111)
                0x06,                           // 1 brights up: 1 2            (00000110)
                0x5b,                           // 2 brights up: 0 1 3 4 6      (01011011)
                0x4f,                           // 3 brights up: 0 1 2 3 6      (01001111)
                0x66,                           // 4 brights up: 1 2 5 6        (01100110)
                0x6d,                           // 5 brights up: 0 2 3 5 6      (01101101)
                0x7d,                           // 6 brights up: 0 2 3 4 5 6    (01111101)
                0x07,                           // 7 brights up: 0 1 2          (00000111)
                0x7f,                           // 8 brights up: 0 1 2 3 4 5 6  (01111111)
                0x6f,                           // 9 brights up: 0 1 2 3 5 6    (01101111)
                0x00,                           // all go down: 0 1 2 4 5 6 7   (00000000)
            });
            Segment7.ChangeDotSignal(8);

            // Both leds
            IPWMPort Led1 = new Netduino.PWM(Pins.GPIO_PIN_D5);
            Led1.StartPulse();
            IPWMPort Led2 = new Netduino.PWM(Pins.GPIO_PIN_D6);
            Led2.StartPulse();
            #endregion

            // Inputs:
            // - PotentioMeter1 (ADC A0)
            // - PotentioMeter2 (ADC A1)
            // - PotentioMeter3 (ADC A2)
            // - Photocell (ADC A3)
            // - TemperatureSensor (ADC A4)
            // - KnockSensor (ADC A5)
            // - PushButton1 (GPIO D10)
            // - PushButton2 (GPIO D11)
            // - PushButton3 (GPIO D12)

            #region "Input definitions"
            // Potentio meters
            IADCPort PotentioMeter1 = new Netduino.ADC(Pins.GPIO_PIN_A0);
            PotentioMeter1.RangeSet(0, 100); // Same range as Led1.SetDutyCycle()
            IADCPort PotentioMeter2 = new Netduino.ADC(Pins.GPIO_PIN_A1);
            PotentioMeter2.RangeSet(0, 100); // Same range as Led2.SetDutyCycle()
            IADCPort PotentioMeter3 = new Netduino.ADC(Pins.GPIO_PIN_A2);
            PotentioMeter3.RangeSet(0, 9);   // Same range as Segment7.SetDigit()
            // Photocell
            IADCPort Photocell = new Netduino.ADC(Pins.GPIO_PIN_A3);
            Photocell.RangeSet(0, 100); // Same range as Led2.SetDutyCycle()
            // Temperature Sensor
            Tmp36 TemperatureSensor = new Tmp36(new Netduino.ADC(Pins.GPIO_PIN_A4));
            // Knock Sensor
            IADCPort KnockSensor = new Netduino.ADC(Pins.GPIO_PIN_A5);
            KnockSensor.RangeSet(0, 200); // Bigger range as Led1.SetDutyCycle() but you really need to smash hard to reach this value
            // Push buttons
            InputPort PushButton1 = new InputPort(Pins.GPIO_PIN_D10, false, Port.ResistorMode.Disabled);
            InputPort PushButton2 = new InputPort(Pins.GPIO_PIN_D11, false, Port.ResistorMode.Disabled);
            InputPort PushButton3 = new InputPort(Pins.GPIO_PIN_D12, false, Port.ResistorMode.Disabled);
            #endregion

            // This value contains which demo is currently active
            int Demo = 1;

            // Contains the last second, so we can switch between two numbers on the 7-segment display (to display the temperature)
            int LastSecond = Utility.GetMachineTime().Seconds;

            // This digit should be currently shown
            bool ShowSecondDigit = false;

            // Infinite loop
            while (true)
            {
                // Switches the demo, when required (NOT statement because of the pullup resistors)
                if (!PushButton1.Read())
                {
                    Demo = 1;
                }
                if (!PushButton2.Read())
                {
                    Demo = 2;
                }

                if (Demo == 1)
                {
                    // First demo is currently active
                    Led1.SetDutyCycle((uint)PotentioMeter1.RangeRead());
                    Led2.SetDutyCycle((uint)PotentioMeter2.RangeRead());
                    Segment7.SetDigit((byte)PotentioMeter3.RangeRead());
                }
                else
                {
                    // Second demo is currently active
                    uint Knocking = (uint)KnockSensor.RangeRead();
                    Led1.SetDutyCycle((uint)(Knocking > 100 ? 100 : Knocking)); // We want to limit to 100
                    Led1.SetDutyCycle((uint)KnockSensor.RangeRead());
                    Led2.SetDutyCycle((uint)Photocell.RangeRead());

                    // Okay, we want two temperature digits seprated, for display purposes
                    float Temp   = TemperatureSensor.Temperature;
                    byte  Digit1 = (byte)(Temp / 10);
                    byte  Digit0 = (byte)(Temp - (10 * Digit1));

                    // Switch the digit to be displayed
                    if (LastSecond != Utility.GetMachineTime().Seconds)
                    {
                        LastSecond = Utility.GetMachineTime().Seconds;
                        Segment7.SetDigit(ShowSecondDigit ? Digit1 : Digit0);
                        Segment7.SetDot(!ShowSecondDigit);
                        ShowSecondDigit = !ShowSecondDigit;
                    }
                }


                // Links the buzzer to the 3rd pushbutton's value
                Buzzer.Write(!PushButton3.Read());
            }
        }
Exemple #2
0
        public static void Main()
        {
#if SD_ENABLED
            // If your Netduino can't execute the next line of code, make sure you got at least firmware 4.1.1 beta 1
            // See also: http://forums.netduino.com/index.php?/topic/1592-netduino-firmware-v411-beta-1/
            StorageDevice.MountSD("SD", SPI_Devices.SPI1, Pins.GPIO_PIN_D10);

            // Determines the filename
            string filename = "";
            int    index    = 0;
            do
            {
                filename = @"\SD\LOGGER" + Tools.ZeroFill(index, 2) + ".CSV";
                ++index;
            }while (File.Exists(filename));

            // Starts writing to the file
            FileStream   stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream);

            // Writes file headers
            writer.WriteLine("ticks,datetime,light,temp");
#endif

            // LEDs
            OutputPort red   = new OutputPort(Pins.GPIO_PIN_D2, false);
            OutputPort green = new OutputPort(Pins.GPIO_PIN_D3, false);

            // An analog light sensor
            IADCPort light = new Netduino.ADC(Pins.GPIO_PIN_A0);
            light.RangeSet(0, 1024);
            // An analog temperature sensor
            Tmp36 temperature = new Tmp36(new Netduino.ADC(Pins.GPIO_PIN_A1));

            // Time module (comment out SetTime once, to set the clock)
            DS1307 time = new DS1307();

            /*time.SetTime(
             *  Day: 11,
             *  Month: 8,
             *  Year: 2012,
             *  Hour: 12,
             *  Minute: 0,
             *  Second: 0
             * );*/
            time.Synchronize();

            while (true)
            {
                // Green status LED ON
                green.Write(true);

                // Builds the output
                string output = "";
                output += DateTime.Now.Ticks.ToString() + ", ";
                output += DateTime.Now.ToString() + ", ";
                output += light.RangeRead().ToString() + ", ";
                output += temperature.Temperature.ToString();

                // Prints the output to the debugger
                Debug.Print(output);
#if SD_ENABLED
                // Writes the output to the SD buffer
                writer.WriteLine(output);
#endif

                // Green status LED OFF, Red status LED ON
                green.Write(false);
                red.Write(true);

#if SD_ENABLED
                // Flushes the buffers to the SD card
                writer.Flush();
                stream.Flush();
#endif

                // Red status LED OFF
                red.Write(false);

                // Sleeps for a second
                Thread.Sleep(1000);
            }
        }