Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.GHIElectronics;
- namespace Gadgeteer_DC_Motor_01
- {
- public partial class Program
- {
- GT.Timer timer = new GT.Timer(100, GT.Timer.BehaviorType.RunContinuously);
- // joystick position
- double joystickPosX;
- double joystickPosY;
- // previous joystick position
- double previousJoystickPosX;
- double previousJoystickPosY;
- // the tolerance to decide
- double joystickTolerance = 0.20;
- //bool flip = false;
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- /*******************************************************************************************
- Modules added in the Program.gadgeteer designer view are used by typing
- their name followed by a period, e.g. button. or camera.
- Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
- button.ButtonPressed +=<tab><tab>
- If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
- GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
- timer.Tick +=<tab><tab>
- timer.Start();
- *******************************************************************************************/
- timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
- timer.Start();
- // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
- Debug.Print("Program Started");
- }
- void timer_Tick(GT.Timer timer)
- {
- // tell the robot to look at the joystick and get it's position
- joystickPosX = joystick.GetJoystickPosition().X; // assigns the x-direction from 0 to 1 to the variable holder joystickPosX
- joystickPosY = joystick.GetJoystickPosition().Y; // assigns the y-direction from 0 to 1 to the variable holder joystickPosY
- Debug.Print("joystickPosX: " + joystickPosX + " joystickPosY: " + joystickPosY);
- // check to see if the last value is signifcantly different from the current value
- if (joystickPosX < previousJoystickPosX + joystickTolerance && joystickPosX > previousJoystickPosX - joystickTolerance)
- {
- // send a new value to the motor
- motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, (int)(joystickPosX * 100));
- }
- if (joystickPosY < previousJoystickPosY + joystickTolerance && joystickPosY > previousJoystickPosY - joystickTolerance)
- {
- // send a new value to the motor
- motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, (int)(joystickPosY * 100));
- }
- previousJoystickPosX = joystickPosX;
- previousJoystickPosY = joystickPosY;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment