multitasking involving main program and a shock sensor function

Hi there,

Can someone guide me in linking up these 2 functions together?

My program is a locking device that is unlocked via keypad password input.

When it is locked, the shock sensor function arms the system.

At the same time it polls and wait for someone to come and unlock it, a successful attempt will disable the system.

if( !strcmp( pswbuf, numbuf) ) { // This will only be true if the passwords are equal,comparing them 1 pair at a time

        Serial.println("Password Correct");
        lcdSetXY(0,0); 
        lcdPutStr("Unlocked");
        Serial.println(" Unlocked");
        pinMode(10,OUTPUT); // this is usually declared in the setup function
        digitalWrite(10,HIGH); 
        delay(10000);
        digitalWrite(10,LOW);
        Serial.println("Shock system activated");
        knockSensor();

Apparently this code does not allows it to poll for user input. It will just exit the function.

Any suggestions?

I'm not sure exactly what you are trying to achieve so this is a bit of a guess ...

It seems to me that the code should be something like this pseudo code

void loop() {
    readInputFromKeypad();
    validatePassword();
    operateLock();
}

void readInputFromKeypad() {
   // if there is data from the keypad save it in tempPwd
}

void validatePassword() {
   // if the pwd is correct pwdValid = true else pwdValid = false
}

void operateLock() {
  // if pwdValid == true open the lock
}

Separating the activities into different functions makes it easier to follow the logic. The code will continuously cycle through the three functions and will open the lock when appropriate. (Presumably there also needs to be some arrangement to close the lock).

I'm assuming the slight delay while the code is reading the input from the keypad doesn't matter enough to incur the complication of reading the input one character at a time while the rest of the code runs.

...R

Hi, Thanks for your response.

Well, what you had proposed generally cycles the program like what you had mentioned.

However. a knock sensor function would have to end and move on to the next function.

I require this function to be running 24hrs only pausing when my password is correct to disarm it.

Does this makes it clearer?

elmo1440:
However. a knock sensor function would have to end and move on to the next function.

I don’t understand what you mean by this or why you think it wouldn’t be compatible with my approach?

Running 24/7 is no problem.

…R

Lets say

readInputFromKeypad();
           validatePassword();
          operateLock();
           activateShockSensor():

My shock sensor keeps checking and checking for disturbances never moving out to read user input at keypad.

If I end my shockSensor function, my device becomes vulnerable.

Any Idea what should be done?

What is the nature of the shock sensor, and if you're polling it how often does it need to be polled in order to detect a knock?

Sorry, @elmo1440, I don't understand your question, probably because I have no idea what the role of the shock sensor is, or how it is implemented.

Can you explain the overall project in more detail.

...R

Hi everyone, Thanks for the keen interest in helping me out.

Well I removed this shock sensor module off a car. It has 3 wires, vcc, ground and signal.

I have program my arduino such that when my shock sensor send a low which indicate vibration, pin 10 (buzzer) will sound off.

Such is the function of shock sensor.

What I cant understand is actually having this function running continously until my user entered the correct password. Which will allows another function to lock my system and after which arms it again.

Sorry for not being clear enough earlier on. I hope this is easier to comprehend.

Sorry, I still don't understand.

It sounds like the sensor responds to vibration, but you haven't explained what causes the vibration or why or when your program needs to take account of vibration.

And will a gentle vibration set it off, or does it need a strong shock?

Does the sensor produce a constant signal or just a pulse for every vibration? How long are the pulses?

There are probably other useful questions as well ...

...R

elmo1440: Hi everyone, Thanks for the keen interest in helping me out.

Well I removed this shock sensor module off a car. It has 3 wires, vcc, ground and signal.

I have program my arduino such that when my shock sensor send a low which indicate vibration, pin 10 (buzzer) will sound off.

Such is the function of shock sensor.

What I cant understand is actually having this function running continously until my user entered the correct password. Which will allows another function to lock my system and after which arms it again.

Sorry for not being clear enough earlier on. I hope this is easier to comprehend.

Hi, you might use hardware pin interrupt on your sensor so when signal goes low security trips. Look into interrupts for detales.

Don't use interrupts unless you know how to debug them.

I reckon if the OP could use interrupts he would also be able to sort out the problem that is at the centre of this Thread.

...R

Before you can decide how to detect a signal, you need to understand what sort of signal you're trying to detect. The questions below are crucial.

Robin2: Does the sensor produce a constant signal or just a pulse for every vibration? How long are the pulses?

OOps, I think I have misleaded you guys. :cold_sweat:

My shock sensor is working fine. My arduino recognised the Low signal coming in from the sensor whenever there is a slight push on the object which it is mounted on. :)

What I am facing is the problem of letting this program or rather arduino be detecting the signal 24/7 while waiting for the user to key in the correct password. Only when it is correct, the program exits this shock sensor function momentarily. Until it is locked again.

What my program structure looks like now.

Await User password input(); // user input from keypad ComparePSW(); // check the password against stored array LockActivated(); // If ComparePSW shows password are the same ShockSensorActivated(); //Automatically when lock is closed

All works well, sequentially. However as it cycles back to the top for the user were to key in password (await user input function), my shocksensor function would have to be exited(The last function). This leaves my system vulnerable.

Is there a way to structure it so that shocksensor function run along throughout the steps only exiting or stopping when my user input is the correct password.

Many Thanks.

elmo1440: OOps, I think I have misleaded you guys. :cold_sweat:

My shock sensor is working fine. My arduino recognised the Low signal coming in from the sensor whenever there is a slight push on the object which it is mounted on. :)

You haven't mislead me. You have just repeatedly failed to answer questions. What the shock sensor is for - why are you using it? What sort of output signal does it produce - continuous (for several seconds) or short pulses (how long are the pulses).

If I understand that I may be able to figure out how it should be integrated into the program.

...R

Okay. I just didn’t see the link between the works of a shock sensor and the way my program is structured.

What the shock sensor is for - why are you using it?

It is to detect vibrations, used in a car such that when it is parked stationary and someone bangs on to it or it is being towed away it will send a low signal to a processor.

I am using it to arm my bicycle kiosk. So that people doesn’t vandalize it. When my arduino receive a low input, output pin connected to buzzer will sound.

What sort of output signal does it produce - continuous (for several seconds) or short pulses (how long are the pulses).

As observed from an oscilioscope, output is observed to be a single low pulse approximately 10ms per shake.

Thanks