Example #1
0
        private static short Calibrate(AnalogIn gyroReading, ref short gyroMax, ref short gyroMin, short sleep, short avg, short gyroZero)
        {
            var tempmV = (Int16)gyroReading.Read();

            Thread.Sleep(sleep);

            for (int i = 0; i < avg; i++)
            {
                Thread.Sleep(sleep / avg);
                tempmV = (Int16)gyroReading.Read();

                gyroZero += tempmV;

                if (tempmV > gyroMax)
                {
                    gyroMax = tempmV;
                }

                if (tempmV < gyroMin)
                {
                    gyroMin = tempmV;
                }
            }

            gyroMax += 3;
            gyroMin -= 3;
            gyroZero = (Int16)(gyroZero / avg);

            return(gyroZero);
        }
Example #2
0
        public static void Main()
        {
            // InitializeDisplay();
            // DisplayStatus(0, "Potentiometer value:");

            AnalogIn pot = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An2);

            Credentials      auth       = new Credentials("admin", "admin");
            string           hubId      = "54f239b1801d90881e9d15be";
            DataPointService dataPoints = new DataPointService(auth, "adminProject", hubId, 5000);

            dataPoints.ConfigureForDevelopment("");

            // Start communications
            dataPoints.Start();

            while (true)
            {
                // Measure potentiometre value
                int potValue = pot.Read();

                // Display current value on screen
                // DisplayStatus(1, FormatValue(potValue));

                // Push current value to the data service
                Number number = new Number(potValue);
                dataPoints.PushDataPoint(number);

                // Wait before next measurement
                Thread.Sleep(3000);
            }
        }
Example #3
0
        public void UpdateData()
        {
            float dt;

            DateTime newTime = DateTime.UtcNow;

            dt            = newTime.Subtract(lastTimePoint).Ticks / (float)TimeSpan.TicksPerSecond;
            lastTimePoint = newTime;

            _voltage = aiPortU.Read() * 0.001f; //in V

            if (_voltage > 2.0f)
            {
                _current = (CurrentOffSet - aiPortI.Read() * 0.001f) * CurrFactor; //in A (1 V/A)
            }
            else
            {
                _current = 0;
            }

            if (dt > 0 && dt < 10)
            {
                _charge += _current * dt / 3.6f;
                _energy += _current * _voltage * dt;
            }
        }
Example #4
0
        private static void MonitorPing()
        {
            _ping.SetLinearScale(0, 1000);
            while (true)
            {
                // Measure distance with ping sensor.
                var distance = _ping.Read();
                Debug.Print(distance.ToString());

                // Activate Laser
                SetLaserState(distance < 150);

                // Set site pattern & fire.
                if (distance >= 180)
                {
                    SetSitePattern(0);
                }
                else if (distance >= 150 && distance < 180)
                {
                    SetSitePattern(3);
                }
                else if (distance > 30 && distance < 150)
                {
                    SetSitePattern(2);
                }
                else
                {
                    SetSitePattern(1);
                    Fire();
                    MoveToNextBarrel();
                }
                Thread.Sleep(200);
            }
        }
Example #5
0
 private void UpdateDelay()
 {
     // only Cobra has a variable resistor connected
     if ((Fez)SystemInfo.SystemID.Model == Fez.Domino)
     {
         _delay = (byte)(((_variableResistor.Read() - 1100) / 2200.0) * 255);
     }
 }
Example #6
0
        public int Read()
        {
            int tmpVal1 = selectorPort.Read();
            int tmpVal2 = selectorPort.Read();
            int tmpVal3 = selectorPort.Read();

            int tmpVal = ((tmpVal1 + tmpVal2 + tmpVal3) / 3);
            int tmpPos = GetSelectorPosition(selectorId, tmpVal);

            if (tmpPos != currentPosition)
            {
                currentPosition = tmpPos;
                // Debug.Print("Selector " + selectorId + " Value: " + tmpVal + " Pos: " + currentPosition);
                if (OnChange != null)
                {
                    OnChange(selectorId, currentPosition);
                }
            }

            return(tmpPos);
        }
