Curriculum
Course: Sound Sensor Clap LED Project
Login
Text lesson

Arduino IDE Coding

Arduino IDE Coding

const int soundSensorPin = A0; // Analog pin connected to the sound sensor
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // Digital pins connected to the LEDs

void setup() {
  pinMode(soundSensorPin, INPUT);
  for (int i = 0; i < 10; i++) {
    pinMode(ledPins[i], OUTPUT); // Set the LED pins as outputs
  }
  Serial.begin(9600); // Initialize serial communication for debugging
}

void loop() {
  int soundValue = analogRead(soundSensorPin); // Read the analog input value
 
  for (int i = 0; i < 10; i++) {
    int lowerBound = 70 + (i * 100);
    int upperBound = lowerBound + 100;
   
    if (soundValue >= lowerBound && soundValue < upperBound) {
      for (int j = 0; j <= i; j++) {
        digitalWrite(ledPins[j], HIGH); // Turn on the LEDs up to the current index
      }
      for (int k = i + 1; k < 10; k++) {
        digitalWrite(ledPins[k], LOW); // Turn off the remaining LEDs
      }
    }
  }
 
}

Kids Friendly Coding For this Project

int soundSensor = A0;

int soundValue;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
  soundValue = analogRead(soundSensor);

  Serial.println(soundValue);

  // LED 1
  if(soundValue > 100)
  {
    digitalWrite(2, HIGH);
  }
  else
  {
    digitalWrite(2, LOW);
  }

  // LED 2
  if(soundValue > 200)
  {
    digitalWrite(3, HIGH);
  }
  else
  {
    digitalWrite(3, LOW);
  }

  // LED 3
  if(soundValue > 300)
  {
    digitalWrite(4, HIGH);
  }
  else
  {
    digitalWrite(4, LOW);
  }

  // LED 4
  if(soundValue > 400)
  {
    digitalWrite(5, HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }

  // LED 5
  if(soundValue > 500)
  {
    digitalWrite(6, HIGH);
  }
  else
  {
    digitalWrite(6, LOW);
  }

  // LED 6
  if(soundValue > 600)
  {
    digitalWrite(7, HIGH);
  }
  else
  {
    digitalWrite(7, LOW);
  }

  // LED 7
  if(soundValue > 700)
  {
    digitalWrite(8, HIGH);
  }
  else
  {
    digitalWrite(8, LOW);
  }

  // LED 8
  if(soundValue > 800)
  {
    digitalWrite(9, HIGH);
  }
  else
  {
    digitalWrite(9, LOW);
  }

  // LED 9
  if(soundValue > 900)
  {
    digitalWrite(10, HIGH);
  }
  else
  {
    digitalWrite(10, LOW);
  }

  // LED 10
  if(soundValue > 1000)
  {
    digitalWrite(11, HIGH);
  }
  else
  {
    digitalWrite(11, LOW);
  }

  delay(50);
}

×
×

Cart