.NET Gadgeteer Potentiometer Voltage Display Graph

This example graphs the voltage variation of a GHI Electronics Potentiometer on the Display_T35.  The output looks like this:

Graphing the Potentiometer Voltage

Graphing the Potentiometer Voltage

The modules are assembled in the Visual Studio .NET Gadgeteer Designer, including the GHI Electronics Potentiometer, a MultiColor LED, a button, the Display_T35, and GHI Electronics Fez Spider Mainboard.

Visual Studio .NET Gadgeteer Modules

Visual Studio .NET Gadgeteer Modules

The voltage of the potentiometer ranges from 0 – 3.3 volts.  With each tick of the timer a pixel on the display is lit that corresponds to the voltage on a y axis scaled to the height of the display.  The position on the x axis advances one pixel with each timer tick. The work is done in the timer tick event handler.

    void timer_Tick(GT.Timer timer)
    {
        double voltage = potentiometer.ReadPotentiometerVoltage();
        PotVoltage potVoltage = SetPotEnumAndLED(voltage);

        uint graph_x = xIndex++;

        if (graph_x + 5 > display.Width)
        {
            xIndex = 5;
            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("Maximum voltage: 3.3 V.",
                Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 150);
        }

        uint graph_y = (uint)((100 * voltage)*.73); // Scale the voltage to size of display.
        display.SimpleGraphics.SetPixel(GT.Color.Orange, graph_x, displayHeight - graph_y);
    }
Potentiometer and Fez Spider Mainboard

Potentiometer and Fez Spider Mainboard

The complete application code is in the Program.cs file as follows.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace Potentiometer
{
    public partial class Program
    {
        GT.Timer timer;
        uint xIndex = 5;
        uint displayHeight;

        void ProgramStarted()
        {
            timer = new GT.Timer(100, GT.Timer.BehaviorType.RunContinuously);
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            displayHeight = display.Height;

            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("Maximum voltage: 3.3 V.",
                Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 150);

            Debug.Print("Program Started");
        }

        enum PotVoltage
        {
            zero = 0,
            low,
            mid,
            high,
            top
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            if (button.IsLedOn)
            {
                timer.Tick -= new GT.Timer.TickEventHandler(timer_Tick);
                timer.Stop();
                button.ToggleLED();
                led.TurnOff();
                return;
            }

            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            timer.Start();
            button.ToggleLED();
        }

        private PotVoltage SetPotEnumAndLED(double voltage)
        {
            PotVoltage enumPot = PotVoltage.zero;
            for(int i = 0; i < 5; i++)
            {
                if (voltage == 0.0)
                {
                    enumPot = PotVoltage.zero;
                    led.TurnWhite();
                    break;
                }
                if(voltage < 1.0)
                {
                    enumPot = PotVoltage.low;
                    led.TurnGreen();
                    break;
                }
                if(voltage < 2.0)
                {
                    enumPot = PotVoltage.mid;
                    led.TurnBlue();
                    break;
                }
                if(voltage < 3.0)
                {
                    enumPot = PotVoltage.high;
                    led.TurnRed();
                    break;
                }

                led.BlinkRepeatedly(GT.Color.Yellow);
                enumPot = PotVoltage.top;
            }

            return enumPot;
        }

        void timer_Tick(GT.Timer timer)
        {
            double voltage = potentiometer.ReadPotentiometerVoltage();
            PotVoltage potVoltage = SetPotEnumAndLED(voltage);

            uint graph_x = xIndex++;

            if (graph_x + 5 > display.Width)
            {
                xIndex = 5;
                display.SimpleGraphics.Clear();
                display.SimpleGraphics.DisplayText("Maximum voltage: 3.3 V.",
                    Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 150);
            }

            uint graph_y = (uint)((100 * voltage)*.73); // Scale the voltage to size of display.
            display.SimpleGraphics.SetPixel(GT.Color.Orange, graph_x, displayHeight - graph_y);
        }
    }
}

Graphing the Potentiometer Voltage
Graphing the Potentiometer Voltage
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: