My Raspberry Pi Networked Media/NAS Server Setup

I have come to a very good place with my media server setup using my Raspberry Pi. The whole thing is accessible using the network, over a wide range of devices which is ideal for me and the other people living in my house.

If you don’t need to see any of the installation, the following software is running on the server: SambaMinidlna, Deluge & Deluge-Web and NTFS-3G.

The combination of all of this software allows me to access my media and files on pretty much any device I would want to. This is a great combination of software to run on your Pi if you’re not doing anything with it.

So let’s begin with the install!


I’m using the latest build of Raspian, the download and install of that is pretty simple, instructions here.

Unless you can hold your media on the SD card your Pi’s OS is installed on, you’ll need some kind of external storage. In my case, I’m using a 3TB external HDD.

We’ll need to mount this drive, I’ve already written a post on how to do this, check that out here.


Now we should involve Samba. Again, it’s a pretty simple install.

sudo apt-get install samba samba-common-bin

Once it installs you should already see signs of it working. If you’re on windows, make sure network sharing is on, and browse to the “network” folder. It should show up as “RASPBERRYPI” as seen in this image:

The only real tricky part is configuring it. Here is an untouched version of the samba config file. On your pi, it is found at:

/etc/samba/smb.conf

You can edit it like you would any config file. This is the configuration following is the configuration I am running on my Pi, if you want a configuration that will work with no problems without any modifications, replace the existing /etc/samba/smb.conf with this version.

There are only a few differences between the standard version and the version I’m using. The biggest one being the actual “Share” being used seen here:

[HDD_Share]
   comment = External HDD Share
   path = /media/USBHDD
   browseable = yes
   read only = no
   valid users = YOURUSER #IF ON A BASIC RASPBERRY PI, US THE USER "pi"
   only guest = no
   create mask = 0777
   directory mask = 0777
   public = no

Basically, this shares the external HDD you just mounted to the network. You can insert this share anywhere in your document and it will work. Once you update your config file, you have to add your user to samba. If you haven’t done anything but install raspbian, your username on the pi should still be “pi” so the following should do the following:

sudo smbpasswd -a pi

Enter your new samba password twice and then you’re good to go after restarting samba.

sudo /etc/init.d/samba restart

In windows you can go to “network” option in My Computer and see your share.

If you’re like me though, you’re going to want multiple users for multiple shares. Samba only can only have users that are members of the system, so in order to add a new user to samba, you have to add a user to the Raspberry Pi. For example, let’s add the user ‘testuser’:

sudo mkdir /home/testuser/
sudo useradd -d /home/testuser -s /bin/false -r testuser
sudo passwd testuser
sudo chown testuser /home/testuser
sudo chgrp users /home/testuser
sudo smbpasswd -a testuser
sudo /etc/init.d/samba restart

I have written a bash script to do this automatically.

On the share level, the line of valid users = should be set to whichever user you want to be able to use the share.

That’s pretty much it for Samba. I’m probably going to do a guide on accessing your shares via SSH tunneling when the need for me to do so arises. I’ll link that here if it ever happens. Now on to minidlna.


MiniDLNA is a very lightweight DLNA server. DLNA is a protocal specifically for streaming media to a huge array of devices from computers to iOS devices or gaming consoles or smart TV’s. I have spent quite a bit of time using minidlna, and have reached a configuration that works extremely well with the raspberry pi. The install is very easy, much like samba, it’s the configuration that is tricky.

sudo apt-get install minidlna

The config file i’m using is found here. There Pi actually handles the streaming really really well, and there only a few things you need to change in the config file, and they are mostly aesthetic. The following lines are examples of media locations for each type of file.

# If you want to restrict a media_dir to a specific content type, you can
# prepend the directory name with a letter representing the type (A, P or V),
# followed by a comma, as so:
#   * "A" for audio    (eg. media_dir=A,/var/lib/minidlna/music)
#   * "P" for pictures (eg. media_dir=P,/var/lib/minidlna/pictures)
#   * "V" for video    (eg. media_dir=V,/var/lib/minidlna/videos)
#
# WARNING: After changing this option, you need to rebuild the database. Either
#          run minidlna with the '-R' option, or delete the 'files.db' file
#          from the db_dir directory (see below).
#          On Debian, you can run, as root, 'service minidlna force-reload' instead.

media_dir=A,/media/USBHDD/Documents/Media/Audio
media_dir=P,/media/USBHDD/Documents/Media/Images/Photos
media_dir=V,/media/USBHDD/Documents/Media/Video
media_dir=/media/USBHDD/Documents/Media/

And changing this line will change the name of the DLNA server on the network:

# Name that the DLNA server presents to clients.
friendly_name=Raspberry Pi DLNA

That’s pretty much all there is to it.

You can stream the files all over the place, the following images show it being used on my kindle and another computer. I stream files to my xbox 360 all the time.

The last major component of this media server is Deluge, let’s proceed with that install.


Deluge is a torrent client for linux servers. The coolest part is it has a very good web based GUI for control. The install isn’t too straightforward, but there is no real specific configuration. The following commands will get things up and running.

sudo apt-get install python-software-properties
sudo add-apt-repository 'deb http://ppa.launchpad.net/deluge-team/ppa/ubuntu precise main'
sudo apt-get update
sudo apt-get install -t precise deluge-common deluged deluge-web deluge-console
sudo apt-get install -t precise libtorrent-rasterbar6 python-libtorrent
sudo apt-get install screen
deluged
screen -d -m deluge-web

And there you go! You can now torrent files directly into your Samba shares which is hugely useful and more secure, the following is me doing just that:


The last thing that needs to be done is run a few commands at boot, particularly mount the HDD and start deluge-web. The easiest way to do this crontab. First run:

sudo crontab -e

Then add the following two lines:

sudo mount -t ntfs-3g /dev/sda1/ /media/USBHDD/
screen -d -m deluge-web

So it looks like this:

And everything will start working upon boot!


Thank you very much for reading. If you have any questions, please leave a comment.

Smart Speaker | Full Working Prototype

Here is a video of the whole thing working:

The whole system works! If you look at this post, which basically shows the whole thing can be battery powered as well. The following photos show the way the cable is run out the back of the housing. Both sets of the 3 wires are tied together and then to the output of the digital pot:

Contrary to what I thought, the 10k ohm digital pot can change the volume just fine! To be safe though, I ordered an SPI digital Pot that can do 50k ohm resistance.

Here is the new working version of the code as well:

#include <SPI.h> //for using the digital pot

const int slaveSelectPin = 10; //for SPI, from example code

//Shift Register Setup, taken from www.bildr.og
int SER_Pin = 6;   //pin 14 on the 75HC595
int RCLK_Pin = 7;  //pin 12 on the 75HC595
int SRCLK_Pin = 8; //pin 11 on the 75HC595
#define number_of_74hc595s 4 //How many of the shift registers - change this
#define numOfRegisterPins number_of_74hc595s * 8
boolean registers[numOfRegisterPins];

int IR_rangefinder = 0; //The pin attached to the rangefinder

int detect_led = 2;
int setlevelMode0_led = 3;

int serialDebug_switch = 4; //Throughout the program, this switch enables or disables all serial output


int check_val = 3; //The arbitrary position above the sensor that indicates a "check" - the position that must be held in order to change the volume
int cycle_delay = 30; //a universal delay time for refreshing the check functions

void setup(){
  
  //shift register setup
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
  clearRegisters();
  writeRegisters();

  //spi setup
  pinMode (slaveSelectPin, OUTPUT);
  SPI.begin(); 

  //general LED's
  pinMode(detect_led, OUTPUT);
  pinMode(setlevelMode0_led, OUTPUT);
  
  pinMode(serialDebug_switch, INPUT);
  
  //Serial setup
  Serial.begin(9600);  
}

//integers to remember values off of the IR sensor. 
int prelevel_0 = 0;
int prelevel_1 = 0;
int prelevel_2 = 0;
int prelevel_3 = 0;
int prelevel_4 = 0;
int prelevel_5 = 0;
int prelevel_6 = 0;
int prelevel_7 = 0;
int prelevel_8 = 0;
int prelevel_9 = 0;

int pre_positions[10] = {prelevel_0, prelevel_1, prelevel_2, prelevel_3, prelevel_4, prelevel_5, prelevel_6, prelevel_7, prelevel_8, prelevel_9}; // an array holding the positions

void loop(){
  
  /*
  
  The system works by sampling the sensor a number of times. It puts these values into an array. 
  Once all sample have been made, each value is compared to a like value. If every && evaluates to true, this means whatever object above the sensor has been there for the "cycle_delay" * the number of comparisions made.
  It will confirm that the user wants their hand the be there and it was not acciential. 
  
  Think of the following loop as the ambient mode, the user can't adjust the volume from here, but they can enter the mode where they can adjust the volume.
  It has much less precision by design. 
  
  */
  
  for(int i = 0; i <= 9; i = i + 1){
    writebargraph(0,map(analogRead(IR_rangefinder),20,600,0,9));
    pre_positions[i] = map(analogRead(IR_rangefinder),20,600,0,9);
    if(pre_positions[i] == check_val){
      if(digitalRead(serialDebug_switch) == HIGH){
        Serial.println("Check Detected");
      }
      digitalWrite(detect_led, HIGH);
    }
    else {
     digitalWrite(detect_led, LOW);  
    }
    delay(cycle_delay);
  } 
  
  if(digitalRead(serialDebug_switch) == HIGH){
    for(int i = 0; i <= 9; i = i + 1){
      Serial.print(pre_positions[i]);
      Serial.print(",");
    }
  }
  
  //Once it has been determined that the object above the sensor has been there for a long enough time, the system enters the secondary level set mode. 
  if (pre_positions[0] == check_val && pre_positions[1] == check_val && pre_positions[2] == check_val && pre_positions[3] == check_val && pre_positions[4] == check_val && pre_positions[5] == check_val && pre_positions[6] == check_val && pre_positions[7] == check_val && pre_positions[8] == check_val && pre_positions[9] == check_val  ){
    if(digitalRead(serialDebug_switch) == HIGH){
      Serial.print(" - Pre Level Set");
      Serial.println("");
    }
    delay(500);
    setlevel(); 
    delay(500);
  }
  else {
    if(digitalRead(serialDebug_switch) == HIGH){
      Serial.println(" - No Set");
    }
  }

}


void setlevel(){
  
  /*
  
  Very similar to the above topology. This version is much more precise, and has 30 comparison samples as opposed to 10. 
  It also writes to the digital potentiometer as well at the the same time as the bar graph. 
  
  */
  
  int level0 = 0;
  int level1 = 0;
  int level2 = 0;
  int level3 = 0;
  int level4 = 0;
  int level5 = 0;
  int level6 = 0;
  int level7 = 0;
  int level8 = 0;
  int level9 = 0;
  int level10 = 0;
  int level11 = 0;
  int level12 = 0;
  int level13 = 0;
  int level14 = 0;
  int level15 = 0;
  int level16 = 0;
  int level17 = 0;
  int level18 = 0;
  int level19 = 0;
  int level20 = 0;
  int level21 = 0;
  int level22 = 0;
  int level23 = 0;
  int level24 = 0;
  int level25 = 0;
  int level26 = 0;
  int level27 = 0;
  int level28 = 0;
  int level29 = 0;
  
  int positions[30] = { level0, level1, level2, level3, level4, level5, level6, level7, level8, level9, level10, level11, level12, level13, level14, level15, level16, level17, level18, level19, level20, level21, level22, level23, level24, level25, level26, level27, level28, level29};
  
  digitalWrite(setlevelMode0_led, LOW);
    
  boolean seeking = true;
  
  while(seeking == true){
    for(int i = 0; i <= 29; i = i + 1){
      writebargraph(1,map(analogRead(IR_rangefinder),20,600,0,19));
      digitalpot(map(analogRead(IR_rangefinder),20,600,0,255)); //Writes to digital pot
      positions[i] = map(analogRead(IR_rangefinder),20,600,0,19);
      if(digitalRead(serialDebug_switch) == HIGH){
        Serial.print(positions[i]);
        Serial.print(",");
      }
      delay(cycle_delay);
    }  
  
  
  //Instead of comparing to a predetermined value, it compares it to the first value sampled. If this if statement is true, it means the users hand has stopped moving, indicating they would like to set the volume at that position.
  if (positions[0] == positions[0] && positions[1] == positions[0] && positions[2] == positions[0] && positions[3] == positions[0] && positions[4] == positions[0] && positions[5] == positions[0] && positions[6] == positions[0] && positions[7] == positions[0] && positions[8] == positions[0] && positions[9] == positions[0] && positions[10] == positions[0] && positions[11] == positions[0] && positions[12] == positions[0] && positions[13] == positions[0] && positions[14] == positions[0] && positions[15] == positions[0]  ){
    if(digitalRead(serialDebug_switch) == HIGH){
     Serial.print(" - Level Set");
    }
    digitalWrite(setlevelMode0_led, HIGH);
    seeking = false; //Stops the loop and holds the last value on the bar graph and digital pot. 
   }
   else {
     if(digitalRead(serialDebug_switch) == HIGH){
       Serial.print(" - No Set");
     }
     digitalWrite(setlevelMode0_led, LOW);
   }
   if(digitalRead(serialDebug_switch) == HIGH){
     Serial.println("");
   }
  }
}

//This function will write to the shift registers -> the bar graph. It will write all of the values below the one specified HIGH and all above LOW. It also allows multiple sets of bar graphs
void writebargraph(int set, int led){
  if(set == 0){
    for(int i = 0; i <= 9; i = i + 1){
      if(i <= led){
                
        setRegisterPin(i, HIGH);
        writeRegisters();
      }
      else if(i > led){
        
        setRegisterPin(i, LOW);
        writeRegisters();
      } 
     }
   }
  if(set == 1){
    for(int k = 10; k <= 29; k = k + 1){
      if(k <= 10 + led){
        setRegisterPin(k, HIGH);
        writeRegisters();
      }
      else if(k > 10 + led){
        setRegisterPin(k, LOW);
        writeRegisters();
      } 
    }
  }
}

//A very simple function to write values to the Digital Pot
void digitalpot(int value){
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(0); // enables the chip
  SPI.transfer(value);
  digitalWrite(slaveSelectPin,HIGH);
}


//SHIFT REGISTER FUNCTIONS. 
//set all register pins to LOW
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
} 
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
  digitalWrite(RCLK_Pin, LOW);
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
    digitalWrite(SRCLK_Pin, LOW);
    int val = registers[i];
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
  }
  digitalWrite(RCLK_Pin, HIGH);
}
//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

The only difference between this one and the last version I posted was the height of the check value. I made it further away from the sensor.

Before the code is “finished” I would like to add a few things. The first being an averaging loop in the raw input ; instead of just using variations of map(analogRead(IR_rangefinder),20,600,0,9); each time, I’d like to maybe write my own function that is more general for assigning comparison. The downside to this however is that it may slow things down and the top priority with this project is keeping it fast and accurate.

Smart Speaker | Amp Teardown and Potentiometer Problem

Here are images of the teardown:

A couple of things to note: I over heated the pads when removing the gain pot and lifted a trace by accident. Instead of soldering directly into the lug hole, I just tied directly to the resistor that is in series with the pot signal.

I also am also not going to be able to use the digital pot I have been working with so far. Note the image that shows the pot with a resistance of 50k, and the one I’ve been working with is 10k.

I’m going to order a pair of these, and many of these to try and solve the bluetooth static problem.

Smart Speaker | Proximity Potentiometer Working Prototype

A direct follow up to this post: https://esologic.com/?p=984


First, here’s a video:

I got the digital potentiometer working! It was very simple using the SPI library built into the Arduino software. All you have to do is address the chip and then write it a value between 0 and 255 to set the resistance value. Here is the code for this version, I’ve cleaned it up a bit since last time:


#include <SPI.h> //for using the digital pot

const int slaveSelectPin = 10; //for SPI, from example code

//Shift Register Setup, taken from www.bildr.og
int SER_Pin = 7;   //pin 14 on the 75HC595
int RCLK_Pin = 8;  //pin 12 on the 75HC595
int SRCLK_Pin = 9; //pin 11 on the 75HC595
#define number_of_74hc595s 4 //How many of the shift registers - change this
#define numOfRegisterPins number_of_74hc595s * 8
boolean registers[numOfRegisterPins];

int IR_rangefinder = 0; //The pin attached to the rangefinder

int detect_led = 2;
int setlevelMode0_led = 3;
int setlevelMode1_led = 4;
int seeking_led = 6;

int check_val = 8; //The arbitrary position above the sensor that indicates a "check" - the position that must be held in order to change the volume
int cycle_delay = 30; //a universal delay time for refreshing the check functions

void setup(){
  
  //shift register setup
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
  clearRegisters();
  writeRegisters();

  //spi setup
  pinMode (slaveSelectPin, OUTPUT);
  SPI.begin(); 

  //general LED's
  pinMode(detect_led, OUTPUT);
  pinMode(setlevelMode0_led, OUTPUT);
  pinMode(setlevelMode1_led, OUTPUT);
  pinMode(seeking_led, OUTPUT);
  
  //Serial setup
  Serial.begin(9600);  
}

//integers to remember values off of the IR sensor. 
int prelevel_0 = 0;
int prelevel_1 = 0;
int prelevel_2 = 0;
int prelevel_3 = 0;
int prelevel_4 = 0;
int prelevel_5 = 0;
int prelevel_6 = 0;
int prelevel_7 = 0;
int prelevel_8 = 0;
int prelevel_9 = 0;

int pre_positions[10] = {prelevel_0, prelevel_1, prelevel_2, prelevel_3, prelevel_4, prelevel_5, prelevel_6, prelevel_7, prelevel_8, prelevel_9}; // an array holding the positions

void loop(){
  
  /*
  
  The system works by sampling the sensor a number of times. It puts these values into an array. 
  Once all sample have been made, each value is compared to a like value. If every && evaluates to true, this means whatever object above the sensor has been there for the "cycle_delay" * the number of comparisions made.
  It will confirm that the user wants their hand the be there and it was not acciential. 
  
  Think of the following loop as the ambient mode, the user can't adjust the volume from here, but they can enter the mode where they can adjust the volume.
  It has much less precision by design. 
  
  */
  
  for(int i = 0; i <= 9; i = i + 1){
    writebargraph(0,map(analogRead(IR_rangefinder),20,600,0,9));
    pre_positions[i] = map(analogRead(IR_rangefinder),20,600,0,9);
    if(pre_positions[i] == check_val){
      Serial.println("Check Detected");
      digitalWrite(detect_led, HIGH);
    }
    else {
     digitalWrite(detect_led, LOW);  
    }
    delay(cycle_delay);
  } 
  for(int i = 0; i <= 9; i = i + 1){
    Serial.print(pre_positions[i]);
    Serial.print(",");
  } 
  
  //Once it has been determined that the object above the sensor has been there for a long enough time, the system enters the secondary level set mode. 
  if (pre_positions[0] == check_val && pre_positions[1] == check_val && pre_positions[2] == check_val && pre_positions[3] == check_val && pre_positions[4] == check_val && pre_positions[5] == check_val && pre_positions[6] == check_val && pre_positions[7] == check_val && pre_positions[8] == check_val && pre_positions[9] == check_val  ){
    Serial.print(" - Pre Level Set");
    Serial.println("");
    delay(500);
    setlevel(); 
    delay(500);
  }
  else {
    Serial.println(" - No Set");
  }

}


void setlevel(){
  
  /*
  
  Very similar to the above topology. This version is much more precise, and has 30 comparison samples as opposed to 10. 
  It also writes to the digital potentiometer as well at the the same time as the bar graph. 
  
  */
  
  int level0 = 0;
  int level1 = 0;
  int level2 = 0;
  int level3 = 0;
  int level4 = 0;
  int level5 = 0;
  int level6 = 0;
  int level7 = 0;
  int level8 = 0;
  int level9 = 0;
  int level10 = 0;
  int level11 = 0;
  int level12 = 0;
  int level13 = 0;
  int level14 = 0;
  int level15 = 0;
  int level16 = 0;
  int level17 = 0;
  int level18 = 0;
  int level19 = 0;
  int level20 = 0;
  int level21 = 0;
  int level22 = 0;
  int level23 = 0;
  int level24 = 0;
  int level25 = 0;
  int level26 = 0;
  int level27 = 0;
  int level28 = 0;
  int level29 = 0;
  
  int positions[30] = { level0, level1, level2, level3, level4, level5, level6, level7, level8, level9, level10, level11, level12, level13, level14, level15, level16, level17, level18, level19, level20, level21, level22, level23, level24, level25, level26, level27, level28, level29};
  
  digitalWrite(setlevelMode1_led, LOW);
  digitalWrite(setlevelMode2_led, LOW);  
  
  boolean seeking = true;
  
  while(seeking == true){
    for(int i = 0; i <= 29; i = i + 1){
      writebargraph(1,map(analogRead(IR_rangefinder),20,600,0,19));
      digitalpot(map(analogRead(IR_rangefinder),20,600,0,255)); //Writes to digital pot
      positions[i] = map(analogRead(IR_rangefinder),20,600,0,19);
      Serial.print(positions[i]);
      Serial.print(",");
      delay(cycle_delay);
    }  
  
  
  //Instead of comparing to a predetermined value, it compares it to the first value sampled. If this if statement is true, it means the users hand has stopped moving, indicating they would like to set the volume at that position.
  if (positions[0] == positions[0] && positions[1] == positions[0] && positions[2] == positions[0] && positions[3] == positions[0] && positions[4] == positions[0] && positions[5] == positions[0] && positions[6] == positions[0] && positions[7] == positions[0] && positions[8] == positions[0] && positions[9] == positions[0] && positions[10] == positions[0] && positions[11] == positions[0] && positions[12] == positions[0] && positions[13] == positions[0] && positions[14] == positions[0] && positions[15] == positions[0]  ){
     Serial.print(" - Level Set");
     digitalWrite(setlevelMode1_led, HIGH);
     seeking = false; //Stops the loop and holds the last value on the bar graph and digital pot. 
   }
   else {
     Serial.print(" - No Set");
     digitalWrite(setlevelMode1_led, LOW);
   }
   Serial.println("");
  }
}

//This function will write to the shift registers -> the bar graph. It will write all of the values below the one specified HIGH and all above LOW. It also allows multiple sets of bar graphs
void writebargraph(int set, int led){
  if(set == 0){
    for(int i = 0; i <= 9; i = i + 1){
      if(i <= led){
                
        setRegisterPin(i, HIGH);
        writeRegisters();
      }
      else if(i > led){
        
        setRegisterPin(i, LOW);
        writeRegisters();
      } 
     }
   }
  if(set == 1){
    for(int k = 10; k <= 29; k = k + 1){
      if(k <= 10 + led){
        setRegisterPin(k, HIGH);
        writeRegisters();
      }
      else if(k > 10 + led){
        setRegisterPin(k, LOW);
        writeRegisters();
      } 
    }
  }
}

