peony3000

Gadgeteer Joystick Simple

Mar 29th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 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_Navigator_01
  15. {
  16.     public partial class Program
  17.     {
  18.         // This method is run when the mainboard is powered up or reset.  
  19.         void ProgramStarted()
  20.         {
  21.             /*******************************************************************************************
  22.             Modules added in the Program.gadgeteer designer view are used by typing
  23.             their name followed by a period, e.g.  button.  or  camera.
  24.            
  25.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  26.                 button.ButtonPressed +=<tab><tab>
  27.            
  28.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  29.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  30.                 timer.Tick +=<tab><tab>
  31.                 timer.Start();
  32.             *******************************************************************************************/
  33.  
  34.  
  35.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  36.             Debug.Print("Program Started");
  37.  
  38.             GT.Timer timer = new GT.Timer(100); // create a tick event every tenth of a second
  39.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); // create an event handling robot to watch for the tick
  40.             timer.Start(); // start the timer
  41.  
  42.             joystick.DebugPrintEnabled = true; // allow the program to ask the joystick questions
  43.         }
  44.  
  45.         void timer_Tick(GT.Timer timer)
  46.         {
  47.  
  48.             // this is what the event handling robot will do whenit sees an event
  49.             double joystickPosX = joystick.GetJoystickPostion().X; // assigns the x-direction from 0 to 1 to the variable holder joystickPosX
  50.             double joystickPosY = joystick.GetJoystickPostion().Y; // assigns the y-direction from 0 to 1 to the variable holder joystickPosY
  51.             //Debug.Print(((int)(joystickPosX * 255)).ToString()); // prints the current joystick position * 255 in the x-direction
  52.             //Debug.Print(((int)(joystickPosY * 255)).ToString()); // prints the current joystick position * 255 in the y-direction
  53.  
  54.             led.SetRedIntensity((int)(joystickPosX * 255)); // sets the led brightness to the current joystick position * 255 in the x-direction
  55.             led1.SetRedIntensity((int)(joystickPosY * 255)); // sets the led brightness to the current joystick position * 255 in the y-direction
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment