unkwntech

Temp Sensor Managed Code

Jul 8th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Komodex.NETMF;
  4. using Microsoft.SPOT;
  5. using NetduinoGo;
  6.  
  7. namespace NetduinoGo_TestProject
  8. {
  9.     public class Program
  10.     {
  11.         private static SevenSegmentDisplay display = new SevenSegmentDisplay();
  12.         private static bool F = true;
  13.         public static void Main()
  14.         {
  15.             Potentiometer sensor = new Potentiometer();
  16.             Button button = new Button();
  17.  
  18.             button.ButtonReleased += new Button.ButtonEventHandler(button_ButtonReleased);
  19.  
  20.             display.SetBrightness((float)0.5);
  21.             display.SetApostrophe(true);
  22.  
  23.             double Tc = 19.5;
  24.             int V0c = 400;
  25.             double Ta = 0;
  26.             double raw = 0;
  27.  
  28.             double Vdiv = (3.3/1023);
  29.  
  30.             while(true)
  31.             {
  32.                 raw = (sensor.GetValue() / Vdiv) * 10;
  33.                 //Vout = Tc*Ta+V0c
  34.                 //Tc = Temperature Coefficient
  35.                 //Ta = Ambient Temperature
  36.                 //V0c = Output at 0C
  37.                 Ta = (raw - V0c)/Tc;
  38.  
  39.                 //Debug.Print(raw + " : " + Ta.ToString());
  40.                 if(F)
  41.                     UpdateDisplay(ConvertCtoF(Ta));
  42.                 else
  43.                     UpdateDisplay(Ta);
  44.  
  45.                 Thread.Sleep(250);
  46.             }
  47.         }
  48.  
  49.         static void button_ButtonReleased(object sender, bool buttonState)
  50.         {
  51.             F = !F;
  52.         }
  53.  
  54.         public static double ConvertCtoF(double c)
  55.         {
  56.             return c * 1.8 + 32;
  57.         }
  58.  
  59.         public static void UpdateDisplay(double value)
  60.         {
  61.             if (value >= 1000)
  62.                 throw new ArgumentOutOfRangeException("value");
  63.  
  64.             string v = value.ToString();//.ToCharArray();
  65.  
  66.             Digit[] output = new Digit[3];
  67.  
  68.             if(value < 100)
  69.             {
  70.                 output[0] = DivineDigit(v[0].ToString());
  71.                 output[1] = (Digit)((byte)DivineDigit(v[1].ToString()) | (byte)Segment.Dp);
  72.                 output[2] = DivineDigit(v[3].ToString());
  73.             }
  74.  
  75.             if(F)
  76.                 display.SetValue(output[0], output[1], output[2], (Digit)Letters.F);
  77.             else
  78.                 display.SetValue(output[0], output[1], output[2], (Digit)Letters.C);
  79.  
  80.         }
  81.  
  82.         public static Digit DivineDigit(string value)
  83.         {
  84.             Digit output;
  85.             if (value == "9")
  86.             {
  87.                 output = Digit.D9;
  88.             }
  89.             else if (value == "8")
  90.             {
  91.                 output = Digit.D8;
  92.             }
  93.             else if (value == "7")
  94.             {
  95.                 output = Digit.D7;
  96.             }
  97.             else if (value == "6")
  98.             {
  99.                 output = Digit.D6;
  100.             }
  101.             else if (value == "5")
  102.             {
  103.                 output = Digit.D5;
  104.             }
  105.             else if (value == "4")
  106.             {
  107.                 output = Digit.D4;
  108.             }
  109.             else if (value == "3")
  110.             {
  111.                 output = Digit.D3;
  112.             }
  113.             else if (value == "2")
  114.             {
  115.                 output = Digit.D2;
  116.             }
  117.             else if (value == "1")
  118.             {
  119.                 output = Digit.D1;
  120.             }
  121.             else
  122.                 output = Digit.D0;
  123.  
  124.             return output;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment