Advertisement
ccarman602

Lesson-6-Sound

Nov 25th, 2021 (edited)
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Sound Control Light
  2. int soundPin = A2; // Analog sound sensor is to be attached to analog
  3. int ledPin = 4; // Digital LED is to be attached to digital
  4. void setup() {
  5.   pinMode(ledPin, OUTPUT);
  6.   pinMode(soundPin, INPUT);
  7.   Serial.begin(9600);
  8. }
  9. void loop(){
  10.   int soundState = analogRead(soundPin); // Read sound sensor’s value
  11.   Serial.println(soundState);
  12.   // if the sound sensor’s value is greater than 400, LED light up
  13.   //Otherwise, the light will be turned off
  14.   if (soundState > 262) {
  15.     digitalWrite(ledPin, HIGH);
  16.     delay(100);
  17.   }else{
  18.     digitalWrite(ledPin, LOW);
  19.   }
  20. }
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement