I'm designing a safe lock of 4 digits, but the password is in terms of colors, I have 3 colors. we are obliged to use a grayscale sensor (SKU: SEN0147).

The assignment is as follows:

Write a program on Arduino that will be used as a safe lock. Consider having 3 colors black white and grey. Black = 0, Red = 1 and White = 2.

• When the microcontroller is turned on, the safe is locked. The Serial Monitor should display: “System is online.”

• The combination lock should be saved in an array of integers called arr_code.

• The user has 3 trials to open the safe

• The system will wait for the user to enter 4 colors. To enter a color, the grayscale sensor should be place on top of the desired color. Once ready the push button is pressed. Once released, the color is saved in another array called arr_user_code.

• Once 4 colors are entered, the system should test automatically if the code entered by the user is equal to the code saved in arr_code.

• If the wrong code is entered, the serial monitor displays: “Wrong Code. Try Again.”

• If the user enters the wrong code 3 times in a row the system is locked and a message is displayed: “System Locked.”

• If the correct code is inserted, the Led on pin 13 is turned on.

• The combinations entered by the user should be displayed on the Serial Monitor. The Arduino program should be clear, separated into functions and documented.

The program I wrote is the following:

char led_pin = 13;
char pb_pin = 4;
int gs_pin = A3;

int arr_code[4] = {14, 14, 14, 14};//color im testing on
int arr_user_code[4];

int counter;
int Lock_Interval = 2500;
bool compare;

bool equal = true;

bool Array_Compare(int *a, int *b) {//function for comparision
  for (int i = 0; i < 4; i++) {
    if (a[i] != b[i]) return false;
  return true;

void Button_Wait(int buttonPin) {//function to sense if button is pressed
  int curr_state;
  int prev_state;

  while (1) {
    prev_state = curr_state;
    curr_state = digitalRead(buttonPin);
    if (curr_state != prev_state) {
      if (curr_state == false) {

int GS_Value(int gsPin) {//function to get GSsensor value
  int gs_value = analogRead(gs_pin);
  return gs_value;

void setup() {
  // put your setup code here, to run once:
  pinMode(led_pin, OUTPUT);
  pinMode(pb_pin, INPUT);


  digitalWrite(led_pin, false);//start with a turned off LED

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("System Online");
  Serial.println("Please Enter Passcode: ");

  for (counter = 0; counter < 3; counter++) {
    for (int i = 0; i < 4; i++) {
      arr_user_code[i] = GS_Value(gs_pin); //uploading GSsensor values to array !!!the problem is here!!!

      Serial.print('GS_Value');//printing GSsesnor values on monitor
      Serial.print(" ");//inserting space
    Serial.println(" ");//skipping a line after every attempt

    compare = Array_Compare( arr_code[4], arr_user_code[4]); // compare arrays and return false if not equal

    if (compare == true) {//in case arrays are equal
      Serial.println("System UNLOCKED!");
      digitalWrite(led_pin, true);

    else if (compare == false) {//in case arrays arent equal
      Serial.println("Wrong Passcode. Try Again");

  if (compare == false) {
    Serial.println("System LOCKED");
    while (1) {
      Lock_Interval = Lock_Interval * 2;

the issue is that the arr_user_code isnt actually filling with the correct sensor values but a value between 20000 and 30000. Thank you in advance.

  • How do you know the values? I don't see you printing them anywhere.
    – chrisl
    Feb 11 '19 at 0:07
  • please proofread your post
    – jsotola
    Feb 11 '19 at 4:59
  • you have no code that prints tthe value of GS_Value ........... why are you comparing arrays?
    – jsotola
    Feb 11 '19 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.