Make Program wait for an input

Hi everyone,
I am trying to figure out how to make this program wait until a knock sensor activates it. Tried looking around but don’t understand what I’m doing wrong. Everything in the sketch works until I entered the knock sensor portion. Now nothing happens. I found the “while” idea here but I must be doing it wrong. Any help appreciated.

//Haunted Magic Book project

// Next I will replace each LED with a motor or other function
//adjusting the delays accordingly.
//delays between cases are short for testing. I will increase them later 

#include <Servo.h>
Servo myservo;
int pos = 0;
int i = 0;
const int pwmLED = 5;  //fading led

int fadeAmount = 5;                    // how many points to fade the LED by
int leds[8] = {2, 3, 4, 5, 6, 7, 8, 10};    //To control the 8 LEDS--for test only
int ranNum;
int ranDel;

int THRESHOLD = 235;  //how hard to tap on book
byte val = 0;
int KnockSensor = A4;
void setup() {

  Serial.begin  (9600);
  pinMode(2, OUTPUT);  // Setup 8 LEDs as Outputs
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(pwmLED, OUTPUT);


  randomSeed(analogRead(A0));     // unused (floating) analog pin.

}
void loop() {

//-------------------------KnockSensor--------------------------------------
  val = analogRead(KnockSensor);
  while (val < THRESHOLD) {        //Don't do anything
    while (val >= THRESHOLD) {       //go on
            resume();
//--------------------------------------------------------------
    }
  }
}
void resume()   {                  //begin actions
  digitalWrite (12, HIGH);         //to test peizo knock
  delay(3000);
  digitalWrite (12, LOW);

  ranNum = random(2, 10);      //Generate random number between 8 and 12
  ranDel = random(100, 250);          // random delay time
  int randChoose = random(8);

  switch (randChoose) {     //choose a case

    //-----------------CASES---------
    // ----------------------------------Evil Laughter-------------------- -
    case 0 :  digitalWrite(2, HIGH);  //Play the recording
      delay(2200);
      digitalWrite(2, LOW);

      resume();
    //-------------Book open and close-->WITH LIGHT--------------------------------------
    case 1 :
      myservo.attach(3);
      for (pos = 0; pos <= 35; pos += 1) {   // goes from 0 degrees to 35 degrees  Stepsin steps of 1 degree

        myservo.write(pos);                    // tell servo to go to position in variable 'pos'
        digitalWrite(11, HIGH);                 //Led On
        delay(100);                            // go slowly in this directin
      }
      delay(1500);                            //wait 1.5 sec
      for (pos = 35; pos >= 0; pos -= 1) {     // return to 0 degrees
        myservo.write(pos);
        digitalWrite (11, LOW);               //led low
        delay(15);                               // waits 15ms for the servo to reach the position
      }
      myservo.detach();                          //stop right there before you go any further

      resume();
    //-----------------------------------Unknown---------------------
    case 2 : digitalWrite(4, HIGH);     //turn on led on Pin 4---to be replaced ith an action
      delay(4000);
      digitalWrite(4, LOW);

      resume();
    //-----------------//Green Light Fade IN and OUT  PIN 5 -------------------------------------------
    case 3 :
      for (int i = 0; i < 255; i++) {    //PIN 5 if i is less than 255 then increase i with 1
        analogWrite(pwmLED, i);                //write the i value to pin 5
        delay(20);                             //wait 5 ms then do the for loop again
      }
      for (int i = 255; i > 0; i--) {           //descrease i with 1
        analogWrite(pwmLED, i);
        delay(5);
        analogWrite(pwmLED, LOW);                //turn it off
        delay(5);
      }
      resume();
    //---------------------Unknown------------------------
    case 4 :
      digitalWrite(6, HIGH);  //to be replaced with an action
      delay(4000);
      digitalWrite (6, LOW);

      resume();
    //---------------Rumble Motor---------------------------
    case 5 :   digitalWrite(10, HIGH);  //Motor with rumble bar will shake
      delay(1000);
      digitalWrite(10, LOW);        //stop right there before you go any further

      resume();
    //------------------Book Move----------------------------
    case 6 :  digitalWrite(7, HIGH);   //motor with wheel on bottom will scoot the book slighty
      delay(50);
      digitalWrite (7, LOW);

      resume();
    //--------------------------Book Open And close, No Lights ----------------------
    case 7 :  myservo.attach(3);
      for (pos = 0; pos <= 35; pos += 1) {   // goes from 0 degrees to 35 degrees  Stepsin steps of 1 degree

        myservo.write(pos);                    // tell servo to go to position in variable 'pos'
        delay(100);                            // go slowly in this directin
      }
      delay(1500);                            //wait 1.5 sec
      for (pos = 35; pos >= 0; pos -= 1) {     // return to 0 degrees
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(20);                               // waits 15ms for the servo to reach the position
      }
      myservo.detach();

      resume();
  }
}

