This example graphs the voltage variation of a GHI Electronics Potentiometer on the Display_T35. The output looks like this:
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.
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); }
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); } } }