-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathProgram.cs
63 lines (49 loc) · 1.61 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
// Uncomment if you are using an ESP32 and install the nuget:
// using nanoFramework.Hardware.Esp32;
using System;
using System.Device.Pwm;
using System.Diagnostics;
using System.Threading;
Debug.WriteLine("Hello from Pwm!");
bool goingUp = true;
float dutyCycle = .00f;
// If you have an ESP32, you should setup the pin first, uncomment the next line:
// Configuration.SetPinFunction(18, DeviceFunction.PWM1);
// Note: if you have a STM32 board, you have to make sure your pin can use PWM (TIM)
// Then you can create the PWM Channel from the pin:
PwmChannel pwmPin = PwmChannel.CreateFromPin(18, 40000, 0);
// Note: even if possible, it is not recommended to adjust the frequency once created.
// Advance way of creating a PWM Channel.
// In this case you need to understand the chip/PWM/TIM to use and the channel/pin:
// PwmChannel pwmPin = new(1, 2, 40000, 0.5);
// Start the PWM
pwmPin.Start();
while (true)
{
if (goingUp)
{
// slowly increase light intensity
dutyCycle += 0.05f;
// change direction if reaching maximum duty cycle (100%)
if (dutyCycle > .95)
goingUp = !goingUp;
}
else
{
// slowly decrease light intensity
dutyCycle -= 0.05f;
// change direction if reaching minimum duty cycle (0%)
if (dutyCycle < 0.10)
goingUp = !goingUp;
}
// update duty cycle
pwmPin.DutyCycle = dutyCycle;
Thread.Sleep(50);
}
// Stop the PWM:
pwmPin.Stop();
Thread.Sleep(Timeout.Infinite);