Child pages
  • 10. Mikrofon

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Vi har ikke testa analog bruk av mikrofonen, selv om det er mulig det vil virke. Her viser vi eksempel på bruk som digital (av/på) måler.Finnes også på GitHub.

Koden finnes i biblioteket vårt, PLabExamples/examples/07.OtherSensors/Microphone.

Dette eksempelet viser hvordan du kan slå av og på et lys ved bruk av mikrofonen.

...

Breadboard oppkobling og skjematisk oppkobling.

Kode

Code Block
languagecpp
titleMicrophone On/Off Example
linenumberstrue
const int micIn = 2;    // Digital pin the microphone is connected to
const int ledOut = 3;   // Digital pin the led is connected to

int previousSensorReading = 0;  // Remember the previous reading of our microphone
int state = LOW;                // Current led state (HIGH or LOW)

void setup() {
  // Start serial communication with console
  Serial.begin(9600);
  // Initialize inputs and outputs
  pinMode(micIn, INPUT);
  pinMode(ledOut, OUTPUT);
}

void loop() {
  // Read the current microphone level
  int sensorRead = digitalRead(micIn);
  // See if reading has changed since last time
  if (previousSensorReading != sensorRead) {
    // Remember to update previous reading
    previousSensorReading = sensorRead;
    Serial.print("Changed, now: ");
    
    // When senosor is in high state, only notify user.
    if (sensorRead == HIGH) {
      Serial.println("High");
    } else {
      // When sensor changes to low state, we change the state of our output light.
      // This prevents the light from just giva a short flash, but instead remain on or off.
      Serial.println("Low");
      if (state == HIGH) {
        state = LOW;
      } else {
        state = HIGH;
      }
    }
  }
  // Update output light value
  digitalWrite(ledOut, state);
  // Short delay to ensure sensor stability
  delay(1);
}

GitHub: Examples/MicrophoneLightOnOffExample/MicophoneLightOnOffExample.ino