I am coding a program to control a servo with a USB Joystick. So far so good, but I am having a problem where the FEZ randomly resets while running my code.
I think one of the issues may be that I am making too many calls to move the servo at a time, so I was going to put a debounce statement in to insure that I am only making X many calls to move the servo during Y seconds.
The problem is that I need a property or method to get the current amount of ticks from the last time the FEZ was restarted. Does such a method or property exist?
In case anyone is interested, here is my code
using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.USBHost;
using System.IO;
using System.Threading;
using GHIElectronics.NETMF.FEZ;
namespace Servo
{
public class Program
{
public static PWM servo = new PWM((PWM.Pin)FEZ_Pin.PWM.Di8);
public static USBH_Joystick joystick;
public static long
public static void Main()
{
servo.SetPulse(20 * 1000 * 1000, 1250 * 1000);
Thread.Sleep(10000);
servo.SetPulse(20 * 1000 * 1000, 1750 * 1000);
Thread.Sleep(1500);
USBHostController.DeviceConnectedEvent += joyConnected;
Thread.Sleep(Timeout.Infinite);
}
public static long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
public static void joyConnected(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.Joystick)
{
Debug.Print("Joystick connected...");
joystick = new USBH_Joystick(device);
joystick.JoystickXYMove += moveServo;
}
}
public static void moveServo(USBH_Joystick sender, USBH_JoystickEventArgs args)
{
llong time = map(sender.Cursor.Y * -1, 0, 512, 1250, 1750);
servo.SetPulse(20 * 1000 * 1000, (uint)time * 1000);
}
}
}
Yeah, I know it’s messy, but it should work.
EDIT: Actually, the FEZ my have been disconnecting because I was pulling too much current from the USB port. When I connected my AC adapter, the code functioned properly.