one button code lock



  • Can anybody help me with this project I found on the internet. It is not a MYSENSORS project but who can help me to make it working in Mysensors? I use a serial gateway to Domoticz. I want to use a match of the code in Domoticz.

    This is the code:

    int count=0;
    unsigned long a[]={0,0,0,0,0};
    unsigned long b[]={0,0,0,0,0};
    unsigned long del[]={1000,1000,1000,5000}; 
    
    float tolerance=.4;
    
    void setup()
    {
    pinMode(2, INPUT);
    pinMode(10, OUTPUT);
    pinMode(13, OUTPUT);
    }
    
    void dataCapture()
    {
    a[count]=millis();
    delay(500); // for button debounce, your value may differ
    count++;
    }
    
    void checkCombination()
    {
    int compare=0;
    count=0;
    // calcualate delays between keypresses
    b[0]=a[1]-a[0];
    b[1]=a[2]-a[1];
    b[2]=a[3]-a[2];
    b[3]=a[4]-a[3];
    
    // compare the button delay values 
    if (b[0]<=(del[0]*(1+tolerance)) && b[0]>=(del[0]*(1-tolerance))) { compare++; }
    if (b[1]<=(del[1]*(1+tolerance)) && b[1]>=(del[1]*(1-tolerance))) { compare++; } 
    if (b[2]<=(del[2]*(1+tolerance)) && b[2]>=(del[2]*(1-tolerance))) { compare++; }
    if (b[3]<=(del[3]*(1+tolerance)) && b[3]>=(del[3]*(1-tolerance))) { compare++; }
    if (compare==4) { digitalWrite(10,HIGH); }
    delay(3000); 
    digitalWrite(10,LOW);
    if (compare!=4) { digitalWrite(13,HIGH); }
    delay(3000);
    digitalWrite(13,LOW);
    
    
    }
    
    void loop()
    {
    if (digitalRead(2)==HIGH)
    {
    dataCapture();
    }
    if (count>4)
    {
    checkCombination();
    }
    }
    

  • Mod



  • this is the code untill now with one compilation error. I installed the Arduino IDE and add the Mysensors master to it but it did not solve the proble.

    
    #include <SPI.h>
    #include <MySensors.h>
    
    //#define SKETCH_NAME "One Button Code Lock"
    #define CHILD_ID_CODE 5
    #define RELAY_PIN 3 //Arduino Digital I/O pin number for relay
    #define BUTTON_PIN  2  // Arduino Digital I/O pin for button/reed switch
    #define LED_PIN 4
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    MySensor gw;
    
    
    //original code code_lock
    int count=0;
    unsigned long a[]={0,0,0,0,0};
    unsigned long b[]={0,0,0,0,0};
    unsigned long del[]={1000,1000,1000,5000}; 
    
    float tolerance=.4;
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    void setup()
    {
      gw.begin();
    pinMode(BUTTON_PIN, INPUT);
    pinMode(RELAY_PIN, OUTPUT);
    pinMode(LED_PIN, OUTPUT);
    }
    
    void dataCapture()
    {
    a[count]=millis();
    delay(500); // for button debounce, your value may differ
    count++;
    }
    
    void checkCombination()
    {
    int compare=0;
    count=0;
    // calcualate delays between keypresses
    b[0]=a[1]-a[0];
    b[1]=a[2]-a[1];
    b[2]=a[3]-a[2];
    b[3]=a[4]-a[3];
    
    // compare the button delay values 
    if (b[0]<=(del[0]*(1+tolerance)) && b[0]>=(del[0]*(1-tolerance))) { compare++; }
    if (b[1]<=(del[1]*(1+tolerance)) && b[1]>=(del[1]*(1-tolerance))) { compare++; } 
    if (b[2]<=(del[2]*(1+tolerance)) && b[2]>=(del[2]*(1-tolerance))) { compare++; }
    if (b[3]<=(del[3]*(1+tolerance)) && b[3]>=(del[3]*(1-tolerance))) { compare++; }
    if (compare==4) { digitalWrite(RELAY_PIN,HIGH); }
    delay(3000); 
    digitalWrite(RELAY_PIN,LOW);
    if (compare!=4) { digitalWrite(LED_PIN,HIGH); }
    delay(3000);
    digitalWrite(LED_PIN,LOW);
    gw.present(CHILD_ID, S_DOOR);
    
    }
    
    void loop()
    {
    if (digitalRead(2)==HIGH)
    
    gw.send(msg.set(value==HIGH ? 1 : 0));
    
    {
    dataCapture();
    }
    if (count>4)
    {
    checkCombination();
    }
    }
    
    
    
    

    the error code during compilation is
    Using library SPI at version 1.0 in folder: C:\Program Files (x86)\arduino-1.6.11\hardware\arduino\avr\libraries\SPI
    exit status 1
    Error compiling for board Arduino Nano.
    This is also during the selection of an UNO.
    Any idea???
    And is this code working using the 1.5.4. lib.



  • anybodey an idea?


  • Contest Winner

    @Dick I'm sorry my friend. This kind of projects our my favorites to get into. But I'm a bit too occupied at the moment. Maybe in the weekend after next, if somebody else hasn't help you before that.



  • @TheoL Nevermind, I am on holiday and I only prepare some projects and this was one of them. If you have time later this week or next week, take your time. Compliment for your website. strait forward and it giave me also some ideas about houtbewerking.
    have a nice evening.


  • Mod

    @Dick the code you posted above is for MySensors 1.x. That is fine, I'm still using version 1.5 myself. But the code won't work with MySensors 2.0 installed, you need to have only MySensors 1.5 installed. MySensors 1.5 uses

    #include <MySensor.h>
    

    while 2.0 uses

    #include <MySensors.h>
    

    (notice the added "s").

    If you want to convert the code to use MySensors 2.0, use this guide.

    If I change the code to use MySensor.h, I get the following compilation errors:

    Build options changed, rebuilding all
    sketch_aug31a:23: error: 'CHILD_ID' was not declared in this scope
    
     MyMessage msg(CHILD_ID, V_TRIPPED);
    
                   ^
    
    C:\Users\Micke\AppData\Local\Temp\arduino_modified_sketch_587105\sketch_aug31a.ino: In function 'void checkCombination()':
    
    sketch_aug31a:73: error: 'CHILD_ID' was not declared in this scope
    
       gw.present(CHILD_ID, S_DOOR);
    
                  ^
    
    C:\Users\Micke\AppData\Local\Temp\arduino_modified_sketch_587105\sketch_aug31a.ino: In function 'void loop()':
    
    sketch_aug31a:81: error: 'value' was not declared in this scope
    
         gw.send(msg.set(value == HIGH ? 1 : 0));
    
                         ^
    
    exit status 1
    'CHILD_ID' was not declared in this scope
    

    The error message says that the compiler doesn't know what CHILD_ID is when it is trying to look at

    MyMessage msg(CHILD_ID, V_TRIPPED);
    

    That is because CHILD_ID has not been defined. I think you meant to use

    #define CHILD_ID 5
    

    instead of

    #define CHILD_ID_CODE 5
    

    The next error,

    sketch_aug31a:81: error: 'value' was not declared in this scope
         gw.send(msg.set(value == HIGH ? 1 : 0));
    

    means that the compiler is unable to find out what "value" is, since it has not been used before. Depending on what logic you want here, different things need to be done.

    Use the code formatting tool in the Arduino IDE (Tools->Auto format) to make the code easier to read. It helps a lot when trying to spot mistakes.



  • Thanks for the reply. I modified the items you stated in your reply. I did not know the use of the "auto format" tools. that makes life easier. About the logic I want to use is the status of the relay "digitalWrite(RELAY_PIN, HIGH); " so I can use that info in Domoticz.


  • Mod

    @Dick in that case

    gw.send(msg.set(HIGH));
    

    should be sufficient.


  • Contest Winner

    @Dick Thank you for the compliments on my website. This is the one of the reasons why I've started with it. I know have been a slacker regarding my blog. But I've got a vacation planned at the end of September and I need to finish some smaller projects before the winter sets in.



  • @mfalkvidd , I added the change you advised and no erroros in the compilation anymore.
    I will post the result after my testing of the project. Thanks for the support.



  • @TheoL Finish what is needed, 
    mfalkvidd gave me already some advise so next week I can test the project. I hope you have a good holiday. Again about the other hobby, working with wood, it is also one of my hobbies. Most of the time only creating housings for my projects. next year I want to start an CNC project.


  • Contest Winner

    @Dick I love CNC projects. I want to do my first lamination project as a housing for a MySensors project. And also would love the build the Japanese lamp for which I've created the gesture controlled lamp.

    I recently purchased a decent table saw from the LIDL. Well it's better than what I have. So I hope to speed up my projects. I've mounted it last Saturday and it works. I've got a new 2000W tool 😉


Log in to reply
 

Suggested Topics

  • 4
  • 9
  • 15
  • 3
  • 8
  • 2

12
Online

11.4k
Users

11.1k
Topics

112.7k
Posts