root / quad2 / arduino / src / ros_lib / examples / button_example / button_example.pde @ c1426757
History | View | Annotate | Download (1.16 KB)
1 | c1426757 | Tom Mullins | /* |
---|---|---|---|
2 | * Button Example for Rosserial |
||
3 | */ |
||
4 | |||
5 | #include <ros.h> |
||
6 | #include <std_msgs/Bool.h> |
||
7 | |||
8 | |||
9 | ros::NodeHandle nh; |
||
10 | |||
11 | std_msgs::Bool pushed_msg; |
||
12 | ros::Publisher pub_button("pushed", &pushed_msg); |
||
13 | |||
14 | const int button_pin = 7; |
||
15 | const int led_pin = 13; |
||
16 | |||
17 | bool last_reading; |
||
18 | long last_debounce_time=0; |
||
19 | long debounce_delay=50; |
||
20 | bool published = true; |
||
21 | |||
22 | void setup() |
||
23 | { |
||
24 | nh.initNode(); |
||
25 | nh.advertise(pub_button); |
||
26 | |||
27 | //initialize an LED output pin |
||
28 | //and a input pin for our push button |
||
29 | pinMode(led_pin, OUTPUT); |
||
30 | pinMode(button_pin, INPUT); |
||
31 | |||
32 | //Enable the pullup resistor on the button |
||
33 | PORTD |= (1<<PD7); |
||
34 | |||
35 | //The button is a normally button |
||
36 | last_reading = ! digitalRead(button_pin); |
||
37 | |||
38 | } |
||
39 | |||
40 | void loop() |
||
41 | { |
||
42 | |||
43 | bool reading = ! digitalRead(button_pin); |
||
44 | |||
45 | if (last_reading!= reading){ |
||
46 | last_debounce_time = millis(); |
||
47 | published = false; |
||
48 | } |
||
49 | |||
50 | //if the button value has not changed for the debounce delay, we know its stable |
||
51 | if ( !published && (millis() - last_debounce_time) > debounce_delay) { |
||
52 | digitalWrite(led_pin, reading); |
||
53 | pushed_msg.data = reading; |
||
54 | pub_button.publish(&pushed_msg); |
||
55 | published = true; |
||
56 | } |
||
57 | |||
58 | last_reading = reading; |
||
59 | |||
60 | nh.spinOnce(); |
||
61 | } |