Try by changing byte val = 0; to int val = 0;.

resume appears to be able to recursively call itself.

That seems to me to be unwise.

Thanks

Okay
Changed val to an integer
deleted "resume"

Still no luck

Sorry. Had to wait 5 min

I'll just add, the sketch works fine without the knock part and a short sketch to test the piezo works.

This is not going to work

  val = analogRead(KnockSensor);
  while (val < THRESHOLD) {        //Don't do anything
    while (val >= THRESHOLD) {       //go on
            resume();
//--------------------------------------------------------------
    }
  }

because nothing changes the content of val inside the WHILE

Try this

  val = analogRead(KnockSensor);
  while (val < THRESHOLD) {        //Don't do anything
    val = analogRead(KnockSensor);
  }

…R

Nope

Tried this:

val = analogRead(KnockSensor);
while (val < THRESHOLD) { //Don’t do anything

val = analogRead(KnockSensor);
while (KnockSensor > THRESHOLD) { //go on

I don’t understand while it stops and won’t go on once I tap the sensor.

We can’t see your code

Code again

//Haunted Magic Book project

// Next I will replace each LED with a motor or other function
//adjusting the delays accordingly.
//delays between cases are short for testing. I will increase them later

#include <Servo.h>
Servo myservo;
int pos = 0;
int i = 0;
const int pwmLED = 5;  //fading led

int fadeAmount = 5;                    // how many points to fade the LED by
int leds[8] = {2, 3, 4, 5, 6, 7, 8, 10};    //To control the 8 LEDS--for test only
int ranNum;
int ranDel;

int THRESHOLD = 235;  //how hard to tap on book
int val = 0;
int KnockSensor = A4;
void setup() {

  Serial.begin  (9600);
  pinMode(2, OUTPUT);  // Setup 8 LEDs as Outputs
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(pwmLED, OUTPUT);


  randomSeed(analogRead(A0));     // unused (floating) analog pin.

}
void loop() {
  val = analogRead(KnockSensor);
  while (val < THRESHOLD) {        //Don't do anything
    
    val = analogRead(KnockSensor);       
    while (KnockSensor > THRESHOLD) { //go on
    
    }
}
}
void resume()   {                  //begin actions
  digitalWrite (12, HIGH);         //to test peizo knock
  delay(3000);
  digitalWrite (12, LOW);

  ranNum = random(2, 10);      //Generate random number between 8 and 12
  ranDel = random(100, 250);          // random delay time
  int randChoose = random(8);

  switch (randChoose) {     //choose a case

    //-----------------CASES---------
    // ----------------------------------Evil Laughter-------------------- -
    case 0 :  digitalWrite(2, HIGH);  //Play the recording
      delay(2200);
      digitalWrite(2, LOW);

      resume();
    //-------------Book open and close-->WITH LIGHT--------------------------------------
    case 1 :
      myservo.attach(3);
      for (pos = 0; pos <= 35; pos += 1) {   // goes from 0 degrees to 35 degrees  Stepsin steps of 1 degree

        myservo.write(pos);                    // tell servo to go to position in variable 'pos'
        digitalWrite(11, HIGH);                 //Led On
        delay(100);                            // go slowly in this directin
      }
      delay(1500);                            //wait 1.5 sec
      for (pos = 35; pos >= 0; pos -= 1) {     // return to 0 degrees
        myservo.write(pos);
        digitalWrite (11, LOW);               //led low
        delay(15);                               // waits 15ms for the servo to reach the position
      }
      myservo.detach();                          //stop right there before you go any further

      resume();
    //-----------------------------------Unknown---------------------
    case 2 : digitalWrite(4, HIGH);     //turn on led on Pin 4---to be replaced ith an action
      delay(4000);
      digitalWrite(4, LOW);

      resume();
    //-----------------//Green Light Fade IN and OUT  PIN 5 -------------------------------------------
    case 3 :
      for (int i = 0; i < 255; i++) {    //PIN 5 if i is less than 255 then increase i with 1
        analogWrite(pwmLED, i);                //write the i value to pin 5
        delay(20);                             //wait 5 ms then do the for loop again
      }
      for (int i = 255; i > 0; i--) {           //descrease i with 1
        analogWrite(pwmLED, i);
        delay(5);
        analogWrite(pwmLED, LOW);                //turn it off
        delay(5);
      }
      resume();
    //---------------------Unknown------------------------
    case 4 :
      digitalWrite(6, HIGH);  //to be replaced with an action
      delay(4000);
      digitalWrite (6, LOW);

      resume();
    //---------------Rumble Motor---------------------------
    case 5 :   digitalWrite(10, HIGH);  //Motor with rumble bar will shake
      delay(1000);
      digitalWrite(10, LOW);        //stop right there before you go any further

      resume();
    //------------------Book Move----------------------------
    case 6 :  digitalWrite(7, HIGH);   //motor with wheel on bottom will scoot the book slighty
      delay(50);
      digitalWrite (7, LOW);

      resume();
    //--------------------------Book Open And close, No Lights ----------------------
    case 7 :  myservo.attach(3);
      for (pos = 0; pos <= 35; pos += 1) {   // goes from 0 degrees to 35 degrees  Stepsin steps of 1 degree

        myservo.write(pos);                    // tell servo to go to position in variable 'pos'
        delay(100);                            // go slowly in this directin
      }
      delay(1500);                            //wait 1.5 sec
      for (pos = 35; pos >= 0; pos -= 1) {     // return to 0 degrees
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(20);                               // waits 15ms for the servo to reach the position
      }
      myservo.detach();

      resume();
  }
}

resume can call itself recursively.

I thought you said you’d fixed that?

I did remove the one I thought you were speaking about.
I think the Resume(); in the cases is necessary

Polliwog:
I think the Resume(); in the cases is necessary

OK.
Let us know how it works out.

If I don't use the "void resume" how do I make it return to that point after each case?

1. From literate programming point of you, your loop() function stands as:

void loop()
{
  val = analogRead(KnockSensor);
  while (val < THRESHOLD)
  { //Don't do anything
    val = analogRead(KnockSensor);
    while (KnockSensor > THRESHOLD)
    { //go on

    }
  }
}

2. Do you desire that the codes of the loop() function are designed to do the following?
(1) If val is less than THRESHOLD, there will be no action; the MCU will read the knockSensor again. If so, why don’t you re-write the codes using if-else structure?

(2) If the val value is greater than THRESHOLD, the sketch will be doing something. If so, are you going to call the resume() subroutine? We don’t see any existence of the resume() subroutine in the loop() function.

3. Could your loop() function be written like this?

void loop()
{
  val = analogRead(KnockSensor);
  if(val > THRESHOLD)
  {
       resume();
  }
}

Thanks, I'll try this

But you have not put any opinion as to the justification of my moderation on your loop() function.

Hello everyone:
I want to thank you all. With your help and a little more research, I came up with this and the sketch does everything how I wanted it too.

void loop() {

  sensor();
  resume();
}


void sensor() {
  val = analogRead(KnockSensor);
  if (val < THRESHOLD) {

    sensor();
  }
  else  {
    resume();
  }
}

void resume() {
  delay (1000);
  ranNum = random(2, 10);      //Generate random number between 8 and 12
  ranDel = random(100, 250);          // random delay time
  int randChoose = random(8);
  switch (randChoose) {     //choose a case

    //-----------------CASES----

Polliwog:
Nope

Tried this:

val = analogRead(KnockSensor);
while (val < THRESHOLD) { //Don’t do anything

val = analogRead(KnockSensor);
while (KnockSensor > THRESHOLD) { //go on

I don’t understand while it stops and won’t go on once I tap the sensor.

Why didn’t you try what I suggested? My suggestion is actually simpler. Why did you add the line

 while (KnockSensor > THRESHOLD) {

Jeez!

…R

Hi,
What model Arduino are you using?

Does the knock sensor portion of your code work on its own?

Can you post just the knock sensor code?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom...

GolamMostafa:
But you have not put any opinion as to the justification of my moderation on your loop() function.

You were correct, I changed what was in and outside the loop function.
Thanks