start reading analog value after key is pressed

Hello everyone,

I have a question about my project using an Arduino Nano, a buzzer, an LDR and a keypad.

My Arduino should start reading the analog value of the LDR after number 4 is pressed on the keypad. When this value comes above a certain threshold the buzzer should activate. The buzzer can be deactivated with a password.

My question is how can I make sure that the Arduino starts reading the LDR value after key 4 is pressed?

void loop()
{
  char key = keypad.getKey();
  
  if (key){
        Serial.println(key);
     if (key == '4'){
      Serial.print("start reading ldr value");
     }}
         
  int ldrvalue = analogRead(ldrpin);
  Serial.println(ldrvalue);
  if(ldrvalue>200)
  
  { 
    Serial.print("buzzer activated          ");
      digitalWrite(buzzpin, HIGH);
     }
  
   char key2 = kpd.getKey();
   if(key2){
   Serial.println(key2);
             
    data[sequenceNum] = key2;
    sequenceNum++;

    if (sequenceNum == 4){
      #ifdef DEBUG
      Serial.print(F("Code entered: "));
      Serial.println(data);
      #endif

      if(strcmp(data, "2133") == 0){
        Serial.print("Correct   ");
        Serial.print("Buzzer disabled");
        digitalWrite(buzzpin, LOW);
        delay(100000000);
      }
      else 
        Serial.print("Wrong");
        
      memset(data, 0, sizeof(data));
      sequenceNum = 0;
      
    }}}

Thank you in advance.

In the code you posted only the

     Serial.print("start reading ldr value");

is dependant on the 4 key. Put the LDR reading code inside the same code block.

First thank you for you help.
Do you mean it like this? because, now I get an error saying that 'ldrvalue' is not declared in the scope.

if (key){
        Serial.println(key);
     if (key == '4'){
      Serial.print("start reading ldr value");
        int ldrvalue = analogRead(ldrpin);
        Serial.println(ldrvalue);
     }}
      if(ldrvalue>200) 
  { 
    Serial.print("buzzer activated          ");
      digitalWrite(buzzpin, HIGH);
     }

Here is your code better formatted

if (key)
{
  Serial.println(key);
  if (key == '4')
  {
    Serial.print("start reading ldr value");
    int ldrvalue = analogRead(ldrpin);
    Serial.println(ldrvalue);
  }
}
if (ldrvalue > 200)
{
  Serial.print("buzzer activated          ");
  digitalWrite(buzzpin, HIGH);
}

It shows up the problem

ldrvalue is declared local to the if (key =='4') code block, hence the error

It is very weird but i'm still getting the same error, that ldr value is not declared in this scope.

Post your corrected code

#include <Keypad.h>

#define ldrpin A4
#define buzzpin 2

#define DEBUG

const byte ROWS =1;
const byte COLS1 = 1;
const byte COLS2 = 3;
char keys[ROWS][COLS1] = {
  {'4'}
};
char keys2[ROWS][COLS2] = {
  {'1','2','3'}
};


byte rowPins[ROWS] = {5};
byte colPins1[COLS1] = {7};
byte colPins2[COLS2] = {8,9,6};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins1, ROWS, COLS1);
Keypad kpd = Keypad(makeKeymap(keys2), rowPins, colPins2, ROWS, COLS2);


char data[] = "    ";
int sequenceNum = 0;


void setup()

{
  #ifdef DEBUG
  Serial.begin(9600);
  #endif
  pinMode(ldrpin,INPUT);
  pinMode(buzzpin,OUTPUT);
}

void loop()
{
  char key = keypad.getKey();
  
if (key)
{
  Serial.println(key);
  if (key == '4')
  {
    Serial.print("start reading ldr value");
    int ldrvalue = analogRead(ldrpin);
    Serial.println(ldrvalue);
  }
}
if (ldrvalue > 200)
{
  Serial.print("buzzer activated          ");
  digitalWrite(buzzpin, HIGH);
}
  
   char key2 = kpd.getKey();
   if(key2){
   Serial.println(key2);
             
    data[sequenceNum] = key2;
    sequenceNum++;

    if (sequenceNum == 4){
      #ifdef DEBUG
      Serial.print(F("Code entered: "));
      Serial.println(data);
      #endif

      if(strcmp(data, "2133") == 0){
        Serial.print("Correct   ");
        Serial.print("Buzzer disabled");
        digitalWrite(buzzpin, LOW);
        delay(100000000);
      }
      else 
        Serial.print("Wrong");
        
      memset(data, 0, sizeof(data));
      sequenceNum = 0;
      
    }}}

No, that's still got the restricted scope of ldrvalue.
Try making it a global instead.

I'm sorry but I really don't know how to make the ldr value a global value without reading that value the whole time. If I put the analog read statement out of the local scope the buzzer is activated before the key 4 is pressed.

To make ldrvalue global declare it at the start of the program and from then on just use it with no type specifier in front of its name

Maybe i should frame my question different because i'm not really getting what your are saying. f
First my working script looked like this:

void loop()
{
  char key = keypad.getKey();
  int ldrvalue = analogRead(ldrpin);
  delay(100);
  Serial.println(ldrvalue);
  
  
if (ldrvalue > 200)
{
  Serial.print("buzzer activated          ");
  digitalWrite(buzzpin, HIGH);
}
    char key2 = kpd.getKey();
   if(key2){
   Serial.println(key2);
             
    data[sequenceNum] = key2;
    sequenceNum++;

    if (sequenceNum == 4){
      #ifdef DEBUG
      Serial.print(F("Code entered: "));
      Serial.println(data);
      #endif

      if(strcmp(data, "2133") == 0){
        Serial.print("Correct   ");
        Serial.print("Buzzer disabled");
        digitalWrite(buzzpin, LOW);
        delay(100000000);
      }
      else 
        Serial.print("Wrong");
        
      memset(data, 0, sizeof(data));
      sequenceNum = 0;
      
    }}}

But now i want to start this loop only if button 4 is pressed. Can i in some way don't start the loop and only start it when button 4 is being pressed on the keypad?

edit:
I have read about the wait for key function from the arduino keypad library but i don't get that to work maybe you know if I can use that in my case or not.

i want to start this loop only if button 4 is pressed

Now I am confused. What exactly do you want to happen when button 4 is pressed and should it continue after button 4 is released ?

No when button 4 is pressed and released, so just a simple click. The loop should start.

The loop should start.

What loop ?

the void loop
it should start reading the analog value of the ldr pin after button 4 is pressed and released.

OK, we are getting closer.

Let me describe what I think you are saying you want

1 - the Arduino is powered up or reset
2 - nothing should happen until button 4 is pressed and released (does it actually need to be released or will just pressing it be OK ?)
3 - the code in loop() runs until the Arduino is powered down/up or reset

Is that correct ?

Yes, that is perfectly what I mean. Button 4 but will in practice be a click so a shot press and release.

An example for you

#include <Keypad.h>

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'}
};

byte colPins[COLS] = {2, 4, 7, 8}; //column pins  (R0 - R3)
byte rowPins[ROWS] = {10, 11, 12, A0}; //row pins  (L0 - L3)
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(115200);
  Serial.println("Press key 4 to start");
  while (keypad.getKey() != '4')
  {
    //do nothing
  }
}

void loop()
{
  Serial.println("in loop()function");
  delay(1000);
}

Thank you so much now I got it working.