summaryrefslogtreecommitdiff
path: root/sketch_receiver/sketch_receiver.ino
blob: 614e1200172f4ee75ada0d724f0e66cf77dc39f8 (plain) (blame)
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
98
#include <RCSwitch.h>
#include <LiquidCrystal.h>

RCSwitch sender = RCSwitch( );
LiquidCrystal lcd = LiquidCrystal(8 ,9 ,4 ,5 ,6 ,7);

#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};

const unsigned long OnCode[2] = {1381717, 1394005};
const unsigned long OffCode[2] = {1381716, 1394004};

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 setSocket(int id, bool on) {
    sender.send((on ? OnCode : OffCode)[id], CodeLength);
    lcd.setCursor(0, id);
    lcd.print("Socket ");
    lcd.print(id);
    lcd.print(on ? " on" : " off");
}

void button_left(){
    setSocket(0, true);
    setSocket(1, false);
}

void button_right(){
    setSocket(0, false);
    setSocket(1, true);
}

void button_up(){
    setSocket(0, true);
    setSocket(1, true);
}

void button_down(){
    setSocket(0, false);
    setSocket(1, false);
}

int check_button(){
  int val = analogRead(A0);
  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()){
    setSocket(0,true);
    setSocket(1,true);
  }
  
  
}