Hi
I am trying to get a servo working I copied this code from the internet when I run it, it fails with the following error
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.dll
Any help will be appreciated
Thanks in advance
Graham
Attached is the code I am using on a netduino plus2 version 4.2.2
program.cs
using System.Threading;
using SecretLabs.NETMF.Hardware.Netduino;
using Microsoft.SPOT;
namespace MinelectricsV2
{
public class Program
{
static Servo[] servos = new Servo[4];
public static void Main()
{
//Netduino only has four PWM pins: GPIO_PIN_D5, 6, 9, and 10
servos[0] = new Servo(Pins.GPIO_PIN_D5);
servos[1] = new Servo(Pins.GPIO_PIN_D6);
servos[2] = new Servo(Pins.GPIO_PIN_D9);
servos[3] = new Servo(Pins.GPIO_PIN_D10);
while (true)
{
MoveServos(0);
MoveServos(90);
MoveServos(180);
}
}
private static void MoveServos(uint angle)
{
for (int i = 0; i < servos.Length; i++)
{
servos[i].Angle = angle;
}
Thread.Sleep(2000);
}
}
}
class.cs
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
namespace MinelectricsV2
{
public class Servo
{
static PWM servo;
public Servo(Cpu.Pin pin)
{
servo = new PWM(pin);
servo.SetDutyCycle(dutyCycle: 0);
}
private uint _angle = 0;
public uint Angle
{
get
{
return _angle;
}
set
{
const uint degreeMin = 0;
const uint degreeMax = 180;
const uint durationMin = 500; // 480
const uint durationMax = 2300; // 2450
_angle = value;
if (_angle < degreeMin) _angle = degreeMin;
if (_angle > degreeMax) _angle = degreeMax;
uint dur = (_angle - degreeMin) * (durationMax - durationMin) / (degreeMax - degreeMin) + durationMin;
servo.SetPulse(period: 20000, duration: dur);
}
}
}
}