4
// Testing controlling ESCs (and motors).
11
#define NUM_CHANNELS 4
13
// pin number of first channel
16
// 0 <= channel value <= this constant
17
#define MAX_CHANNEL_VALUE 1000UL
19
// pulse width when channel value is 0 and MAX_CHANNEL_VALUE in microseconds
20
#define MIN_PULSE_WIDTH 1150UL
21
#define MAX_PULSE_WIDTH 1750UL
23
// interval between rising edges of successinve channels in microseconds
24
#define CHANNEL_INTERVAL ( MAX_PULSE_WIDTH + 500UL )
26
// duration of a whole frame (including the pulse) in microseconds
27
#define FRAME_DURATION 20000UL
34
// deltas (applied every 100 milliseconds)
35
#define RAMP_UP_DELTA 4
36
#define RAMP_DOWN_DELTA -10
42
for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_CHANNELS; pin++ ) {
43
pinMode( pin, OUTPUT );
44
digitalWrite( pin, LOW );
51
signed long calculate_duration( unsigned long then, unsigned long now )
53
// does it look like now has overflowed (and wrapped)?
54
if( now < then && now < ( ULONG_MAX / 2 ) && then > ( ULONG_MAX / 2 ) )
55
return now + ( ULONG_MAX - then );
57
// else, calculate duration
63
void update_channels( int channels[] )
65
static int event = NUM_CHANNELS * 2;
66
unsigned long now = micros();
67
static unsigned long frame_start = now - FRAME_DURATION;
68
static unsigned long next_event_at;
70
if( now >= next_event_at )
73
digitalWrite( FIRST_PIN + ( event / 2 ), ( event & 1 )? LOW : HIGH );
76
if( ++event >= NUM_CHANNELS * 2 ) {
78
frame_start += FRAME_DURATION;
81
// calculate the time that the next event will occur
82
next_event_at = frame_start + ( event / 2 ) * CHANNEL_INTERVAL;
84
next_event_at += MIN_PULSE_WIDTH + channels[ event / 2 ] *
85
( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
87
// Serial.print( event / 2 );
88
// Serial.print( ( event & 1 )? 'v' : '^' );
89
// Serial.print( " " );
90
// Serial.println( next_event_at );
93
unsigned long width = MIN_PULSE_WIDTH +
94
(unsigned long)channels[ event / 2 ] *
95
( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
96
Serial.print( channels[ event / 2 ] );
98
Serial.println( width );
106
int channels[ NUM_CHANNELS ];
107
for( int a = 0; a < NUM_CHANNELS; a++ )
110
// test ramping state
111
unsigned long then = millis();
112
int val = RAMP_START, delta = RAMP_UP_DELTA;
116
update_channels( channels );
118
// update ramp value and set channel 0 value
119
unsigned long now = millis();
120
if( now > then + 100 ) {
123
if( val >= RAMP_MAX ) {
125
delta = RAMP_DOWN_DELTA;