-
Notifications
You must be signed in to change notification settings - Fork 5
/
sbusleds.ino
57 lines (45 loc) · 1.09 KB
/
sbusleds.ino
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
#include <SBUS.h>
#include <limits.h>
// Remember to unplug the RX pin of your UNO when uploading this sketch.
SBUS sbus(Serial);
const int LED1 = 5;
const int LED2 = 6;
const int LED3 = 9;
const int LED4 = 10;
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
sbus.begin();
}
// This is timer2, which triggers ever 1ms and processes the incoming SBUS datastream.
ISR(TIMER2_COMPA_vect)
{
sbus.process();
}
static int minChannel = INT_MAX;
static int maxChannel = INT_MIN;
// Scale the S.BUS channel values into the range [0, 255] for use as LED brightness values.
int getChannel(int channel) {
int value = sbus.getChannel(channel);
if (value < minChannel) {
minChannel = value;
}
if (value > maxChannel) {
maxChannel = value;
}
float result = value;
result -= minChannel;
result /= (maxChannel - minChannel);
result *= 255;
return (int)result;
}
void loop()
{
analogWrite(LED1, getChannel(1));
analogWrite(LED2, getChannel(2));
analogWrite(LED3, getChannel(3));
analogWrite(LED4, getChannel(4));
}