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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <RCSwitch.h>
#include <LiquidCrystal.h>
RCSwitch mySwitch = RCSwitch( );
RCSwitch sender = RCSwitch( );
LiquidCrystal lcd = LiquidCrystal(8 ,9 ,4 ,5 ,6 ,7);
//Sender
#define OnCode1 1381717
#define OffCode1 1381716
#define OnCode2 1394005
#define OffCode2 1394004
#define CodeLength 24
#define SendPin A5
//Sonic sensor
#define trigPin A2
#define echoPin A1
//Buttons
#define KEY_COUNT 4
int keyLimits [KEY_COUNT+1] = {100, 330, 580, 900, 1023};
int keyNames [KEY_COUNT+1] = {0, 1, 2, 3, 4};
void setup() {
lcd.begin(16,2);
Serial.begin(9600);
Serial.println("RC receiver");
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
pinMode(SendPin, OUTPUT);
sender.enableTransmit(SendPin);
sender.setProtocol(1);
Serial.begin (9600);
pinMode(trigPin, OUTPUT) ;
pinMode(echoPin, INPUT) ;
}
void button_left(){
sender.send(OffCode1, CodeLength);
sender.send(OnCode2, CodeLength);
}
void button_right(){
sender.send(OnCode1, CodeLength);
sender.send(OffCode2, CodeLength);
}
void button_up(){
sender.send(OnCode1, CodeLength);
sender.send(OnCode2, CodeLength);
}
void button_down(){
sender.send(OffCode1, CodeLength);
sender.send(OffCode2, CodeLength);
}
int check_button(){
int val = analogRead(A0);
lcd.setCursor(0,0);
lcd.print(val);
for (int i = 0; i <= KEY_COUNT; i++)
if (val < keyLimits[i])
return keyNames[i];
}
bool smaller_than(){
digitalWrite(trigPin, HIGH) ;
delayMicroseconds(10);
digitalWrite(trigPin, LOW) ;
int duration = pulseIn(echoPin, HIGH) ;
lcd.setCursor(0,1);
lcd.print((duration / 2) / 29);
return (((duration / 2) / 29) < 100);
}
void loop() {
switch (check_button()) {
case 0: button_right(); break;
case 1: button_up(); break;
case 2: button_down(); break;
case 3: button_left(); break;
}
if(smaller_than()){
sender.send(OnCode1,CodeLength);
sender.send(OnCode2,CodeLength);
}
}
|