//A very simple function to write values to the Digital Pot
void digitalpot(int value){
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(0); // enables the chip
  SPI.transfer(value);
  digitalWrite(slaveSelectPin,HIGH);
}


//SHIFT REGISTER FUNCTIONS. 
//set all register pins to LOW
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
} 
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
  digitalWrite(RCLK_Pin, LOW);
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
    digitalWrite(SRCLK_Pin, LOW);
    int val = registers[i];
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
  }
  digitalWrite(RCLK_Pin, HIGH);
}
//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

If you have questions about the code please leave it in the comments.

Smart Speaker | Proximity Potentiometer Proof of Concept

New project! First, here’s a video:

This is the proof of concept for the volume control of the speaker system. In it’s final form, this sensor will be exposed to the outside and will allow users to control the volume without opening the system, preserving the fidelity of the inside. For example, imagine a user is at the beach and wants to change the volume but they have sand covered hands or wet hands. This system will solve that problem.

The sensor is the Sharp GP2Y0A41SK0F. Here are some very very macro shots of sensors inner workings.

But now for what you came here for, the code. It’s very poorly commented as this is just a prototype, but it’s better than nothing. As this project progresses I’ll posted updated versions of this code.

This demo also relies heavily this shift register. I still haven’t decided if i’m going to use a buzzer to interact with the user or if I’m going to use these bar graphs.

int SER_Pin = 10;   //pin 14 on the 75HC595
int RCLK_Pin = 11;  //pin 12 on the 75HC595
int SRCLK_Pin = 12; //pin 11 on the 75HC595

//How many of the shift registers - change this
#define number_of_74hc595s 4 

//do not touch
#define numOfRegisterPins number_of_74hc595s * 8

boolean registers[numOfRegisterPins];

int detect_led = 2;

int setlevelMode0_led = 3;
int setlevelMode1_led = 4;
int setlevelMode2_led = 5;

int seeking_led = 6;

int check_val = 8;

void setup(){
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
  //reset all register pins
  clearRegisters();
  writeRegisters();

  pinMode(detect_led, OUTPUT);

  pinMode(setlevelMode0_led, OUTPUT);
  pinMode(setlevelMode1_led, OUTPUT);
  pinMode(setlevelMode2_led, OUTPUT);

  pinMode(seeking_led, OUTPUT);

  Serial.begin(9600);
}

int prelevel_0 = 0;
int prelevel_1 = 0;
int prelevel_2 = 0;
int prelevel_3 = 0;
int prelevel_4 = 0;
int prelevel_5 = 0;
int prelevel_6 = 0;
int prelevel_7 = 0;
int prelevel_8 = 0;
int prelevel_9 = 0;

int pre_positions[10] = {prelevel_0, prelevel_1, prelevel_2, prelevel_3, prelevel_4, prelevel_5, prelevel_6, prelevel_7, prelevel_8, prelevel_9};

void loop(){
  for(int i = 0; i <= 9; i = i + 1){
    writebargraph(0,map(analogRead(0),20,600,0,9));
    pre_positions[i] = map(analogRead(0),20,600,0,9);
    if(pre_positions[i] == check_val){
      Serial.println("Check Detected");
      digitalWrite(detect_led, HIGH);
    }
    else {
     digitalWrite(detect_led, LOW);
    }
    delay(30);
  }
  for(int i = 0; i <= 9; i = i + 1){
    Serial.print(pre_positions[i]);
    Serial.print(",");
  } 

  if (pre_positions[0] == check_val && pre_positions[1] == check_val && pre_positions[2] == check_val && pre_positions[3] == check_val && pre_positions[4] == check_val && pre_positions[5] == check_val && pre_positions[6] == check_val && pre_positions[7] == check_val && pre_positions[8] == check_val && pre_positions[9] == check_val  ){
    Serial.print(" - Pre Level Set");
    Serial.println("");
    delay(500);
    setlevel();
    delay(500);
  }
  else {
    Serial.println(" - No Set");
  }

}

