peony3000

Gadgeteer Joystick LED Blink

Mar 30th, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 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_02
  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.         // led
  23.         TimeSpan ledBlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
  24.         int ledBlinkOffset = 500;
  25.         // led1
  26.         TimeSpan led1BlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
  27.         int led1BlinkOffset = 500;
  28.         // led2
  29.         TimeSpan led2BlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
  30.         int led2BlinkOffset = 500;
  31.         // led color set to cold
  32.         GT.Color hotColdColor = new GT.Color(0, 0, 255);
  33.         int hotColdColorRed = 0;
  34.         int hotColdColorGreen = 0;
  35.         int hotColdColorBlue = 255;
  36.        
  37.         // create the main timer event robot to trigger repeated tasks
  38.         GT.Timer mainTimer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously);
  39.  
  40.         // This method is run when the mainboard is powered up or reset.
  41.         void ProgramStarted()
  42.         {
  43.             // here we go!
  44.             Debug.Print("Program Started");
  45.  
  46.             // wake up with a start the timer event robot
  47.             mainTimer.Tick += new GT.Timer.TickEventHandler(mainTimer_Tick);
  48.             mainTimer.Start();
  49.  
  50.             // enable us to ask the joystick questions
  51.             joystick.DebugPrintEnabled = true;
  52.  
  53.             // flip the green and blue!!
  54.             led.GreenBlueSwapped = true;
  55.             led1.GreenBlueSwapped = true;
  56.             led2.GreenBlueSwapped = true;
  57.  
  58.         }
  59.  
  60.         void mainTimer_Tick(GT.Timer timer)
  61.         {
  62.             // this is what the event handling robot will do when it sees an event
  63.            
  64.             // tell the robot to look at the joystick and get it's position
  65.             double joystickPosX = joystick.GetJoystickPostion().X; // assigns the x-direction from 0 to 1 to the variable holder joystickPosX
  66.             double joystickPosY = joystick.GetJoystickPostion().Y; // assigns the y-direction from 0 to 1 to the variable holder joystickPosY
  67.            
  68.             // print out the numbers coming in from the joystick
  69.             //Debug.Print(((int)(joystickPosX * 1000)).ToString()); // prints the current joystick position * 255 in the x-direction
  70.             //Debug.Print(((int)(joystickPosY * 1000)).ToString()); // prints the current joystick position * 255 in the y-direction
  71.  
  72.             // convert the joystick's X position into milliseconds
  73.             ledBlinkOffset = (int)(joystickPosX * 667);
  74.             led1BlinkOffset = 667 - ledBlinkOffset;
  75.             ledBlinkOffset += 166;
  76.             led1BlinkOffset += 166;
  77.  
  78.             // convert the joystick's Y position into colors
  79.             hotColdColorRed = (int)(joystickPosY * 255);
  80.             hotColdColorBlue = 255 - hotColdColorRed;
  81.             hotColdColor = GT.Color.FromRGB((byte)hotColdColorRed, (byte)hotColdColorGreen, (byte)hotColdColorBlue);
  82.  
  83.             // add blink time
  84.             ledBlinkLength = TimeSpan.FromTicks((100 + (int)(joystickPosY * 200)) * 1000);
  85.  
  86.             // create a new led timer event robot
  87.             GT.Timer ledTimer = new GT.Timer(ledBlinkOffset, GT.Timer.BehaviorType.RunOnce);
  88.             ledTimer.Tick += new GT.Timer.TickEventHandler(ledTimer_Tick);
  89.            
  90.             // create a new led1 timer event robot
  91.             GT.Timer led1Timer = new GT.Timer(led1BlinkOffset, GT.Timer.BehaviorType.RunOnce);
  92.             led1Timer.Tick += new GT.Timer.TickEventHandler(led1timer_Tick);
  93.  
  94.             // create a new led1 timer event robot
  95.             GT.Timer led2Timer = new GT.Timer(led2BlinkOffset, GT.Timer.BehaviorType.RunOnce);
  96.             led2Timer.Tick += new GT.Timer.TickEventHandler(led2timer_Tick);
  97.          
  98.             // start the new led timer event robots
  99.             ledTimer.Start();
  100.             led1Timer.Start();
  101.             led2Timer.Start();
  102.  
  103.         }
  104.  
  105.         // led timer event robot's instructions
  106.         void ledTimer_Tick(GT.Timer timer)
  107.         {
  108.             led.BlinkOnce(hotColdColor, ledBlinkLength);
  109.             timer.Stop();
  110.         }
  111.  
  112.         // led1 timer event robot's instructions
  113.         void led1timer_Tick(GT.Timer timer)
  114.         {
  115.             led1.BlinkOnce(hotColdColor, ledBlinkLength);
  116.             timer.Stop();
  117.         }
  118.  
  119.         // led2 timer event robot's instructions
  120.         void led2timer_Tick(GT.Timer timer)
  121.         {
  122.             led2.BlinkOnce(hotColdColor, ledBlinkLength);
  123.             timer.Stop();
  124.         }
  125.  
  126.        
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment