Пример #1
0
 //static void button_OnInterrupt(uint data1, uint data2, DateTime time)
 //{
 //    if (data2 == 0)
 //    {
 //        myservo.SetPulse(period, angleBottom);
 //    }
 //}
 static void backAndForth(PWM myservo, uint period, uint angleTop, uint angleBottom)
 {
     while (true)
     {
         myservo.SetPulse(period, angleBottom);
         Thread.Sleep(1000);
         myservo.SetPulse(period, angleTop);
         Thread.Sleep(1000);
         myservo.SetPulse(period, angleBottom);
     }
 }
        public static void Main()
        {
            uint watchdogTimer = 1000;

            PWM umbrella = new PWM(Pins.GPIO_PIN_D10); //Right controller

            Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            receiveSocket.Bind(new IPEndPoint(IPAddress.Any, 4444));
            byte[] rxData = new byte[10]; // Incoming data buffer
            double raw_speed = 0;

            while (true) /* Main program loop */
            {
                /* Try to receive new data - spend 100uS waiting */
                if (receiveSocket.Poll(100, SelectMode.SelectRead))
                {
                    int rxCount = receiveSocket.Receive(rxData);
                    watchdogTimer = 0;
                }

                if (watchdogTimer < 200)   // Only enable the robot if data was received recently
                {
                    // 900 (full rev) to 2100 (full fwd), 1500 is neutral

                    raw_speed += (rxData[0] - 127.5) * .001; // Add the value of the stick to the current speed
                    // Mediate added speed to negative if it's below center line(on ipgamepad). Make the added speed very little because the mount of UDP packets is large.
                    // map function only accept input between 0-255
                    if (raw_speed < 0)
                    {
                        raw_speed = 0;
                    }
                    else if (raw_speed > 255)
                    {
                        raw_speed = 255;
                    }
                    // Stick maintains speed unless calibrate changes.
                    umbrella.SetPulse(20000, map((uint)raw_speed, 0, 255, 1500, 2100)); // Right controller 1500-2100 -- only positive

                    watchdogTimer++;
                }
                else
                {
                    // Disable the robot
                    umbrella.SetDutyCycle(0);
                }
            }
        }
Пример #3
0
        public static void Main()
        {
            // write your code here
            var scale = new System.Collections.Hashtable
            {
                { "c", 1915u },
                { "d", 1700u },
                { "e", 1519u },
                { "f", 1432u },
                { "g", 1275u },
                { "a", 1136u },
                { "b", 1014u },
                { "C", 956u },
                { "D", 851u },
                { "E", 758u },
                { "h", 0u }
            };

            int beatsPerMinute = 90;
            int beatTimeInMilliseconds = 6000 / beatsPerMinute;
            int pauseTimeInMillisenconds = (int)(beatTimeInMilliseconds * 0.1);

            string song = "C1C1C1g1a1a1g2E1E1D1D1C2u";

            PWM speaker = new PWM(Pins.GPIO_PIN_D5);

            for (int i = 0; i < song.Length; i += 2)
            {
                string note = song.Substring(i, 1);
                int beatCount = int.Parse(song.Substring(i + 1, 1));

                uint noteDuration = (uint)scale[note];
                speaker.SetPulse(noteDuration * 2, noteDuration);
                Thread.Sleep(beatTimeInMilliseconds * beatCount - pauseTimeInMillisenconds);

                speaker.SetDutyCycle(0);
                Thread.Sleep(pauseTimeInMillisenconds);
            }

            Thread.Sleep(Timeout.Infinite);
        }
Пример #4
0
        public static void Main()
        {
            // Create servo object
            PWM servo = new PWM(Pins.GPIO_PIN_D9);

            // Create an input variable that represents the potentiometer
            AnalogInput pot = new AnalogInput(Pins.GPIO_PIN_A0);
            // Set range to fit the pulse width range for position
            pot.SetRange(750, 2250);

            while (true)
            {
                // Update position reading
                int position = pot.Read();
                // Set position based on reading
                servo.SetPulse(20000, (uint)position);

                // Wait to get to the position
                Thread.Sleep(25);
            }
        }
Пример #5
0
        /// <summary>
        /// Create a tlc device, and configure it to communicate on the SPI interface. Set the SPI settings to neutral:
        /// new SPI.Configuration(Pins.GPIO_PIN_D8, false, 0, 0, ...  , where the pin number can be the latch pin 
        /// 
        /// </summary>
        /// <param name="config">The SPI configuration. </param>
        /// <param name="PWMchannel1">Channel for the GSCLK pin</param>
        /// <param name="PWMChannel2">Channel for the BLANK pin</param>
        /// <param name="LATCHpin">Required output channel</param>
        /// <param name="channelCount">Must be max Channelcount provided by the Tlc5940</param>
        public Tlc5940(SPI.Configuration config, PWM gsclk, PWM blank, OutputPort LATCHport, uint channelCount)
        {
            useSPIInterface = true;

            SPIDevice = config;
            SPIBus = new SPI(SPIDevice);

            GSCLKPin = gsclk;
            BLANKPin = blank;
            XLATpin = LATCHport;
            ValidateChannelCount(channelCount);
            writeBuffer = CreateBuffer(channelCount);
            // Clear the channels, and disable the output
            GSCLKPin.SetDutyCycle(0);
            BLANKPin.SetDutyCycle(0);
            XLATpin.Write(false);
            GSCLKPin.SetPulse(gsclk_period, 1);
            //BLANKPin.SetPulse((gsclk_period * 4096), 1);
            BLANKPin.SetPulse((gsclk_period)*(4096/2), 1);
            //BLANKPin.SetPulse((gsclk_period + 1)*(4096/2), 1);

            // THis is the arduino formula:
        }