hmkim
Published © GPL3+

PHPoC Sound-Sensoring LED Light Control

Control an LED light using a sound detecting sensor with PHPoC Blue.

BeginnerShowcase (no instructions)5 hours969
PHPoC Sound-Sensoring LED Light Control

Things used in this project

Hardware components

PHPoC Blue
PHPoC Blue
×1
Relay (generic)
×1
12V LED Light
×1
SparkFun Sound Detector (SEN-12642)
×1

Story

Read more

Schematics

Connecting to PHPoC Blue

[Sound detecting sensor] ---- [PHPoC Blue]
ENVELOPE ---- AD0
GATE ---- NSS/0
GND ---- GND
VCC ---- 3.3V

[Relay] ---- [PHPoC Blue]
IN ---- SCK/1
GND ---- GND
5V ---- PWR5

Code

2. Clap once for On, Clap twice for Off

PHP
It recognizes the number of noises within 500ms.
<?php
include_once "/lib/sd_340.php";
adc_setup(0, 0);
uio_setup(0, 0, "in");
uio_setup(0, 1, "out");
uio_out(0, 1, LOW); 
st_free_setup(0);
 
define("FINAL_DETECTED",    0);
define("CLAP_NOT_DETECTED",   1);
define("TIMEOUT",   2);
 
$start_time = 0;
 
function indicate_clap($clap_num)
{
    if($clap_num == 2) //2 clap
        uio_out(0, 1, LOW); 
 
    else if($clap_num == 1) //1 clap
        uio_out(0, 1, HIGH);    
    
    usleep(100000);
}
 
function detect_claps($clap_num)
{    
    global $start_time;
    
    if($clap_num == 2)  // 2 clap -> off
    {
        indicate_clap($clap_num);
 
        return FINAL_DETECTED;
    }
    
    $uio_in = uio_in(0, 0);    
    $adc_in = adc_in(0, 100);
    
    indicate_clap($clap_num); //no clap
        
    if($adc_in > 600 && $uio_in == 1) //clap detect
    {
        $start_time = st_free_get_count(0);
        $current_time = st_free_get_count(0);
    
        while(1)
        {
            if($current_time - $start_time < 500)
            {    
                $current_time = st_free_get_count(0);
                $clap_state = detect_claps($clap_num + 1);   
 
                if($clap_state == FINAL_DETECTED)
                    return $clap_state;
            }
            else 
                return TIMEOUT;            
        }    
    }
    
    return CLAP_NOT_DETECTED;
}
 
while(1)
{            
    $clap_state = detect_claps(0);
    
      if($clap_state == FINAL_DETECTED)
        indicate_clap(0);
 
    usleep(10000);          
}
?>

1. On/Off Toggle

PHP
<?php
include_once "/lib/sd_340.php";
adc_setup(0, 0);
uio_setup(0, 0, "in");
uio_setup(0, 1, "out");
uio_out(0, 1, LOW);
function control_led()
{
    uio_out(0, 1, TOGGLE);    
    usleep(100000);
}
while(1)
{        
    $uio_in = uio_in(0, 0);    //GATE 
    $adc_in = adc_in(0, 100); //ENVELOPE 
    
    if($adc_in > 600 && $uio_in == 1)
        control_led();
        
    usleep(10000);  
}
?>

Credits

hmkim

hmkim

1 project • 24 followers

Comments