Wednesday, December 8, 2010

Final Project

I've finally finished my project. The end result was a a little less than I had originally anticipated but I achieved my main goal of getting my box operational. I had originally planed on having four buttons but due to a lot of complications with code and hardware I ended up only having one. Everything else works as originally designed, the box makes noise when tilted in a direction and the button also makes noise when pressed. The only problem is that the speakers I used aren't very powerful so they aren't very loud.

To make the housing for my components I used blue form insulation that I got from Lowes, black spray paint that I got from Hobby Lobby and Great Stuff spray foam insulation also from Lowes. I cut uniform  squares that I stacked on top of each other and "glued" together with the Great Stuff. I then smoothed out the edges with a knife and cut out the inside of the box. After that I "glued" side panels on to the sides to give it a more box like appearance and spray painted it black. After the paint dried I assembled the components for my box and used velcro to have a removable top. 

I have to give credit to user dany32412 for supplying his code so that I could use it for my project and Thomas Asmuth for helping me trouble shoot my projects problems and helping me figure out my code.








This is the code I used to make my box work. For the complete original code that this is based off of you can go to user dany32412 's profile oninstructibles.com or go to


#include <Wire.h>
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx,accy,accz,joyx,joyy,zbut,cbut;
int ledPin = 13;
int Lledpin = 2; // left led
int Rledpin = 3; // right led
int Uledpin = 4; // up led
int Dledpin = 5; // down led

const int buttonPin = 7; // the number of the pushbutton pin
const int btnPin = 2; // the number of the LED pin
const int extraPin = 8;


// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;


void setup()
{
Serial.begin(19200);
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake

Serial.print("WiiChuckDemo ready\n");

pinMode(btnPin, OUTPUT); 
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(extraPin, OUTPUT);
}

void loop()
{
buttonState2 = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) { 
// turn LED on: 
digitalWrite(btnPin, HIGH); 
} 
else {
// turn LED off:
digitalWrite(btnPin, LOW); 
}

if(buttonState2 == HIGH) {
digitalWrite(extraPin, HIGH);
} 
else {
digitalWrite(extraPin, LOW);
}

if( loop_cnt > 10 ) { // every 100 msecs get new data
loop_cnt = 0;

nunchuck_get_data();

accx = nunchuck_accelx();
accy = nunchuck_accely();
accz = nunchuck_accelz();
joyx = nunchuck_joyx();
joyy = nunchuck_joyy(); 
zbut = nunchuck_zbutton();
cbut = nunchuck_cbutton(); 
digitalRead(zbut);
if(zbut == 1 ){
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
//Left led accelerometer
if(accx <= 90){
digitalWrite(Lledpin, HIGH);
} else {
digitalWrite(Lledpin, LOW);}
//Right led accelerometer 
if(accx >= 180){
digitalWrite(Rledpin, HIGH);
} else {
digitalWrite(Rledpin, LOW);}
//Up led accelerometer
if(accy >= 165){
digitalWrite(Uledpin, HIGH);
} else {
digitalWrite(Uledpin, LOW);}
//Down led accelerometer
if(accy <= 100){
digitalWrite(Dledpin, HIGH);
} else {
digitalWrite(Dledpin, LOW);}
//Right led joystick
if(cbut == 1){
if(joyx >= 210){
digitalWrite(Rledpin, HIGH);
} else {
digitalWrite(Rledpin, LOW);}
//Left led joystick
if(joyx <= 50){
digitalWrite(Lledpin, HIGH);
} else {
digitalWrite(Lledpin, LOW);}
//Down led joystick
if(joyy <= 70){
digitalWrite(Dledpin, HIGH);
} else {
digitalWrite(Dledpin, LOW);}
//Up led joystick
if(joyy >= 210){
digitalWrite(Uledpin, HIGH);
} else {
digitalWrite(Uledpin, LOW);}
}




Serial.print("accx: "); Serial.print((byte)accx,DEC);
Serial.print("\taccy: "); Serial.print((byte)accy,DEC);
Serial.print("\taccz: "); Serial.print((byte)accz,DEC);
Serial.print("\tjoyx: "); Serial.print((byte)joyx,DEC);
Serial.print("\tjoyy: "); Serial.print((byte)joyy,DEC);
}
loop_cnt++;
delay(1);
}

Final Design