Yamilett Estrada-Reyes
Published

Sound-Controlled* Speakers

Too lazy to turn on your speakers? Too forgetful to turn them off? Look no further, as this project will automate all of your worries away!

IntermediateFull instructions provided2 hours196
Sound-Controlled* Speakers

Things used in this project

Hardware components

Argon
Particle Argon
×1
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
Optional -- used for testing purposes mainly
×1
Small Sound Sensor
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Wood sheets
×5
Popsicle sticks
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Blynk
Blynk

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Schematics

See brief description under "Hardware Setup"

Code

The Code:

C/C++
#include <LiquidCrystal.h>
#include <blynk.h>

#define servoPin A5
#define soundDigitalPin D8

char auth[] = "insert-blynk-auth"; // replace with your respective auth token

const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 

Servo servo;

int off = 0;
int on = 90;

int onCounter;

boolean soundState;
boolean turnedOn = false;
boolean assignedStartTime = false;

unsigned long startTime; // records the time the speakers were turned on
unsigned long lastEvent; // records the last time checkSound() was called


void setup() {
    Serial.begin(9600);
    
    Blynk.begin(auth);
    
    pinMode(soundDigitalPin, INPUT);
    
    servo.attach(servoPin);
    servo.write(off); 
    
    Blynk.virtualWrite(V0, LOW); // updates the appropriate Blynk buttons
    
    lcd.begin(16, 2); // columns & rows
}

BLYNK_WRITE(V0) { // receives data from the app
    int buttonState = param.asInt();
    
    Serial.print("Button value is: ");
    Serial.println(buttonState);
    
    if(buttonState == 1) {
        turnOn();
    }
    
    else {
        turnOff();
    }
}

void checkSound() { // called every second for 30 seconds
    soundState = digitalRead(soundDigitalPin); // is music being heard? (0 = off, 1 = on)
    
    if(soundState == 1) {
        onCounter++;
    }
    
    // display results on LCD screen; OPTIONAL  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Sound Counter: ");
    lcd.setCursor(0,1);
    lcd.print(onCounter);
    
    Serial.print("Sound State: ");
    Serial.println(soundState); 
    
    lastEvent = millis();
}

void turnOn() { // turn speakers on
    servo.write(on);
    turnedOn = true;
    assignedStartTime = false;
}

void turnOff() { // turn speakers off
    turnedOn = false;
    onCounter = 0;
    servo.write(off);
}

void loop() {
    Blynk.run();
    
    // the following statements are only executed if the speakers are ON:
    if(turnedOn == true && assignedStartTime == false) { // if speakers are on, but a start time hasn't been assigned, assign one
        startTime = millis(); // record @ what time the toggle was turned on; called only ONCE
        assignedStartTime = true;
    }
    
    if(turnedOn == true && ((millis() - startTime) < 30000)) { // if the speakers are turned on and 30 seconds have not yet passed since the startTime, checkSound() every 0.5s
        if(((millis() - lastEvent) >= 500)) { 
            checkSound();
        }
    }
    
    if(turnedOn == true && ((millis() - startTime) > 30000)) {
        if(onCounter <= 1) { 
            Serial.println("Idle for too long; turning off");
            Blynk.virtualWrite(V0, LOW); // updates the appropiate Blink buttons to OFF
            turnOff();
        }
        else {
            Serial.println("Resetting the loop");
            onCounter = 0; // reset the counter & prepare for another iteration
            startTime = millis(); // reset the timer; assuming its still on; overwritten if false
        }
    }
}

Credits

Yamilett Estrada-Reyes

Yamilett Estrada-Reyes

1 project • 2 followers

Comments

Add projectSign up / Login