peony3000

Gadgeteer Joystick LED Blink

Mar 29th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.GHIElectronics;
  13.  
  14. namespace Gadgeteer_Joystick_Blink_01
  15. {
  16.  
  17.     public partial class Program
  18.     {
  19.  
  20.         // Variable holders that are available everywhere
  21.         TimeSpan blinkTick = new TimeSpan(0, 0, 0, 0, 1000);
  22.         TimeSpan ledBlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
  23.         int ledBlinkOffset = 500;
  24.         //int ledBlinkSpace = (250);
  25.         TimeSpan led1BlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
  26.         int led1BlinkOffset = 500;
  27.         //int led1BlinkSpace = (250);
  28.         // This method is run when the mainboard is powered up or reset.
  29.  
  30.         GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously);
  31.  
  32.         void ProgramStarted()
  33.         {
  34.             /*******************************************************************************************
  35.             Modules added in the Program.gadgeteer designer view are used by typing
  36.             their name followed by a period, e.g.  button.  or  camera.
  37.            
  38.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  39.                 button.ButtonPressed +=<tab><tab>
  40.            
  41.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  42.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  43.                 timer.Tick +=<tab><tab>
  44.                 timer.Start();
  45.             *******************************************************************************************/
  46.  
  47.  
  48.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  49.             Debug.Print("Program Started");
  50.  
  51.            
  52.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  53.             timer.Start();
  54.  
  55.             joystick.DebugPrintEnabled = true; // allow the program to ask the joystick questions
  56.  
  57.         }
  58.  
  59.         void timer_Tick(GT.Timer timer)
  60.         {
  61.             // this is what the event handling robot will do when it sees an event
  62.             //look at the joystick
  63.             double joystickPosX = joystick.GetJoystickPostion().X; // assigns the x-direction from 0 to 1 to the variable holder joystickPosX
  64.             double joystickPosY = joystick.GetJoystickPostion().Y; // assigns the y-direction from 0 to 1 to the variable holder joystickPosY
  65.             //Debug.Print(((int)(joystickPosX * 1000)).ToString()); // prints the current joystick position * 255 in the x-direction
  66.             //Debug.Print(((int)(joystickPosY * 1000)).ToString()); // prints the current joystick position * 255 in the y-direction
  67.  
  68.             //ledBlinkOffset = TimeSpan.FromTicks((100 + (int)(joystickPosX * 800)) * 1000);
  69.             ledBlinkOffset = (100 + (int)(joystickPosX * 800));
  70.  
  71.             led1BlinkOffset = 1000 - ledBlinkOffset;
  72.  
  73.             GT.Timer ledTimer = new GT.Timer(ledBlinkOffset, GT.Timer.BehaviorType.RunOnce);
  74.             ledTimer.Tick += new GT.Timer.TickEventHandler(ledTimer_Tick);
  75.            
  76.             GT.Timer led1Timer = new GT.Timer(led1BlinkOffset, GT.Timer.BehaviorType.RunOnce);
  77.             led1Timer.Tick += new GT.Timer.TickEventHandler(led1timer_Tick);
  78.            
  79.            
  80.             ledTimer.Start();
  81.             led1Timer.Start();
  82.  
  83.         }
  84.  
  85.         void led1timer_Tick(GT.Timer timer)
  86.         {
  87.             //led1.BlinkOnce(GT.Color.Red, led1BlinkLength);
  88.             led1.SetRedIntensity(255);
  89.             //Thread.Sleep(100);
  90.             led1.SetRedIntensity(0);
  91.             timer.Stop();
  92.         }
  93.  
  94.         void ledTimer_Tick(GT.Timer timer)
  95.         {
  96.             //led.BlinkOnce(GT.Color.Red, ledBlinkLength);
  97.             led.SetRedIntensity(255);
  98.             //Thread.Sleep(100);
  99.             led.SetRedIntensity(0);
  100.             timer.Stop();
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment