#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
    Private wifi As ESP8266WiFi
    Public Serial1 As Serial
    Public mykey As Byte
    Private password As String = "12345678" 'the password is 12345678, then press "#" to confirm password inserted!
    Private passwordBuffer(password.Length) As Byte
    Private passwordIndex As Int
    dim bc as ByteConverter
    Dim timer1 As Timer
End Sub
 
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
 
If wifi.Connect2("DDIS1", "password1") Then
        Log("Connected to network ")
    Else
        If wifi.Connect2("SSID2", "password2") Then
            Log("Connected to network ")
        Else
             If wifi.Connect2("SSID3", "password3") Then
                 Log("Connected to network ")
            Else
               Log("Failed to connect to network")
            End If
        End If
    End If
     timer1.Initialize("timer1_tick", 5000) 'you have 5 seconds for each number digit, after password will be resetted
 
    'reset password....
    clearpassword
    AddLooper("looperx")
End Sub
 
#if c
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// For Arduino Microcontroller
//byte rowPins[ROWS] = {9, 8, 7, 6};
//byte colPins[COLS] = {5, 4, 3, 2};
// For ESP8266 Microcontroller
byte rowPins[ROWS] = {D1, D2, D3, D4};
byte colPins[COLS] = {D5, D6, D7};
// For ESP32 Microcontroller
//byte rowPins[ROWS] = {23, 22, 3, 21};
//byte colPins[COLS] = {19, 18, 5, 17};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
 
void loop2(B4R::Object* o) {
  // Serial.println('xxx');
  char key = keypad.getKey();
  //b4r_main::_mykey =  (char) 0;
  if (key){
     b4r_main::_mykey = key;
    //Serial.println(key);
  }
}
#End If
 
 Sub looperx
 
     mykey=0
    RunNative( "loop2" , Null )
    If mykey<>0 Then
        timer1.Enabled=False
        timer1.Enabled=True
        Pad_KeyPressed (mykey)
        mykey=0
    End If
End Sub
Sub timer1_tick
    clearpassword
    Log("password cleared!!!")
    timer1.Enabled=False
End Sub
Sub clearpassword
    timer1.Enabled=False
    passwordIndex = 0
    For x=0 To password.Length-1
        bc.ArrayCopy2("-",0,passwordBuffer,x,1)
    Next
End Sub
Sub Pad_KeyPressed (Key As Byte)
    Log("key: ", Key) 'for debug only
    If (Key) = 42 Then '42 is * char
        Log("asc: " , Asc(Key))
        Log("cleared")
        clearpassword
        Return
    End If
 
 
    If passwordIndex >= passwordBuffer.Length Then
        passwordIndex=0
    End If
 
    If (passwordIndex < passwordBuffer.Length) And (Key<>35) Then
        Log("again...")
        'put the key in the buffer.
        passwordBuffer(passwordIndex) =  (Key)
        Log("asc: " , Asc(Key))
        passwordIndex = passwordIndex + 1
        Log(passwordBuffer)
    End If
 
    'if you press # you confirm password inserted
    If Key=35  Then
          'if right password
          If passwordBuffer = password Then
              Log("Well done!!!")
            Log("Well done!!!")
            Log("Well done!!!")
            Log("Well done!!!")
            Log("Well done!!!")
 
          'otherwise...ERROR!!!!!
          Else
            Log ("CODE ERROR!!!")
            Log ("CODE ERROR!!!")
            Log ("CODE ERROR!!!")
          End If
          Log("cleared2")
    
          clearpassword
    End If
 
 
End Sub