-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathProgram.cs
77 lines (58 loc) · 2.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
////
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
////
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.GiantGecko.Adc;
namespace ContinuousSampling
{
public class Program
{
public static void Main()
{
Console.WriteLine("ADC sample: continuous channel scanning a list of ADC channels");
Console.WriteLine();
// this is instantiating the ADC controller with the default configuration
// there is an overloaded constructor that accepts the configuration as a parameter
AdcController adc1 = new AdcController();
// setup configuration to perform conversion
var adcChannel0Config = new AdcChannelConfiguration()
{
ReferenceVoltage = ReferenceVoltage.InternalDifferencial_5V,
SampleResolution = SampleResolution._12bits
};
// calling this will have the ADC start scanning continuously the specified channels (one and three) using the configuration requested
adc1.StartContinuousSampling(new int[] {1, 3 }, adcChannel0Config);
// allow some time for the ADC to start the scanning
Thread.Sleep(100);
int[] lastSamples;
// print the last sample from the channels being scanned (these are the raw values read from the ADC channels)
for (int i = 0; i < 10; i++)
{
lastSamples = adc1.LastContinuousSamples;
Console.WriteLine("Last sample");
Console.WriteLine($"channel 1: {lastSamples[0]}");
Console.WriteLine($"channel 3: {lastSamples[1]}");
Console.WriteLine();
}
// stop continuous conversion
adc1.StopContinuousSampling();
// there is also the possibility to have the library take N samples and return the average value of those
adc1.StartAveragedContinuousSampling(new int[] { 2, 4 }, 10);
// print the averaged 10 last samples from the channels being scanned (these are the raw values read from the ADC channels)
for (int i = 0; i < 10; i++)
{
lastSamples = adc1.LastContinuousSamples;
Console.WriteLine("Average for the last 10 samples");
Console.WriteLine($"channel 2: {lastSamples[0]}");
Console.WriteLine($"channel 4: {lastSamples[1]}");
Console.WriteLine();
}
// stop continuous conversion
adc1.StopContinuousSampling();
Thread.Sleep(Timeout.Infinite);
}
}
}