robobuggy / arduino / InterruptSingleChannel / InterruptSingleChannel.ino @ c5d6b0e8
History | View | Annotate | Download (1.32 KB)
1 |
// First Example in a series of posts illustrating reading an RC Receiver with |
---|---|
2 |
// micro controller interrupts. |
3 |
// |
4 |
// Subsequent posts will provide enhancements required for real world operation |
5 |
// in high speed applications with multiple inputs. |
6 |
// |
7 |
// http://rcarduino.blogspot.com/ |
8 |
// |
9 |
// Posts in the series will be titled - How To Read an RC Receiver With A Microcontroller |
10 |
|
11 |
// See also http://rcarduino.blogspot.co.uk/2012/04/how-to-read-multiple-rc-channels-draft.html |
12 |
|
13 |
// Include the servo library |
14 |
#include <Servo.h> |
15 |
#include "receiver.h" |
16 |
|
17 |
Servo myservo; |
18 |
// Center is 90 |
19 |
int pos = 0; |
20 |
// Note the following mapping is used between pins and interrupt numbers: |
21 |
//Board int.0 int.1 int.2 int.3 int.4 int.5 |
22 |
//Uno, Ethernet 2 3 |
23 |
//Mega2560 2 3 21 20 19 18 |
24 |
//Leonardo 3 2 0 1 7 |
25 |
//Due (see below) |
26 |
|
27 |
void setup() |
28 |
{ |
29 |
receiver_init(); |
30 |
myservo.attach(9); // attaches the servo on pin 9 to the servo object |
31 |
Serial.begin(9600); |
32 |
} |
33 |
|
34 |
void loop() |
35 |
{ |
36 |
static int input = 128; |
37 |
if(receiver_new()) { |
38 |
input = receiver_get_angle(); |
39 |
} |
40 |
|
41 |
//myservo.write(133+(input-128)/20); |
42 |
// I decided 133 is center |
43 |
// Note that 4 is a damping factor. |
44 |
// 43 |
45 |
int out = (input/4)+(90*3/4)+39; |
46 |
if(out < 105 || out > 160) { |
47 |
Serial.println("FAKFAKFAK SERVO OUT OF RANGE"); |
48 |
Serial.println(out); |
49 |
out = 129; |
50 |
} |
51 |
Serial.println(out); |
52 |
myservo.write(out); |
53 |
} |
54 |
|
55 |
|