peony3000

Gadgeteer_DD01_Led_Joystick_Transition_01

Feb 11th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. using Microsoft.SPOT.Presentation;
  6. using Microsoft.SPOT.Presentation.Controls;
  7. using Microsoft.SPOT.Presentation.Media;
  8. using Microsoft.SPOT.Touch;
  9.  
  10. using Gadgeteer.Networking;
  11. using GT = Gadgeteer;
  12. using GTM = Gadgeteer.Modules;
  13. using Gadgeteer.Modules.GHIElectronics;
  14.  
  15. namespace Gadgeteer_DD01_Led_Joystick_Transition_01
  16. {
  17.     public partial class Program
  18.     {
  19.         // variables that are visible "Program" wide go here
  20.  
  21.         // create a timer robot that ticks every 100 milliseconds
  22.         GT.Timer timer = new GT.Timer(100);
  23.  
  24.         // variable holder for the current X position color
  25.         int currentColorX;
  26.  
  27.         // variable holder for the current Y position color
  28.         int currentColorY;
  29.  
  30.         // variable holder for the previous X position color
  31.         int previousColorX;
  32.  
  33.         // variable holder for the previous Y position color
  34.         int previousColorY;
  35.  
  36.         // variable holder for the speed of transition
  37.         int transitionValue = 10;
  38.  
  39.         // This method is run when the mainboard is powered up or reset.  
  40.         void ProgramStarted()
  41.         {
  42.             /*******************************************************************************************
  43.             Modules added in the Program.gadgeteer designer view are used by typing
  44.             their name followed by a period, e.g.  button.  or  camera.
  45.            
  46.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  47.                 button.ButtonPressed +=<tab><tab>
  48.            
  49.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  50.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  51.                 timer.Tick +=<tab><tab>
  52.                 timer.Start();
  53.             *******************************************************************************************/
  54.  
  55.             //  create a timer event handler robot that notices when the timer has ticked
  56.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  57.  
  58.             // start the timer robot running
  59.             timer.Start();
  60.  
  61.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  62.             Debug.Print("Program Started");
  63.         }
  64.  
  65.         void timer_Tick(GT.Timer timer)
  66.         {
  67.             // the timer robot has ticked
  68.        
  69.             // look at the joystick and get it's position
  70.             // assign the x-direction from 0 to 1 to the variable holder joystickPosX
  71.             double joystickPosX = joystick.GetJoystickPosition().X;
  72.  
  73.             // assign the y-direction from 0 to 1 to the variable holder joystickPosY
  74.             double joystickPosY = joystick.GetJoystickPosition().Y;
  75.  
  76.             // the joystick values range from 0 - 1
  77.             // print the readings to the debug output window
  78.             Debug.Print("X = " + joystickPosX + " Y = " + joystickPosY);
  79.  
  80.             // find the difference between the target value from the joystick and the previous value
  81.             // subtract the difference from the previous value
  82.             // if the value is negative it will be subtracted
  83.             // you have to convert the joystick's position into a bigger value
  84.             currentColorX = previousColorX - ((previousColorX - (int)(joystickPosX * 255)) / transitionValue);
  85.  
  86.             // find the difference between the target value from the joystick and the previous value
  87.             // subtract the difference from the previous value
  88.             // if the value is negative it will be subtracted
  89.             // you have to convert the joystick's position into a bigger value
  90.             currentColorY = previousColorY - ((previousColorY - (int)(joystickPosY * 255)) / transitionValue);
  91.  
  92.             // print to the debug output window
  93.             Debug.Print("X color = " + currentColorX + " Y color = " + currentColorY);
  94.  
  95.             // assign the x value to blue
  96.             multicolorLed.SetBlueIntensity(currentColorX);
  97.  
  98.             // assign the y value to red
  99.             multicolorLed.SetRedIntensity(currentColorY);
  100.  
  101.             // save the current values away so we can use it next time the timer ticks
  102.             previousColorX = currentColorX;
  103.             previousColorY = currentColorY;
  104.  
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment