/// <summary>
        /// Repeatedly checks the state of the switch and resets it necessary
        /// </summary>
        private static void MainLoop(Motor motor)
        {
            var touchSensor = new TouchSensor(SensorPort.In1);

            while (true) {

                // there are a few states that the switch and the arm can be in
                // our default is the arm down, and the switch off
                // if someone flips the switch, we'll move our arm up until it completes its motion
                // at that point, the switch will be off again and we'll move the arm back

                // Our "switch" is off when the touch sensor is pressed (inverse)
                var switchStatus = !touchSensor.IsPressed ();

                // When our switch is on, we need to turn it back off
                if (switchStatus) {
                    // if the motor hasn't moved backwards to push the switch off, we'll do that
                    if (motor.GetTachoCount () > -120) {
            //						LcdConsole.WriteLine ("(On) Extending arm");
                        motor.On (-20);
                        System.Threading.Thread.Sleep (50);
                    } else { // if it's moved all the way back, stop pushing it
            //						LcdConsole.WriteLine ("(On) Arm fully extended");
                        motor.Off ();
                    }

                } else { // When it's off, we're happy
                    // if the arm isn't fully retracted, it's time to put it back to zero
                    if (motor.GetTachoCount () < -20) {
            //						LcdConsole.WriteLine ("(Off) Reset the arm");
                        motor.On (20);
                        System.Threading.Thread.Sleep (200);
                    } else { // if it is, we're done
            //						LcdConsole.WriteLine ("(Off) Arm fully reset");
                        motor.Off ();
                    }
                }

                if (switchStatus != PreviouslySwitched && !PreviouslySwitched) {
                    Action<string> foo = delegate(string x) {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create ("http://crappychatws.azurewebsites.net/api/lego");
                        request.Proxy = null;
                        request.GetResponse ();
                        LcdConsole.WriteLine(x);
                    };
                    foo.BeginInvoke ("Someone flipped my switch >_<", null, null);
                }

                PreviouslySwitched = switchStatus;
            }
        }
示例#2
0
 public static void Main(string[] args)
 {
     Console.WriteLine ("Resetting the motors");
     TouchSensor ts = new TouchSensor(SensorPort.In2);
     Motor motorFwd = new Motor(MotorPort.OutB);
     Motor motorTurn = new Motor(MotorPort.OutC);
     Motor motorSwitch = new Motor(MotorPort.OutA);
     Motor motorArm = new Motor(MotorPort.OutD);
     motorArm.On (-60);
     ButtonEvents buts = new ButtonEvents();
     bool keepGoing = true;
     while (keepGoing) {
         buts.EscapePressed += () => {
             keepGoing = false;
         };
         if (ts.IsPressed ()) {
             keepGoing = false;
         }
     }
     motorArm.Off ();
     switchMode ();
     keepGoing = true;
     TouchSensor ts2 = new TouchSensor(SensorPort.In4);
     motorArm.On (-60);
     while (keepGoing) {
         buts.EscapePressed += () => {
             keepGoing = false;
         };
         if (ts2.IsPressed ()) {
             keepGoing = false;
         }
     }
     motorArm.Off ();
     switchMode ();
     motorArm.ResetTacho ();
     motorFwd.ResetTacho ();
     motorSwitch.ResetTacho ();
     motorTurn.ResetTacho ();
 }