My program above was wrong. Though, it seems firmware 4.3 beta also have it share of bugs. I tried re-flashing to the older firmware version, 4.2.1, and this corrected code ran perfectly. but when I tried it on firmware v4.3,VS does not even hit the breakpoint, and the debugger just exits suddenly without anything happening.
I guess that firmware 4.3 beta is still buggy. Reverting back to firmware v4.2.1....
This is the correct one:
public class Program
{
// setup analog input
static SecretLabs.NETMF.Hardware.AnalogInput potentiometer = new
secretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0);
// use an output port that can handle PWM (pin D5,D6,D9,etc.)
static PWM pulseWidthModulatedLed = new PWM(PWMChannels.PWM_PIN_D5, 100, 0.5, false); // Set later
public static void Main()
{
potentiometer.SetRange(0, 100); // to make this show value in %
pulseWidthModulatedLed.Frequency = 200;
pulseWidthModulatedLed.Start();
int potentiometerValue = 0;
double percent = 0;
while (true)
{
// By default, Read() will return int type from 0-1023 by default
// But here, the range was customized to return values
// from 0, 100
potentiometerValue = potentiometer.Read();
// divide the read integer value to get the decimal value
percent = potentiometerValue / 100.0;
// dutycycle(a double type) needs a value from 0 to 1
pulseWidthModulatedLed.DutyCycle = percent;
}
}
}