-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_with_timer.ino
93 lines (80 loc) · 2.1 KB
/
sketch_with_timer.ino
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
//Before using this sketch, you need to upload a value to EEPROM 0
//Run initEEPROM.ino before this sketch
#include <IRremote.h>
#include <EEPROM.h>
//change motor max time here (ms)
const unsigned long maxTime = 10000;
unsigned long nowTime;
//use pin 9/10 to control up/down
const int upPin = 9;
const int downPin = 10;
//IR receiver pin 11
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
//change the hex code here
unsigned long upButton = 0x123456;
unsigned long downButton = 0x654321;
bool flag = false;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(upPin, OUTPUT);
pinMode(downPin, OUTPUT);
EEPROM.get(0, nowTime);
}
void loop()
{
unsigned long from;
from = millis();
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
if(results.value == upButton)
{
if(nowTime <= maxTime)
{
digitalWrite(upPin, HIGH);
Serial.println("Motor up");
nowTime = nowTime + (millis() - from) + 50;
flag = true;
}
else
{
Serial.println("Motor up to max");
digitalWrite(upPin, LOW);
digitalWrite(downPin, LOW);
EEPROM.put(0, nowTime);
flag = false;
}
}
else if(results.value == downButton)
{
if(nowTime >= 0)
{
digitalWrite(downPin, HIGH);
Serial.println("Motor down");
nowTime = nowTime - (millis() - from) - 50;
flag = true;
}
else
{
Serial.println("Motor down to max");
digitalWrite(upPin, LOW);
digitalWrite(downPin, LOW);
EEPROM.put(0, nowTime);
flag = false;
}
}
irrecv.resume();
}
else if(flag)
{
digitalWrite(upPin, LOW);
digitalWrite(downPin, LOW);
EEPROM.put(0, nowTime);
flag = false;
}
delay(50);
}