Example #7
0
        // gets the distance from a MaxSonar ultrasonic sensor and returns the value
        // in feet.
        public static double getDistance(AnalogIn sensor)
        {
            uint distance = 0;
            for (int i = 0; i < 200; i++)
                distance += (uint)sensor.Read();
            distance /= 200;

            double distInches = distance * (3300.0 / 512);
            Debug.Print("distance: " + (distInches / 144).ToString("F4") + "");

            return distInches / 144.0;
        }
Example #8
0
        private static void Accelerometer_(AnalogIn accXYZ, string axis)
        {
            int AccOutput = accXYZ.Read();

            if (AccGlobal.Length > 0)
            {
                AccGlobal = AccGlobal + "   Axis (" + axis + ") " + AccOutput.ToString();
            }
            else
            {
                AccGlobal = "Accelerometer Axis (" + axis + ") " + AccOutput.ToString();
            }
        }
Example #9
0
        private static void Run(AnalogIn gyroReading, short gyroMax, short gyroZero, short gyroMin, short sleep,
                                float milliVoltPerDeg, string axis)
        {
            DateTime lastTime   = DateTime.Now;
            float    milliVolts = 0;
            float    headingMilliVolts;
            DateTime time;
            float    subheading;
            float    heading = 0;

            //while (true)
            //{
            Thread.Sleep(sleep);

            milliVolts = gyroReading.Read();
            time       = DateTime.Now;

            if (milliVolts >= gyroMin && milliVolts <= gyroMax)
            {
                headingMilliVolts = 0f;
            }
            else
            {
                headingMilliVolts = (gyroZero - milliVolts);
            }

            subheading = (headingMilliVolts / milliVoltPerDeg * time.Subtract(lastTime).Milliseconds / 1000);

            if (subheading > 0.066f || subheading < -0.066f)
            {
                heading += subheading;
            }

            string test = "Axis (" + axis + ") " + heading.ToString("F") + "°";

            //"ms, MilliVolts: " + milliVolts + "mV (min:" + gyroMin + ";avg:" +
            //         gyroZero + ";max:" + gyroMax + ")\nheading(mV): " +
            //         headingMilliVolts.ToString("F") + "mV, Heading:" +

            if (GyroGlobal.Length > 0)
            {
                GyroGlobal = GyroGlobal + "   " + test;
            }
            else
            {
                GyroGlobal = "Gyro " + test;
            }

            lastTime = time;
            //}
        }
        public float GetSpeed()
        {
            float roh = BarometricPressure / (RLuft * (Temperature + 273.15f));

            DynamicPressure = (aiPort.Read() / 5000f - 0.04f) / 0.00009f; //in Pa
            if (roh > 0 && DynamicPressure > 0)
            {
                return((float)MathEx.Sqrt(2 / roh * DynamicPressure));
            }
            else
            {
                return(0);
            }
        }
        public static void Main()
        {
            int noBall = 350;
            int ball = 160;

            int threshold = (noBall + ball) / 2;
            AnalogIn photodiode = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An0);

            while (true)
            {
                int d = photodiode.Read();
                Debug.Print(d + ": "+ (d > threshold ? "No ball" : "Ball"));
                Thread.Sleep(50);
            }
        }
Example #12
0
        public Keys GetKey()
        {
            int i = AnKey.Read();       // 10 bit AtoD, range = 0..1023
            // use this to read values to calibrate

            /*while (true)
             * {
             *  i = AnKey.Read();
             *  Debug.Print(i.ToString());
             *  Thread.Sleep(300);
             * }*/
            const int ERROR = 50;

            if (i >= 1021)
            {
                return(Keys.None);
            }

            if (i < 0 + ERROR)
            {
                return(Keys.Right);
            }

            if (i < 195 + ERROR && i > 195 - ERROR)
            {
                return(Keys.Up);
            }

            if (i < 450 + ERROR && i > 450 - ERROR)
            {
                return(Keys.Down);
            }

            if (i < 690 + ERROR && i > 690 - ERROR)
            {
                return(Keys.Left);
            }

            if (i < 1000 + ERROR && i > 1000 - ERROR)
            {
                return(Keys.Select);
            }

            return(Keys.None);
        }
            public static Keys GetKey()
            {
                int i = AnKey.Read();

                if (i < 1050 && i >= 1000)
                {
                    return(Keys.None);
                }

                if (i < 50 && i >= 0)
                {
                    return(Keys.Right);
                }

                if (i < 250 && i >= 180)
                {
                    return(Keys.Up);
                }

                if (i < 550 && i >= 400)
                {
                    return(Keys.Down);
                }

                if (i < 790 && i >= 650)
                {
                    return(Keys.Left);
                }

                if (i < 999 && i >= 830)
                {
                    return(Keys.Select);
                }

                return(Keys.None);
            }
