Down On The Corner – Arduino

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); 

// LEDs
const int pin1 = 13;      
const int pin2 = 12;
const int pin3 = 11;
const int pin4 = 10;
const int pin5 = 9; 

const int soundSensor = A0; // the piezo is connected to analog pin 0
int sensorReading = 0;      

void setup() {
    pinMode(pin1, OUTPUT); 
    pinMode(pin2, OUTPUT);
    pinMode(pin3, OUTPUT);
    pinMode(pin4, OUTPUT);
    pinMode(pin5, OUTPUT);
    lcd.init();   
    lcd.clear();
    lcd.backlight();

    Serial.begin(9600);
}

void loop() {
    sensorReading = analogRead(soundSensor);
    float v = sensorReading * (1000.0 / 1023.0);
    Serial.print("volume: ");
    Serial.println(v);

    lcd.setCursor(0,1);
    lcd.print("volume:  ");
    lcd.print(v);

    if(v >=100)
      digitalWrite(pin1, HIGH);
    else
      digitalWrite(pin1, LOW);

    if(v >=200)
      digitalWrite(pin2, HIGH);
    else
      digitalWrite(pin2, LOW);

    if(v >=300)
      digitalWrite(pin3, HIGH);
    else 
      digitalWrite(pin3, LOW);

    if(v >=400)
      digitalWrite(pin4, HIGH);
    else
      digitalWrite(pin4, LOW);

    if(v >=500)
      digitalWrite(pin5, HIGH);
    else
      digitalWrite(pin5, LOW);
}

Hardly rocket science, but fun…

Advertisements