IOT031
Published © GPL3+

Keypad Controlled LED

Using a Keypad to simulate a locking mechanism with LED lights. Input the right code and the respective LED turns on.

IntermediateFull instructions provided5 hours999
Keypad Controlled LED

Things used in this project

Hardware components

Argon
Particle Argon
×2
Breadboard (generic)
Breadboard (generic)
×2
LED (generic)
LED (generic)
×3
Resistor 220 ohm
Resistor 220 ohm
×2
4x4 Membrane Keypad
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service

Story

Read more

Schematics

Circuit Diagram

This is a diagram of the circuits used for both IOT devices, not all the pins on the particle boards are there, but the pins there are all that is necessary.

Code

Keypad Code (Argon 1)

C/C++
This is the code for the argon with the Keypad, it is designed to see the order of inputs put into the keypad and if it meets certain requirements, send a signal to Argon 2 informing it that the code is correct.
// Example usage for Keypad_Particle library by Mark Stanley, Alexander Brevig.

#include <Keypad_Particle.h>

int led1 = D5; 

int led2 = D7;

// set up keypad buttons
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = { D3, D2, D1, D0 };
byte colPins[COLS] = { D6, D5, D4 };

// create Keypad object variable called "keypad"
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// change to any secret code or command of any length
const int codeLength = 3;
char secretCode[codeLength] = {'0', '8', '5'};

int val1 = 0;
int val2 = 0;
int val3 = 0;


int correctCount = 0;
boolean correctCode = false;
int uwu = 0;
int i = 0;

void myHandler(const char *event, const char *data);




void setup(){
     pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  delay(4000);
     
  Serial.begin(9600);
   delay(4000);
   Particle.subscribe("RESPONSE", myHandler, MY_DEVICES);
   pinMode(A5, INPUT);
   pinMode(A4, INPUT);
   pinMode(A3, INPUT);
   
   pinMode(A2, OUTPUT);
   pinMode(D8, OUTPUT);
   
   
   
}
  
void loop(){

    checkCode();

    if (correctCode) {

        // add instructions to do something if secret code entered correctly
		
  pinMode(led2, OUTPUT);
		  digitalWrite(led2, HIGH);
     delay(500);
     
    }
    val1 = digitalRead(A5);
   
    
    

    if (val1 == HIGH) {
        digitalWrite(A2, HIGH);
        delay(4000);
        
        val1 = 0 ;
        val2 = digitalRead(A4);
        if (val2 == HIGH) {
            digitalWrite(led2, HIGH);
            
            delay(4000);
            val2 = 0 ;
            
            val3 = digitalRead(A3);
            if (val3 == HIGH) {
                val3 = 0 ;
                digitalWrite(D8, HIGH);
               Particle.publish("CODE", "CORRECT" , PRIVATE);
                delay(4000); 
            }
        }
        }
digitalWrite(A2, LOW);
digitalWrite(led2, LOW);
digitalWrite(D8, LOW);
}
    
void myHandler(const char *event, const char *data)
{
     if (strcmp(data,"TRUE")==0){
        digitalWrite(D7,HIGH);
        delay(4000);
        digitalWrite(D7,LOW);
        }
}


    



void checkCode() {
    char key = keypad.getKey();
    // if key pressed, then check which key
    if (key) { 
        // optional: play sound as feedback when key pressed
        // tone(speakerPin, 3000, 25);

        // if key matches next key in secret code, add one to correct count
        if (key == secretCode[correctCount]) {
            correctCount++; 
        } else {
            // else wrong key was entered (reset correct count)
            correctCount = 0; 
        }
    }
    // check to see if complete secret code entered correctly
    if (correctCount == codeLength) {
        correctCode = true;
    }
    // need slight delay between key readings
    delay(500);
}

LED Code (Argon 2)

C/C++
This is the code that reads the signal sent by Argon 1, lights up an LED, then sends a signal back.
//The only goal of this code is to read an event published and see if the CODE is CORRECT or INCORRECT

int i = 0;


void setup(){
  pinMode(D7, OUTPUT);
  pinMode(D6, OUTPUT);
 
  Serial.begin(9600);
   delay(4000);
   Particle.subscribe("CODE", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data)
{
if (strcmp(data,"CORRECT")==0){
        digitalWrite(D6,HIGH);
        delay(4000);
        digitalWrite(D6,LOW);
        Particle.publish("RESPONSE", "TRUE", PRIVATE);
        }

 }

Credits

IOT031

IOT031

1 project • 0 followers

Comments