Skip to main content
expanded upon answer; refactored
Source Link
esoterik
  • 628
  • 3
  • 10

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

// edit: just gonna refactor a bit
// edit: added debounce
unsigned long start, end;end, time;
int state = 0;
...
//edit adding debounce
#define BOUNCE 10
int dbounce(unsigned long time, unsigned long start, unsigned long len)
{
    return (millis()time - timestart) > len;
}
...
void loop()
{
  pin = digitalRead(soundPin);
  time = millis();
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if dbounce(time,start,BOUNCE) 
          break; // edit: one way to debounce
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if(dbounce(end - ,start) > ,1000))
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if((pin == LOW) && dbounce(starttime,end,BOUNCE)) // another way to debounce
         state = 0; // start over  
      break;
  }
}

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

unsigned long start, end;
int state = 0;
...
//edit adding debounce
#define BOUNCE 10
int dbounce(unsigned long time, len)
{
    return (millis() - time) > len;
}
...
void loop()
{
  pin = digitalRead(soundPin);
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if dbounce(start,BOUNCE) 
          break; // edit: one way to debounce
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if((end - start) > 1000)
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if(pin == LOW) && dbounce(start,BOUNCE) // another way to debounce
         state = 0; // start over  
      break;
  }
}

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

// edit: just gonna refactor a bit
// edit: added debounce
unsigned long start, end, time;
int state = 0;
...
#define BOUNCE 10
int dbounce(unsigned long time, unsigned long start, unsigned long len)
{
    return (time - start) > len;
}
...
void loop()
{
  pin = digitalRead(soundPin);
  time = millis();
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if dbounce(time,start,BOUNCE) 
          break; // one way to debounce
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if(dbounce(end,start,1000))
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if((pin == LOW) && dbounce(time,end,BOUNCE)) //another way to debounce
         state = 0; // start over  
      break;
  }
}
expanded upon answer.
Source Link
esoterik
  • 628
  • 3
  • 10

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

unsigned long start, end;
int state = 0;
...
//edit adding debounce
#define BOUNCE 10
int dbounce(unsigned long time, len)
{
    return (millis() - time) > len;
}
...
void loop()
{
  pin = digitalRead(soundPin);
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if dbounce(start,BOUNCE) 
          break; // edit: one way to debounce
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if((end - start) > 1000)
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if(pin == LOW) && dbounce(start,BOUNCE) // another way to debounce
         state = 0; // start over  
      break;
  }
}

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

unsigned long start, end;
int state = 0;
...
void loop()
{
  pin = digitalRead(soundPin);
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if((end - start) > 1000)
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if(pin == LOW)
         state = 0; // start over  
      break;
  }
}

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

unsigned long start, end;
int state = 0;
...
//edit adding debounce
#define BOUNCE 10
int dbounce(unsigned long time, len)
{
    return (millis() - time) > len;
}
...
void loop()
{
  pin = digitalRead(soundPin);
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if dbounce(start,BOUNCE) 
          break; // edit: one way to debounce
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if((end - start) > 1000)
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if(pin == LOW) && dbounce(start,BOUNCE) // another way to debounce
         state = 0; // start over  
      break;
  }
}
expanded upon answer.
Source Link
esoterik
  • 628
  • 3
  • 10

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

unsigned long start, end;
int state = 0;
...
void loop()
{
  pin = digitalRead(soundPin);
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if((end - start) > 1000)
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if(pin == LOW)
         state = 0; // start over  
      break;
  }
}

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

Your logic is flawed; you don't have any delay between readingState and readingState2 other than a serial.Print() which you shouldn't rely on for a delay.

You need to keep track of state in your loop. You need to capture the time of the first input, see it go low, then record the time of the second input. N.b. time between instructions is blindingly fast even on 'slow' proccessors.

something like:

unsigned long start, end;
int state = 0;
...
void loop()
{
  pin = digitalRead(soundPin);
  switch(state)
  {
  case 0:
      if (pin == HIGH)
      {
        start = millis();
        state = 1;
      }
      break;
  case 1:
      if( pin == LOW)
          state = 2;
      break;
  case 2:
      if(pin == HIGH)
      {
         end = millis();
         state = 3;
      }
      break;
  case 3:
      if((end - start) > 1000)
      {
         // do stuff
      }
      state = 4;
      break;
  case 4:
      if(pin == LOW)
         state = 0; // start over  
      break;
  }
}
Source Link
esoterik
  • 628
  • 3
  • 10
Loading