hi,
I am creating a small model of a domotic home (I dunno if it is the right term, I am italian). I need to use a Keypad to turn on some LEDs, a fan, the alarm, a servo, and a curtain. I'll use the button 1 to turn on led 1, the button 2 to turn on led 2, button 3 to turn on led 3, button 4 to start the fan, button 5 to turn on the alarm, button 6 to start the servo, button 7 to open the curtain (it can go up and down), and button 8 to stop all. Other things go without the Keypad.
Here the code, but in the section with the Keypad I did not put yet the servo, the fan and, I think, the alarm.
#include <LiquidCrystal.h>
#include <Servo.h>
#include <Keypad.h>
#include <Stepper.h>
#include <SimpleDHT.h>
int checkAlarmInterval = 50;
int lightsInterval = 120;
int keypadInterval = 100;
int temperatureInterval = 1000;
int alarmInterval = 50;
int ActualAlarmInterval = 50;
int RFIDinterval = 100;
int stepperTendaInterval = 2457.6;
int IRreceiverInterval = 120;
int IRcodesInterval = 120;
int ventolaInterval = 10000;
int DHT11Interval = 10000;
int altezzaAcquaInterval = 50;
int lavatriceInterval = 30;
int entrance1Led = 0;
int kitchenLed = 0;
int bedroomsLed = 0;
int tenda1 = 0;
int allarme = 0;
int delayRead = 1000;
long duration;
const String codiceGiusto = "70EFBE1938";
const int stepsPerRevolution1 = 4096;
const int stepsPerRevolution2 = 4096;
long r;
Stepper tenda(stepsPerRevolution1, 24, 25, 26, 27);
Stepper door(stepsPerRevolution2, 40, 41, 42, 43);
int i = 0;
int pos = 0;
int KitchenLed = 4;
int entranceLed = 3;
int BedroomsLed = 5;
int alarmLed = 2;
int alarmBuzzer = 6;
int triggerPort = 28;
int echoPort = 29;
int pompa = 7;
int ventola = 12;
int DHT11 = A2;//???????????????????
int pirPin = 8;
int calibrationTime = 30;
int livello = 0; // variabile di memorizzazione del livello dell'acqua
int valoremin = 210; // variabile in cui viene memorizzato il valore minimo
int valoremax = 395; // variabile in cui viene memorizzato il valore massimo rilevabile dal sensore
SimpleDHT11 dht11;
byte temperature = 0;
byte humidity = 0;
long temperaturePreviousMillis = 0;
long IRreceiverPreviousMillis = 0;
long IRcodesPreviousMillis = 0;
long alarmPreviousMillis = 0;
long ActualAlarmPreviousMillis = 0;
long RFIDpreviousMillis = 0;
long ventolaPreviousMillis = 0;
long DHT11PreviousMillis = 0;
long stepperTendaPreviousMillis = 0;
long lightsPreviousMillis = 0;
long altezzaAcquaPreviousMillis = 0;
long keypadPreviousMillis = 0;
long lavatricePreviousMillis = 0;
long checkAlarmPreviousMillis = 0;
int isAlarmTriggered = 0;
Servo washingMachine;
char key;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
int code1[] = {};
int code2[] = {};
int code3[] = {};
int code4[] = {};
int code5[] = {};
int code6[] = {};
byte rowPins[ROWS] = {51, 50, 49, 48}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {47, 46, 45, 44}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(9600); // start serial communication
Serial.println("setup");
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
pinMode(entranceLed, OUTPUT);
pinMode(alarmLed, OUTPUT);
pinMode(BedroomsLed, OUTPUT);
washingMachine.attach(22);
Serial.println("Premere sul telecomando:");
Serial.println("1 = accendi/spegni luce dell'entrata");
Serial.println("2 = accendi/spegni luce della cucina");
Serial.println("3 = accendi/spegni luce delle camere");
Serial.println("4 = fai partire/spegni la lavatrice");
Serial.println("5 = alza/abbassa la tenda");
Serial.println("6 = attiva/disattiva allarme");
pinMode(KitchenLed, OUTPUT);
i = 0;
}
void loop() {
digitalWrite(7, HIGH);
if (millis() - alarmPreviousMillis > alarmInterval ){
alarmPreviousMillis = millis();
Serial.println("allarme");
if ((allarme) == (1)) {
digitalWrite(triggerPort, LOW);
digitalWrite(triggerPort, HIGH );
delayMicroseconds( 10 );
digitalWrite(triggerPort, LOW );
Serial.println("allarme2");
duration = pulseIn(echoPort, HIGH);
r = 0.034 * duration / 2;
if( duration > 38000 ) {
Serial.println( "fuori portata");
}
else {
Serial.print( r ); Serial.println( "cm" );
}
}
}
if (millis() - checkAlarmPreviousMillis > checkAlarmInterval ){
checkAlarmPreviousMillis = millis();
if ((r) < (14)) {
(isAlarmTriggered) = (1);
} else {
isAlarmTriggered = 0;
}
}
if (millis() - ActualAlarmPreviousMillis > ActualAlarmInterval ){
ActualAlarmPreviousMillis = millis();
if ((isAlarmTriggered) == (1)) {
Serial.println("allarme attivato");
digitalWrite(alarmLed, HIGH);
delay(750);
digitalWrite(alarmLed, LOW);
delay(750);
}
}
if (millis() - ActualAlarmPreviousMillis > ActualAlarmInterval ){
ActualAlarmPreviousMillis = millis();
if (isAlarmTriggered == 1) {
tone(alarmBuzzer, 330, 375);
delay(375);
tone(alarmBuzzer, 262, 375);
delay(375);
tone(alarmBuzzer, 330, 375);
delay(375);
tone(alarmBuzzer, 262, 375);
delay(375);
}
}
delay(1000);
}
if (millis() - ventolaPreviousMillis > ventolaInterval ){
ventolaPreviousMillis = millis();
Serial.println("ventola");
if ((temperature) >= (25)) {
digitalWrite(ventola, HIGH);
} else {
digitalWrite(ventola, LOW);
}
}
if (millis() - DHT11PreviousMillis > DHT11Interval ){
DHT11PreviousMillis = millis();
Serial.println("DHT11");
byte data[40] = {0};
if (dht11.read(DHT11, &temperature, &humidity, data)) {
return;
}
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" %")
delay(1000);
}
if (millis() - stepperTendaPreviousMillis > stepperTendaInterval ){
stepperTendaPreviousMillis = millis();
for (i = 0; i<1230; i++) {
tenda.step(-10); //Un passo in senso orario.
delay(1);
i = (i+1);
}
for (i = 1230; i>0; i--) {
tenda.step(10); //Un passo in senso orario.
delay(1);
i = (i-1);
}
}
if (millis() - lightsPreviousMillis > lightsInterval) {
lightsPreviousMillis = millis();
if (entrance1Led == true) {
digitalWrite(entranceLed, HIGH);
} else {
digitalWrite(entranceLed, LOW);
}
if (kitchenLed == true) {
digitalWrite(KitchenLed, HIGH);
} else {
digitalWrite(KitchenLed, LOW);
}
if (bedroomsLed == true) {
digitalWrite(BedroomsLed, HIGH);
} else {
digitalWrite(BedroomsLed, LOW);
}
}
if (millis() - keypadPreviousMillis > keypadInterval ){ //temperature
keypadPreviousMillis = millis();
code1[0] = keypad.getKey();
Serial.println(code1[0]);
if ( (code1[0] == '1') ) {
digitalWrite(3, HIGH);
keypad.setDebounceTime(200);
}
code2[0] = keypad.getKey();
if ( (code2[0] == '2') ) {
digitalWrite(4, HIGH);
keypad.setDebounceTime(200);
}
code3[0] = keypad.getKey();
if ( (code3[0] == '3') ) {
digitalWrite(5, HIGH);
keypad.setDebounceTime(200);
}
code4[0] = keypad.getKey();
if ( (code4[0] == '4') ) {
for (i = 0; i<1230; i++) {
tenda.step(-10); //Un passo in senso orario.
delay(1);
i = (i+1);
}
for (i = 1230; i>0; i--) {
tenda.step(10); //Un passo in senso orario.
delay(1);
i = (i-1);
}
keypad.setDebounceTime(200);
}
code5[0] = keypad.getKey();
if ( (code5[0] == '5') ) {
allarme = 1;
keypad.setDebounceTime(200);
}
code6[0] = keypad.getKey();
if ( (code6[0] == '*') ) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
allarme = 0;
keypad.setDebounceTime(200);
}
keypad.setDebounceTime(200);
}
}
If you could help me with the keypad and if you could tell me if the code's OK, I would be very grateful. I have to finish it today 23/06/2018.
Thanks