// The conundrum of the Barking Dog enigma.
// ----------------------------------------
// (English is not my native language, I had to look up "conundrum" and "enigma")
//
// A analog input does not need a pinMode.
// For millis see the Blink Without Delay
//   https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
// An analog version of the State Change Detection is used.
//   https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
//   

const int ledPin = 7;
const int analogPin = A0;
int barkCount = 0;            // count the barks
int lastBarkValue;

void setup() 
{
  Serial.begin(9600);
	pinMode( ledPin, OUTPUT);
}

void loop()
{
  int barkValue = analogRead( analogPin);

  if( lastBarkValue < 500 && barkValue >= 500)
  {
    // The previous level was below 500, and it just got above or equal to 500
    barkCount++;
    Serial.print( "Bark !  (");
    Serial.print( barkCount);
    Serial.println( ")");
  }
  lastBarkValue = barkValue;
    	
  delay( 100);        // slow down the sketch to reduce serial output messages
}