//set all register pins to LOW
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
} 

//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
  digitalWrite(RCLK_Pin, LOW);
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
    digitalWrite(SRCLK_Pin, LOW);
    int val = registers[i];
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
  }
  digitalWrite(RCLK_Pin, HIGH);
}

//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

void writebargraph(int set, int led){
  if(set == 0){
    for(int i = 0; i <= 9; i = i + 1){
      if(i <= led){

        setRegisterPin(i, HIGH);
        writeRegisters();
      }
      else if(i > led){

        setRegisterPin(i, LOW);
        writeRegisters();
      }
     }
   }
  if(set == 1){
    for(int k = 10; k <= 29; k = k + 1){
      if(k <= 10 + led){
        setRegisterPin(k, HIGH);
        writeRegisters();
      }
      else if(k > 10 + led){
        setRegisterPin(k, LOW);
        writeRegisters();
      }
    }
  }

}

void setlevel(){

  int level0 = 0;
  int level1 = 0;
  int level2 = 0;
  int level3 = 0;
  int level4 = 0;
  int level5 = 0;
  int level6 = 0;
  int level7 = 0;
  int level8 = 0;
  int level9 = 0;
  int level10 = 0;
  int level11 = 0;
  int level12 = 0;
  int level13 = 0;
  int level14 = 0;
  int level15 = 0;
  int level16 = 0;
  int level17 = 0;
  int level18 = 0;
  int level19 = 0;
  int level20 = 0;
  int level21 = 0;
  int level22 = 0;
  int level23 = 0;
  int level24 = 0;
  int level25 = 0;
  int level26 = 0;
  int level27 = 0;
  int level28 = 0;
  int level29 = 0;

  int positions[30] = { level0, level1, level2, level3, level4, level5, level6, level7, level8, level9, level10, level11, level12, level13, level14, level15, level16, level17, level18, level19, level20, level21, level22, level23, level24, level25, level26, level27, level28, level29};

  digitalWrite(setlevelMode1_led, LOW);
  digitalWrite(setlevelMode2_led, LOW);  

  boolean seeking = true;

  while(seeking == true){
    for(int i = 0; i <= 29; i = i + 1){
      writebargraph(1,map(analogRead(0),20,600,0,19));
      positions[i] = map(analogRead(0),20,600,0,19);
      Serial.print(positions[i]);
      Serial.print(",");
      delay(10);
    }  

  if (positions[0] == positions[0] && positions[1] == positions[0] && positions[2] == positions[0] && positions[3] == positions[0] && positions[4] == positions[0] && positions[5] == positions[0] && positions[6] == positions[0] && positions[7] == positions[0] && positions[8] == positions[0] && positions[9] == positions[0] && positions[10] == positions[0] && positions[11] == positions[0] && positions[12] == positions[0] && positions[13] == positions[0] && positions[14] == positions[0] && positions[15] == positions[0]  ){
     Serial.print(" - Level Set");
     digitalWrite(setlevelMode1_led, HIGH);
     seeking = false;
   }

   else {
     Serial.print(" - No Set");
     digitalWrite(setlevelMode1_led, LOW);
   }

   Serial.println("");

  }

}

Here are some photos of the board if you want to try and work out the schematic:

PiPlanter 2 | Little Plants 1 / Germination Setup

The plants are coming along quite nicely, here is an album of images:

As for my grow setup in this stage, it’s pretty simple. Basically I keep the two desk lamps I have from that area on 24/7 and on the plants. Every morning I put about a half gallon into each of the trays. I also spray about 8oz onto the surface of the plants. Working pretty well so far, all of this growth is only after a week and two days.