Num Pad plus Soundsensor

Hej,

I have following setup.

with a 4x4 number pad i want to start different if loops. If 1 than start 1, etc.
And I want to combine that with a soundsensor. So if i press 1, start 1 and wait for the soundsensor.

if (key =='1') {
   int (sensorData == 1) {
   if (LEDstatus==false) {
      LEDstatus=true;
      lcd.print("on");
   }
   else {
       LEDstatus=false;
       lcd.print("off");
}
}

If i do not use the num Pad the toggle between 1 and 0 on the LCD display works. With the Num pad it won't.

Can you help me out?
Thanks in advance,

Jules

Start by posting a complete program and using code tags rather than quote tags when you do

I noticed this in the snippet you posted

int (sensorData == 1)

What are you trying to do and what does it actually do I wonder ?

= is used for assignment of values
== is used for comparison on values

Why are the brackets there ?

Hej,

thanks for your reply.

Here is the code

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 

#include <Keypad.h>

const byte ROWS = 3; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'}
  //{'*','0','#'}
};

byte rowPins[ROWS] = {3, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {11,12,13}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


boolean LEDStatus=false;
int Sensor = 10;

void setup() { 
 lcd.begin(16,2);   
 pinMode(Sensor, INPUT);

}

void loop() { 

char key = keypad.getKey();
    // just print the pressed key
   if (key){
    lcd.println(key);
    delay(1500);
    lcd.clear();
    } 
   if (key =='4'){

    int SensorData=digitalRead(Sensor); 
    if(SensorData==1){
    
        if(LEDStatus==false){
            LEDStatus=true;
           
                    lcd.print("on");
            }
        else{
            LEDStatus=false;    
            lcd.print("off");
    
            }
        
     }
  }
}

SO what happens when you run the code that is different from what you want to happen ?

I note that you are changing the value of a variable named LEDStatus and testing its value but you appear to have no LED

Have you tried adding some Serial.print()s to the program so that you can tell which parts are being executed and the value of pertinent variables at important points such as just before testing their value ?

Hej,

i want it to stay in the if loop and toogle there between on and of. In this case it does it once. But i want it to react to the sensor until I press a different number in the keypad. Imagine it like choosing between different programs.

thanks. Jules

i want it to stay in the if loop

There is no such thing as an if loop. Unless the test is put in say a while loop then the test and dependant actions will occur only once

Suggestion :

Move the sensor reading code to a function, let's call it readSensor() and call it if the user entry is 4. OK so far. The trick is to check whether the number entered has changed since the last time through loop(). If there is no entry to read or the number entered has not changed since last time then call readSensor() again. If/when a different number is entered the program will not call readSensor() and you can make it do something else

ok thanks. i will give it a try.

BR