-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpw.ino
180 lines (156 loc) · 3.91 KB
/
pw.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "mpr121.h"
#include "i2c.h"
#include <Servo.h>
// 11 max digits used
#define DIGITS 11
// Match key inputs with electrode numbers
#define ONE 8
#define TWO 5
#define THREE 2
#define FOUR 7
#define FIVE 4
#define SIX 1
#define SEVEN 6
#define EIGHT 3
#define NINE 0
//extras (not used)
//#define ELE9 9
//#define ELE10 10
//#define ELE11 11
//interupt pin
int irqpin = 2; // D2
//PhotoResistor Pin
int lightPin = 3; //the analog pin the photoresistor is
//connected to
//the photoresistor is not calibrated to any units so
//this is simply a raw sensor value (relative light)
//LED Pin
int ledPin = 5; //the pin the LED is connected to
//we are controlling brightness so
//we use one of the PWM (pulse width
// modulation pins)
Servo myservo;
void setup()
{
//make sure the interrupt pin is an input and pulled high
pinMode(irqpin, INPUT);
digitalWrite(irqpin, HIGH);
myservo.attach(9);
Serial.begin(9600);
DDRC |= 0b00010011;
PORTC = 0b00110000;
i2cInit();
delay(100);
mpr121QuickConfig();
// Create and interrupt to trigger when a button
// is hit, the IRQ pin goes low, and the function getNumber is run.
attachInterrupt(0,getNumber,LOW);
// prints 'Ready...' when you can start hitting numbers
pinMode(13, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Ready...");
}
void loop()
{
nightLight();
delay(500);
}
int i = 0;
char digits[DIGITS];
void getNumber()
{
int touchNumber = 0;
uint16_t touchstatus;
touchstatus = mpr121Read(0x01) << 8;
touchstatus |= mpr121Read(0x00);
for (int j=0; j<12; j++) // Check how many electrodes were pressed
{
if ((touchstatus & (1<<j)))
touchNumber++;
}
if (touchNumber == 1)
{
if (touchstatus & (1<<SEVEN))
digits[i] = '7';
else if (touchstatus & (1<<FOUR))
digits[i] = '4';
else if (touchstatus & (1<<ONE)){
digits[i] = '1'; i=0;}
else if (touchstatus & (1<<EIGHT))
digits[i] = '8';
else if (touchstatus & (1<<FIVE))
digits[i] = '5';
else if (touchstatus & (1<<TWO))
digits[i] = '2';
else if (touchstatus & (1<<NINE))
digits[i] = '9';
else if (touchstatus & (1<<SIX))
digits[i] = '6';
else if (touchstatus & (1<<THREE))
digits[i] = '3';
Serial.print(digits[i]);
i++;
if (touchstatus & (1<<TWO)){
i=0;
}
if(i==4){
Serial.print("i4!");
i=0;
Serial.print("Reset!\n");
blinker(3);
}
}
if(digits[0] == '1' && digits[1] == '9' && digits[2] == '8' && digits[3] == '7'){
blinker(3);
myservo.write(42);
i=0;
Serial.print("Unlocked!");
}
if(digits[0] == '3' && digits[1] == '3' && digits[2] == '3' && digits[3] == '3'){
blinker(3);
myservo.write(140);
i=0;
Serial.print("Locked!");
}
else if (touchNumber == 2){
if (touchstatus & (1<<SEVEN)){
blinker(3);
myservo.write(90);
i=0;
}
;
}
else
;
}
void nightLight(){
int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255
lightLevel = constrain(lightLevel, 0, 255);//make sure the
//value is betwween
//0 and 255
// Serial.print("Light Level");
Serial.println(lightLevel);
if(lightLevel >200)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
//analogWrite(9, lightLevel); //write the value
}
void blinker(int t){
for(int x = 0; x<=t; x++){
on();
delay(1000);
off();
delay(1000);
}
}
void on(){
digitalWrite(13, LOW);
}
void off(){
digitalWrite(13, HIGH);
}