Example #14
0
        public double getActualCurrent()
        {
            int mVolts = current.Read();

            return(mVolts * ampScale);//mAmps
        }
 public AnalogSonar(int pin)
 {
     SonarSensor = GetAnalogPort(pin);
     SonarSensor.SetLinearScale(0, 1023);
     SonarSensor.Read();
 }
 public MaxSonar(int pin)
 {
     SonarSensor = GetPort(pin);
     SonarSensor.SetLinearScale(0, 1023);
     SonarSensor.Read();             // Self-calibration on first read cycle
 }
Example #17
0
        static double sensorRH, tempCompensatedRH; // Reuse to reduce GC pressure

        #endregion Fields

        #region Methods

        public static void Main()
        {
            // Blink board LED

            var ledState = false;
            var led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

            // Enable LCD.
            var display = ConfigureLiquidCrystalDisplay();
            display.Clear();
            // Turn on backlight
            var greenBacklight = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di51, true);
            var redBacklight = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di50, true);
            var blueBacklight = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di52, true);

            ShowMessage("Weather Station;1.0", display);

            String message;
            var readings = new System.Collections.Queue();

            var humidity = new AnalogIn(AnalogIn.Pin.Ain0);
            var thermistor = new AnalogIn(AnalogIn.Pin.Ain5);
            int i, thermistorSample, padLength;
            double temp = 0;
            double tempF = 0;
            double rh = 0D;
            double[] temps = new double[5];
            double[] humiditySamples = new double[10];
            string tempStr, humidityStr;
            Boolean firstLoop = true;
            while (true)
            {
                //var sensor = new MPL1151A();
                //sensor.Start();
                //var temp = sensor.TemperatureF;
                //sensor.Stop();
                //var temp = PressureSensor.calculatePressure();

                for (i = 0; i < 5; i++)
                {
                    thermistorSample = thermistor.Read();
                    temp = ConvertADCToTemperature(thermistorSample);
                    temps[i] = temp;
                    Thread.Sleep(20);
                }

                temp = ArrayExtensions.Average(temps);
                tempF = (1.8D * temp) + 32;
                if (tempF < 69.0D)
                {
                    greenBacklight.Write(false);
                    blueBacklight.Write(true);
                    redBacklight.Write(false);
                }
                else if (tempF > 73.0D)
                {
                    greenBacklight.Write(false);
                    blueBacklight.Write(false);
                    redBacklight.Write(true);
                }
                else
                {
                    greenBacklight.Write(true);
                    blueBacklight.Write(false);
                    redBacklight.Write(false);
                }
                tempStr = tempF.ToString("F1");
                padLength = 8 - tempStr.Length;
                for (i = 0; i < padLength; i++)
                {
                    tempStr += " ";
                }

                for (i = 0; i < 10; i++)
                {
                    humiditySamples[i] = SampleHumidity(humidity.Read(), temp);
                    Thread.Sleep(10);
                }

                rh = ArrayExtensions.Average(humiditySamples);
                if (firstLoop)
                {
                    humidityStr = rh.ToString("F1") + "%";
                    message = MainModeHeader + ";" + tempStr + humidityStr;
                    ShowMessage(message, display);
                    firstLoop = false;
                }
                else
                {
                    humidityStr = rh.ToString("F1") + "%";
                    message = tempStr + humidityStr;
                    UpdateMessage(message, display);
                }

                Thread.Sleep(1000);
            }
        }
Example #18
0
 public float GetDistance_cm()
 {
     return(C / ((float)adc.Read() + (float).001) - (C / X0) + Y0);
 }
Example #19
0
        public double getActualVoltage()
        {
            int mVolts = voltage.Read();

            return(mVolts * voltScale / 1000);
        }