Difference between revisions of "Students/Ian Petrov"

From Robert-Depot
Jump to: navigation, search
Line 42: Line 42:
 
Code
 
Code
 
----
 
----
[quote]
+
<pre>
[color=#7E7E7E]//Ian Petrov VIS147A Final[/color]
+
<span style="color: #7E7E7E;">//Ian&nbsp;Petrov&nbsp;VIS147A&nbsp;Final</span>
[color=#7E7E7E]//code edited from Adafruit Wave Shield Site[/color]
+
<span style="color: #7E7E7E;">//code&nbsp;edited&nbsp;from&nbsp;Adafruit&nbsp;Wave&nbsp;Shield&nbsp;Site</span>
#include <FatReader.h>
+
#include&nbsp;<FatReader.h>
#include <SdReader.h>
+
#include&nbsp;<SdReader.h>
#include <avr/pgmspace.h>
+
#include&nbsp;<avr/pgmspace.h>
#include [color=#006699]"WaveUtil.h"[/color]
+
#include&nbsp;<span style="color: #006699;">"WaveUtil.h"</span>
#include [color=#006699]"WaveHC.h"[/color]
+
#include&nbsp;<span style="color: #006699;">"WaveHC.h"</span>
  
  
SdReader card;    [color=#7E7E7E]// This object holds the information for the card[/color]
+
SdReader&nbsp;card;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// This object holds the information for the card</span>
FatVolume vol;    [color=#7E7E7E]// This holds the information for the partition on the card[/color]
+
FatVolume&nbsp;vol;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// This holds the information for the partition on the card</span>
FatReader root;   [color=#7E7E7E]// This holds the information for the filesystem on the card[/color]
+
FatReader&nbsp;root;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// This holds the information for the filesystem on the card</span>
FatReader f;      [color=#7E7E7E]// This holds the information for the file we're play[/color]
+
FatReader&nbsp;f;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// This holds the information for the file we're play</span>
  
WaveHC wave;      [color=#7E7E7E]// This is the only wave (audio) object, since we will only play one at a time[/color]
+
WaveHC&nbsp;wave;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// This is the only wave (audio) object, since we will only play one at a time</span>
  
#define DEBOUNCE 100  [color=#7E7E7E]// button debouncer[/color]
+
#define&nbsp;DEBOUNCE&nbsp;100&nbsp;&nbsp;<span style="color: #7E7E7E;">// button debouncer</span>
  
[color=#7E7E7E]// this handy function will return the number of bytes currently free in RAM, great for debugging!   [/color]
+
<span style="color: #7E7E7E;">//&nbsp;this&nbsp;handy&nbsp;function&nbsp;will&nbsp;return&nbsp;the&nbsp;number&nbsp;of&nbsp;bytes&nbsp;currently&nbsp;free&nbsp;in&nbsp;RAM,&nbsp;great&nbsp;for&nbsp;debugging!&nbsp;&nbsp;&nbsp;</span>
[color=#CC6600]int[/color] freeRam([color=#CC6600]void[/color])
+
<span style="color: #CC6600;">int</span> freeRam(<span style="color: #CC6600;">void</span>)
 
{
 
{
  extern [color=#CC6600]int[/color] __bss_end;  
+
&nbsp;&nbsp;extern&nbsp;<span style="color: #CC6600;">int</span> __bss_end;  
  extern [color=#CC6600]int[/color] *__brkval;  
+
&nbsp;&nbsp;extern&nbsp;<span style="color: #CC6600;">int</span> *__brkval;  
  [color=#CC6600]int[/color] free_memory;  
+
&nbsp;&nbsp;<span style="color: #CC6600;">int</span> free_memory;  
  [color=#CC6600]if[/color](([color=#CC6600]int[/color])__brkval == 0) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span>((<span style="color: #CC6600;">int</span>)__brkval == 0) {
    free_memory = (([color=#CC6600]int[/color])&free_memory) - (([color=#CC6600]int[/color])&__bss_end);  
+
&nbsp;&nbsp;&nbsp;&nbsp;free_memory&nbsp;=&nbsp;((<span style="color: #CC6600;">int</span>)&free_memory) - ((<span style="color: #CC6600;">int</span>)&__bss_end);  
  }
+
&nbsp;&nbsp;}
  [color=#CC6600]else[/color] {
+
&nbsp;&nbsp;<span style="color: #CC6600;">else</span> {
    free_memory = (([color=#CC6600]int[/color])&free_memory) - (([color=#CC6600]int[/color])__brkval);  
+
&nbsp;&nbsp;&nbsp;&nbsp;free_memory&nbsp;=&nbsp;((<span style="color: #CC6600;">int</span>)&free_memory) - ((<span style="color: #CC6600;">int</span>)__brkval);  
  }
+
&nbsp;&nbsp;}
  [color=#CC6600]return[/color] free_memory;  
+
&nbsp;&nbsp;<span style="color: #CC6600;">return</span> free_memory;  
} 
+
}&nbsp;
  
[color=#CC6600]void[/color] sdErrorCheck([color=#CC6600]void[/color])
+
<span style="color: #CC6600;">void</span> sdErrorCheck(<span style="color: #CC6600;">void</span>)
 
{
 
{
  [color=#CC6600]if[/color] (!card.errorCode()) [color=#CC6600]return[/color];
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (!card.errorCode()) <span style="color: #CC6600;">return</span>;
  putstring([color=#006699]"\n\rSD I/O error: "[/color]);
+
&nbsp;&nbsp;putstring(<span style="color: #006699;">"\n\rSD I/O error: "</span>);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](card.errorCode(), [color=#006699]HEX[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(card.errorCode(), <span style="color: #006699;">HEX</span>);
  putstring([color=#006699]", "[/color]);
+
&nbsp;&nbsp;putstring(<span style="color: #006699;">", "</span>);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](card.errorData(), [color=#006699]HEX[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(card.errorData(), <span style="color: #006699;">HEX</span>);
  [color=#CC6600]while[/color](1);
+
&nbsp;&nbsp;<span style="color: #CC6600;">while</span>(1);
 
}
 
}
  
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
+
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>() {
  [color=#7E7E7E]// set up serial port[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// set up serial port</span>
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
+
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
  putstring_nl([color=#006699]"WaveHC with 6 buttons"[/color]);
+
&nbsp;&nbsp;putstring_nl(<span style="color: #006699;">"WaveHC with 6 buttons"</span>);
  
+
&nbsp;&nbsp;
   putstring([color=#006699]"Free RAM: "[/color]);      [color=#7E7E7E]// This can help with debugging, running out of RAM is bad[/color]
+
&nbsp;&nbsp;&nbsp;putstring(<span style="color: #006699;">"Free RAM: "</span>);      <span style="color: #7E7E7E;">// This can help with debugging, running out of RAM is bad</span>
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](freeRam());      [color=#7E7E7E]// if this is under 150 bytes it may spell trouble![/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(freeRam());      <span style="color: #7E7E7E;">// if this is under 150 bytes it may spell trouble!</span>
  
+
&nbsp;&nbsp;
  [color=#7E7E7E]// Set the output pins for the DAC control. This pins are defined in the library[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Set the output pins for the DAC control. This pins are defined in the library</span>
  [color=#CC6600]pinMode[/color](2, [color=#006699]OUTPUT[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(2, <span style="color: #006699;">OUTPUT</span>);
  [color=#CC6600]pinMode[/color](3, [color=#006699]OUTPUT[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(3, <span style="color: #006699;">OUTPUT</span>);
  [color=#CC6600]pinMode[/color](4, [color=#006699]OUTPUT[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(4, <span style="color: #006699;">OUTPUT</span>);
  [color=#CC6600]pinMode[/color](5, [color=#006699]OUTPUT[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(5, <span style="color: #006699;">OUTPUT</span>);
 
+
&nbsp;
  [color=#7E7E7E]// pin13 LED[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// pin13 LED</span>
  [color=#CC6600]pinMode[/color](13, [color=#006699]OUTPUT[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(13, <span style="color: #006699;">OUTPUT</span>);
 
+
&nbsp;
  [color=#7E7E7E]// enable pull-up resistors on switch pins (analog inputs)[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// enable pull-up resistors on switch pins (analog inputs)</span>
  [color=#CC6600]digitalWrite[/color](14, [color=#006699]HIGH[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(14, <span style="color: #006699;">HIGH</span>);
  [color=#CC6600]digitalWrite[/color](15, [color=#006699]HIGH[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(15, <span style="color: #006699;">HIGH</span>);
  [color=#CC6600]digitalWrite[/color](16, [color=#006699]HIGH[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(16, <span style="color: #006699;">HIGH</span>);
  [color=#CC6600]digitalWrite[/color](17, [color=#006699]HIGH[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(17, <span style="color: #006699;">HIGH</span>);
  [color=#CC6600]digitalWrite[/color](18, [color=#006699]HIGH[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(18, <span style="color: #006699;">HIGH</span>);
  [color=#CC6600]digitalWrite[/color](19, [color=#006699]HIGH[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(19, <span style="color: #006699;">HIGH</span>);
 
+
&nbsp;
  [color=#7E7E7E]//  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">//  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you</span>
  [color=#CC6600]if[/color] (!card.init()) {        [color=#7E7E7E]//play with 8 MHz spi (default faster!)  [/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (!card.init()) {        <span style="color: #7E7E7E;">//play with 8 MHz spi (default faster!)  </span>
    putstring_nl([color=#006699]"Card init. failed!"[/color]);  [color=#7E7E7E]// Something went wrong, lets print out why[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl(<span style="color: #006699;">"Card init. failed!"</span>);  <span style="color: #7E7E7E;">// Something went wrong, lets print out why</span>
    sdErrorCheck();
+
&nbsp;&nbsp;&nbsp;&nbsp;sdErrorCheck();
    [color=#CC6600]while[/color](1);                            [color=#7E7E7E]// then 'halt' - do nothing![/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">while</span>(1);                            <span style="color: #7E7E7E;">// then 'halt' - do nothing!</span>
  }
+
&nbsp;&nbsp;}
  
+
&nbsp;&nbsp;
  [color=#7E7E7E]// enable optimize read - some cards may timeout. Disable if you're having problems[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// enable optimize read - some cards may timeout. Disable if you're having problems</span>
  card.partialBlockRead([color=#CC6600]true[/color]);
+
&nbsp;&nbsp;card.partialBlockRead(<span style="color: #CC6600;">true</span>);
 
+
&nbsp;
[color=#7E7E7E]// Now we will look for a FAT partition![/color]
+
<span style="color: #7E7E7E;">//&nbsp;Now&nbsp;we&nbsp;will&nbsp;look&nbsp;for&nbsp;a&nbsp;FAT&nbsp;partition!</span>
  uint8_t part;
+
&nbsp;&nbsp;uint8_t&nbsp;part;
  [color=#CC6600]for[/color] (part = 0; part < 5; part++) {    [color=#7E7E7E]// we have up to 5 slots to look in[/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (part = 0; part < 5; part++) {    <span style="color: #7E7E7E;">// we have up to 5 slots to look in</span>
    [color=#CC6600]if[/color] (vol.init(card, part))  
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (vol.init(card, part))  
      [color=#CC6600]break[/color];                            [color=#7E7E7E]// we found one, lets bail[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">break</span>;                            <span style="color: #7E7E7E;">// we found one, lets bail</span>
  }
+
&nbsp;&nbsp;}
  [color=#CC6600]if[/color] (part == 5) {                      [color=#7E7E7E]// if we ended up not finding one  :([/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (part == 5) {                      <span style="color: #7E7E7E;">// if we ended up not finding one  :(</span>
    putstring_nl([color=#006699]"No valid FAT partition!"[/color]);
+
&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl(<span style="color: #006699;">"No valid FAT partition!"</span>);
    sdErrorCheck();      [color=#7E7E7E]// Something went wrong, lets print out why[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;sdErrorCheck();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Something went wrong, lets print out why</span>
    [color=#CC6600]while[/color](1);                            [color=#7E7E7E]// then 'halt' - do nothing![/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">while</span>(1);                            <span style="color: #7E7E7E;">// then 'halt' - do nothing!</span>
  }
+
&nbsp;&nbsp;}
  
+
&nbsp;&nbsp;
  [color=#7E7E7E]// Lets tell the user about what we found[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Lets tell the user about what we found</span>
  putstring([color=#006699]"Using partition "[/color]);
+
&nbsp;&nbsp;putstring(<span style="color: #006699;">"Using partition "</span>);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](part, [color=#006699]DEC[/color]);
+
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(part, <span style="color: #006699;">DEC</span>);
  putstring([color=#006699]", type is FAT"[/color]);
+
&nbsp;&nbsp;putstring(<span style="color: #006699;">", type is FAT"</span>);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](vol.fatType(),[color=#006699]DEC[/color]);    [color=#7E7E7E]// FAT16 or FAT32?[/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(vol.fatType(),<span style="color: #006699;">DEC</span>);    <span style="color: #7E7E7E;">// FAT16 or FAT32?</span>
  
+
&nbsp;&nbsp;
  [color=#7E7E7E]// Try to open the root directory[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Try to open the root directory</span>
  [color=#CC6600]if[/color] (!root.openRoot(vol)) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (!root.openRoot(vol)) {
    putstring_nl([color=#006699]"Can't open root dir!"[/color]); [color=#7E7E7E]// Something went wrong,[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl(<span style="color: #006699;">"Can't open root dir!"</span>); <span style="color: #7E7E7E;">// Something went wrong,</span>
    [color=#CC6600]while[/color](1);                            [color=#7E7E7E]// then 'halt' - do nothing![/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">while</span>(1);                            <span style="color: #7E7E7E;">// then 'halt' - do nothing!</span>
  }
+
&nbsp;&nbsp;}
  
+
&nbsp;&nbsp;
  [color=#7E7E7E]// Whew! We got past the tough parts.[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Whew! We got past the tough parts.</span>
  putstring_nl([color=#006699]"Ready!"[/color]);
+
&nbsp;&nbsp;putstring_nl(<span style="color: #006699;">"Ready!"</span>);
 
}
 
}
  
[color=#CC6600]void[/color] playcomplete([color=#CC6600]char[/color] good) {
+
<span style="color: #CC6600;">void</span> playcomplete(<span style="color: #CC6600;">char</span> good) {
  [color=#7E7E7E]// call our helper to find and play this name[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// call our helper to find and play this name</span>
  playfile([color=#006699]"GOOD.WAV"[/color]);
+
&nbsp;&nbsp;playfile(<span style="color: #006699;">"GOOD.WAV"</span>);
  [color=#CC6600]while[/color] (wave.isplaying) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">while</span> (wave.isplaying) {
  [color=#7E7E7E]// do nothing while its playing[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// do nothing while its playing</span>
  }
+
&nbsp;&nbsp;}
  [color=#7E7E7E]// now its done playing[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// now its done playing</span>
 
}
 
}
  
  
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
+
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>() {
  [color=#7E7E7E]//putstring(".");            // uncomment this to see if the loop isnt running  [/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">//putstring(".");            // uncomment this to see if the loop isnt running  </span>
    
+
&nbsp;&nbsp;&nbsp;&nbsp;
  [color=#CC6600]switch[/color] (check_switches()) {    [color=#7E7E7E]//this is the loop which cycles the arnold sounds based[/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;">switch</span> (check_switches()) {    <span style="color: #7E7E7E;">//this is the loop which cycles the arnold sounds based</span>
    [color=#CC6600]case[/color] 1:                      [color=#7E7E7E]//on which switch you connect to.[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">case</span> 1:                      <span style="color: #7E7E7E;">//on which switch you connect to.</span>
      playcomplete([color=#006699]"STOP.WAV"[/color]);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete(<span style="color: #006699;">"STOP.WAV"</span>);
      [color=#CC6600]break[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">break</span>;
    [color=#CC6600]case[/color] 2:
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">case</span> 2:
      playcomplete([color=#006699]"SOB.WAV"[/color]);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete(<span style="color: #006699;">"SOB.WAV"</span>);
      [color=#CC6600]break[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">break</span>;
    [color=#CC6600]case[/color] 3:
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">case</span> 3:
      playcomplete([color=#006699]"DONT.WAV"[/color]);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete(<span style="color: #006699;">"DONT.WAV"</span>);
      [color=#CC6600]break[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">break</span>;
    [color=#CC6600]case[/color] 4:
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">case</span> 4:
      playcomplete([color=#006699]"LACK.WAV"[/color]);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete(<span style="color: #006699;">"LACK.WAV"</span>);
      [color=#CC6600]break[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">break</span>;
    [color=#CC6600]case[/color] 5:
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">case</span> 5:
      playcomplete([color=#006699]"FIST.WAV"[/color]);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete(<span style="color: #006699;">"FIST.WAV"</span>);
  }
+
&nbsp;&nbsp;}
 
}
 
}
  
[color=#CC6600]byte[/color] check_switches()
+
<span style="color: #CC6600;">byte</span> check_switches()
 
{
 
{
  [color=#CC6600]static[/color] [color=#CC6600]byte[/color] previous[6];
+
&nbsp;&nbsp;<span style="color: #CC6600;">static</span> <span style="color: #CC6600;">byte</span> previous[6];
  [color=#CC6600]static[/color] [color=#CC6600]long[/color] time[6];
+
&nbsp;&nbsp;<span style="color: #CC6600;">static</span> <span style="color: #CC6600;">long</span> time[6];
  [color=#CC6600]byte[/color] reading;
+
&nbsp;&nbsp;<span style="color: #CC6600;">byte</span> reading;
  [color=#CC6600]byte[/color] pressed;
+
&nbsp;&nbsp;<span style="color: #CC6600;">byte</span> pressed;
  [color=#CC6600]byte[/color] index;
+
&nbsp;&nbsp;<span style="color: #CC6600;">byte</span> index;
  pressed = 0;
+
&nbsp;&nbsp;pressed&nbsp;=&nbsp;0;
  
  [color=#CC6600]for[/color] ([color=#CC6600]byte[/color] index = 0; index < 6; ++index) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">byte</span> index = 0; index < 6; ++index) {
    reading = [color=#CC6600]digitalRead[/color](14 + index);
+
&nbsp;&nbsp;&nbsp;&nbsp;reading&nbsp;=&nbsp;<span style="color: #CC6600;">digitalRead</span>(14 + index);
    [color=#CC6600]if[/color] (reading == [color=#006699]LOW[/color] && previous[index] == [color=#006699]HIGH[/color] && [color=#CC6600]millis[/color]() - time[index] > DEBOUNCE)
+
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (reading == <span style="color: #006699;">LOW</span> && previous[index] == <span style="color: #006699;">HIGH</span> && <span style="color: #CC6600;">millis</span>() - time[index] > DEBOUNCE)
    {
+
&nbsp;&nbsp;&nbsp;&nbsp;{
      [color=#7E7E7E]// switch pressed[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// switch pressed</span>
      time[index] = [color=#CC6600]millis[/color]();
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time[index]&nbsp;=&nbsp;<span style="color: #CC6600;">millis</span>();
      pressed = index + 1;
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pressed&nbsp;=&nbsp;index&nbsp;+&nbsp;1;
      [color=#CC6600]break[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">break</span>;
    }
+
&nbsp;&nbsp;&nbsp;&nbsp;}
    previous[index] = reading;
+
&nbsp;&nbsp;&nbsp;&nbsp;previous[index]&nbsp;=&nbsp;reading;
  }
+
&nbsp;&nbsp;}
  [color=#7E7E7E]// return switch number (1 - 6)[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// return switch number (1 - 6)</span>
  [color=#CC6600]return[/color] (pressed);
+
&nbsp;&nbsp;<span style="color: #CC6600;">return</span> (pressed);
 
}
 
}
  
  
[color=#7E7E7E]// Plays a full file from beginning to end with no pause.[/color]
+
<span style="color: #7E7E7E;">//&nbsp;Plays&nbsp;a&nbsp;full&nbsp;file&nbsp;from&nbsp;beginning&nbsp;to&nbsp;end&nbsp;with&nbsp;no&nbsp;pause.</span>
[color=#CC6600]void[/color] playcomplete([color=#CC6600]char[/color] *name) {
+
<span style="color: #CC6600;">void</span> playcomplete(<span style="color: #CC6600;">char</span> *name) {
  [color=#7E7E7E]// call our helper to find and play this name[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// call our helper to find and play this name</span>
  playfile(name);
+
&nbsp;&nbsp;playfile(name);
  [color=#CC6600]while[/color] (wave.isplaying) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">while</span> (wave.isplaying) {
  [color=#7E7E7E]// do nothing while its playing[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// do nothing while its playing</span>
  }
+
&nbsp;&nbsp;}
  [color=#7E7E7E]// now its done playing[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// now its done playing</span>
 
}
 
}
  
[color=#CC6600]void[/color] playfile([color=#CC6600]char[/color] *name) {
+
<span style="color: #CC6600;">void</span> playfile(<span style="color: #CC6600;">char</span> *name) {
  [color=#7E7E7E]// see if the wave object is currently doing something[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// see if the wave object is currently doing something</span>
  [color=#CC6600]if[/color] (wave.isplaying) {[color=#7E7E7E]// already playing something, so stop it![/color]
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (wave.isplaying) {<span style="color: #7E7E7E;">// already playing something, so stop it!</span>
    wave.[color=#CC6600]stop[/color](); [color=#7E7E7E]// stop it[/color]
+
&nbsp;&nbsp;&nbsp;&nbsp;wave.<span style="color: #CC6600;">stop</span>(); <span style="color: #7E7E7E;">// stop it</span>
  }
+
&nbsp;&nbsp;}
  [color=#7E7E7E]// look in the root directory and open the file[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// look in the root directory and open the file</span>
  [color=#CC6600]if[/color] (!f.open(root, name)) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (!f.open(root, name)) {
    putstring([color=#006699]"Couldn't open file "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](name); [color=#CC6600]return[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;putstring(<span style="color: #006699;">"Couldn't open file "</span>); <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(name); <span style="color: #CC6600;">return</span>;
  }
+
&nbsp;&nbsp;}
  [color=#7E7E7E]// OK read the file and turn it into a wave object[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// OK read the file and turn it into a wave object</span>
  [color=#CC6600]if[/color] (!wave.create(f)) {
+
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (!wave.create(f)) {
    putstring_nl([color=#006699]"Not a valid WAV"[/color]); [color=#CC6600]return[/color];
+
&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl(<span style="color: #006699;">"Not a valid WAV"</span>); <span style="color: #CC6600;">return</span>;
  }
+
&nbsp;&nbsp;}
  
+
&nbsp;&nbsp;
  [color=#7E7E7E]// ok time to play! start playback[/color]
+
&nbsp;&nbsp;<span style="color: #7E7E7E;">// ok time to play! start playback</span>
  wave.play();
+
&nbsp;&nbsp;wave.play();
 
}
 
}
  
[/quote]
+
</pre>

Revision as of 02:09, 20 March 2010

I am a Visual Arts - Media major with an interest in graphic design. I was born in Europe, and moved to Canada when I was 5. I've lived in San Diego since I was 10, and graduated from University City High School before coming to UCSD as a transfer. I have been working with computers since grade school, and am highly interested in pursuing a career in the computer field. I have been in interested in electronics also since a young age and I think it will add an interesting dimension to my artistic career, and am eagerly looking forward to learning how to make art projects using them.


DSC_0055-1.jpg

Portable Heater


DSC_0051.jpg

Generator?


DSC_0050.jpg

Air Conditioner


DSC_0049.jpg

Cell Phone


DSC_0046.jpg

Car Keys


Midterm Proposal

My proposal is a hat which contains a cooling fan enabled by a switch. The switch will hang down from the inside of the hat to about shoulder length, enabling the wearer to turn on and off the fan. This is a crude implementation of a warm weather cowboy hat which provides cooling apart from shade. It will be powered by a 9v battery, and connected by a normally open switch. In order to operate the fan, you must hold down the button.


MidDiag.jpg


Final Proposal

My final project will be called "Punch a Celebrity". It will consist of a punching pad with a picture of a celebrity on it. The pad will have an impact sensor attached to a arduino/breakout board, which is attached to a speaker. This will enable a wav or mp3 file to play at the moment of impact, and simulate that you are actually hitting a celebrity.

proposal.png

Code


<span style="color: #7E7E7E;">//Ian Petrov VIS147A Final</span>
<span style="color: #7E7E7E;">//code edited from Adafruit Wave Shield Site</span>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include <span style="color: #006699;">"WaveUtil.h"</span>
#include <span style="color: #006699;">"WaveHC.h"</span>


SdReader card;    <span style="color: #7E7E7E;">// This object holds the information for the card</span>
FatVolume vol;    <span style="color: #7E7E7E;">// This holds the information for the partition on the card</span>
FatReader root;   <span style="color: #7E7E7E;">// This holds the information for the filesystem on the card</span>
FatReader f;      <span style="color: #7E7E7E;">// This holds the information for the file we're play</span>

WaveHC wave;      <span style="color: #7E7E7E;">// This is the only wave (audio) object, since we will only play one at a time</span>

#define DEBOUNCE 100  <span style="color: #7E7E7E;">// button debouncer</span>

<span style="color: #7E7E7E;">// this handy function will return the number of bytes currently free in RAM, great for debugging!   </span>
<span style="color: #CC6600;">int</span> freeRam(<span style="color: #CC6600;">void</span>)
{
  extern <span style="color: #CC6600;">int</span>  __bss_end; 
  extern <span style="color: #CC6600;">int</span>  *__brkval; 
  <span style="color: #CC6600;">int</span> free_memory; 
  <span style="color: #CC6600;">if</span>((<span style="color: #CC6600;">int</span>)__brkval == 0) {
    free_memory = ((<span style="color: #CC6600;">int</span>)&free_memory) - ((<span style="color: #CC6600;">int</span>)&__bss_end); 
  }
  <span style="color: #CC6600;">else</span> {
    free_memory = ((<span style="color: #CC6600;">int</span>)&free_memory) - ((<span style="color: #CC6600;">int</span>)__brkval); 
  }
  <span style="color: #CC6600;">return</span> free_memory; 
} 

<span style="color: #CC6600;">void</span> sdErrorCheck(<span style="color: #CC6600;">void</span>)
{
  <span style="color: #CC6600;">if</span> (!card.errorCode()) <span style="color: #CC6600;">return</span>;
  putstring(<span style="color: #006699;">"\n\rSD I/O error: "</span>);
  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(card.errorCode(), <span style="color: #006699;">HEX</span>);
  putstring(<span style="color: #006699;">", "</span>);
  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(card.errorData(), <span style="color: #006699;">HEX</span>);
  <span style="color: #CC6600;">while</span>(1);
}

<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>() {
  <span style="color: #7E7E7E;">// set up serial port</span>
  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
  putstring_nl(<span style="color: #006699;">"WaveHC with 6 buttons"</span>);
  
   putstring(<span style="color: #006699;">"Free RAM: "</span>);       <span style="color: #7E7E7E;">// This can help with debugging, running out of RAM is bad</span>
  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(freeRam());      <span style="color: #7E7E7E;">// if this is under 150 bytes it may spell trouble!</span>
  
  <span style="color: #7E7E7E;">// Set the output pins for the DAC control. This pins are defined in the library</span>
  <span style="color: #CC6600;">pinMode</span>(2, <span style="color: #006699;">OUTPUT</span>);
  <span style="color: #CC6600;">pinMode</span>(3, <span style="color: #006699;">OUTPUT</span>);
  <span style="color: #CC6600;">pinMode</span>(4, <span style="color: #006699;">OUTPUT</span>);
  <span style="color: #CC6600;">pinMode</span>(5, <span style="color: #006699;">OUTPUT</span>);
 
  <span style="color: #7E7E7E;">// pin13 LED</span>
  <span style="color: #CC6600;">pinMode</span>(13, <span style="color: #006699;">OUTPUT</span>);
 
  <span style="color: #7E7E7E;">// enable pull-up resistors on switch pins (analog inputs)</span>
  <span style="color: #CC6600;">digitalWrite</span>(14, <span style="color: #006699;">HIGH</span>);
  <span style="color: #CC6600;">digitalWrite</span>(15, <span style="color: #006699;">HIGH</span>);
  <span style="color: #CC6600;">digitalWrite</span>(16, <span style="color: #006699;">HIGH</span>);
  <span style="color: #CC6600;">digitalWrite</span>(17, <span style="color: #006699;">HIGH</span>);
  <span style="color: #CC6600;">digitalWrite</span>(18, <span style="color: #006699;">HIGH</span>);
  <span style="color: #CC6600;">digitalWrite</span>(19, <span style="color: #006699;">HIGH</span>);
 
  <span style="color: #7E7E7E;">//  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you</span>
  <span style="color: #CC6600;">if</span> (!card.init()) {         <span style="color: #7E7E7E;">//play with 8 MHz spi (default faster!)  </span>
    putstring_nl(<span style="color: #006699;">"Card init. failed!"</span>);  <span style="color: #7E7E7E;">// Something went wrong, lets print out why</span>
    sdErrorCheck();
    <span style="color: #CC6600;">while</span>(1);                            <span style="color: #7E7E7E;">// then 'halt' - do nothing!</span>
  }
  
  <span style="color: #7E7E7E;">// enable optimize read - some cards may timeout. Disable if you're having problems</span>
  card.partialBlockRead(<span style="color: #CC6600;">true</span>);
 
<span style="color: #7E7E7E;">// Now we will look for a FAT partition!</span>
  uint8_t part;
  <span style="color: #CC6600;">for</span> (part = 0; part < 5; part++) {     <span style="color: #7E7E7E;">// we have up to 5 slots to look in</span>
    <span style="color: #CC6600;">if</span> (vol.init(card, part)) 
      <span style="color: #CC6600;">break</span>;                             <span style="color: #7E7E7E;">// we found one, lets bail</span>
  }
  <span style="color: #CC6600;">if</span> (part == 5) {                       <span style="color: #7E7E7E;">// if we ended up not finding one  :(</span>
    putstring_nl(<span style="color: #006699;">"No valid FAT partition!"</span>);
    sdErrorCheck();      <span style="color: #7E7E7E;">// Something went wrong, lets print out why</span>
    <span style="color: #CC6600;">while</span>(1);                            <span style="color: #7E7E7E;">// then 'halt' - do nothing!</span>
  }
  
  <span style="color: #7E7E7E;">// Lets tell the user about what we found</span>
  putstring(<span style="color: #006699;">"Using partition "</span>);
  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(part, <span style="color: #006699;">DEC</span>);
  putstring(<span style="color: #006699;">", type is FAT"</span>);
  <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(vol.fatType(),<span style="color: #006699;">DEC</span>);     <span style="color: #7E7E7E;">// FAT16 or FAT32?</span>
  
  <span style="color: #7E7E7E;">// Try to open the root directory</span>
  <span style="color: #CC6600;">if</span> (!root.openRoot(vol)) {
    putstring_nl(<span style="color: #006699;">"Can't open root dir!"</span>); <span style="color: #7E7E7E;">// Something went wrong,</span>
    <span style="color: #CC6600;">while</span>(1);                             <span style="color: #7E7E7E;">// then 'halt' - do nothing!</span>
  }
  
  <span style="color: #7E7E7E;">// Whew! We got past the tough parts.</span>
  putstring_nl(<span style="color: #006699;">"Ready!"</span>);
}

<span style="color: #CC6600;">void</span> playcomplete(<span style="color: #CC6600;">char</span> good) {
  <span style="color: #7E7E7E;">// call our helper to find and play this name</span>
  playfile(<span style="color: #006699;">"GOOD.WAV"</span>);
  <span style="color: #CC6600;">while</span> (wave.isplaying) {
  <span style="color: #7E7E7E;">// do nothing while its playing</span>
  }
  <span style="color: #7E7E7E;">// now its done playing</span>
}


<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>() {
  <span style="color: #7E7E7E;">//putstring(".");            // uncomment this to see if the loop isnt running  </span>
    
  <span style="color: #CC6600;">switch</span> (check_switches()) {     <span style="color: #7E7E7E;">//this is the loop which cycles the arnold sounds based</span>
    <span style="color: #CC6600;">case</span> 1:                       <span style="color: #7E7E7E;">//on which switch you connect to.</span>
      playcomplete(<span style="color: #006699;">"STOP.WAV"</span>);
      <span style="color: #CC6600;">break</span>;
    <span style="color: #CC6600;">case</span> 2:
      playcomplete(<span style="color: #006699;">"SOB.WAV"</span>);
      <span style="color: #CC6600;">break</span>;
    <span style="color: #CC6600;">case</span> 3:
      playcomplete(<span style="color: #006699;">"DONT.WAV"</span>);
      <span style="color: #CC6600;">break</span>;
    <span style="color: #CC6600;">case</span> 4:
      playcomplete(<span style="color: #006699;">"LACK.WAV"</span>);
      <span style="color: #CC6600;">break</span>;
    <span style="color: #CC6600;">case</span> 5:
      playcomplete(<span style="color: #006699;">"FIST.WAV"</span>);
  }
}

<span style="color: #CC6600;">byte</span> check_switches()
{
  <span style="color: #CC6600;">static</span> <span style="color: #CC6600;">byte</span> previous[6];
  <span style="color: #CC6600;">static</span> <span style="color: #CC6600;">long</span> time[6];
  <span style="color: #CC6600;">byte</span> reading;
  <span style="color: #CC6600;">byte</span> pressed;
  <span style="color: #CC6600;">byte</span> index;
  pressed = 0;

  <span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">byte</span> index = 0; index < 6; ++index) {
    reading = <span style="color: #CC6600;">digitalRead</span>(14 + index);
    <span style="color: #CC6600;">if</span> (reading == <span style="color: #006699;">LOW</span> && previous[index] == <span style="color: #006699;">HIGH</span> && <span style="color: #CC6600;">millis</span>() - time[index] > DEBOUNCE)
    {
      <span style="color: #7E7E7E;">// switch pressed</span>
      time[index] = <span style="color: #CC6600;">millis</span>();
      pressed = index + 1;
      <span style="color: #CC6600;">break</span>;
    }
    previous[index] = reading;
  }
  <span style="color: #7E7E7E;">// return switch number (1 - 6)</span>
  <span style="color: #CC6600;">return</span> (pressed);
}


<span style="color: #7E7E7E;">// Plays a full file from beginning to end with no pause.</span>
<span style="color: #CC6600;">void</span> playcomplete(<span style="color: #CC6600;">char</span> *name) {
  <span style="color: #7E7E7E;">// call our helper to find and play this name</span>
  playfile(name);
  <span style="color: #CC6600;">while</span> (wave.isplaying) {
  <span style="color: #7E7E7E;">// do nothing while its playing</span>
  }
  <span style="color: #7E7E7E;">// now its done playing</span>
}

<span style="color: #CC6600;">void</span> playfile(<span style="color: #CC6600;">char</span> *name) {
  <span style="color: #7E7E7E;">// see if the wave object is currently doing something</span>
  <span style="color: #CC6600;">if</span> (wave.isplaying) {<span style="color: #7E7E7E;">// already playing something, so stop it!</span>
    wave.<span style="color: #CC6600;">stop</span>(); <span style="color: #7E7E7E;">// stop it</span>
  }
  <span style="color: #7E7E7E;">// look in the root directory and open the file</span>
  <span style="color: #CC6600;">if</span> (!f.open(root, name)) {
    putstring(<span style="color: #006699;">"Couldn't open file "</span>); <span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(name); <span style="color: #CC6600;">return</span>;
  }
  <span style="color: #7E7E7E;">// OK read the file and turn it into a wave object</span>
  <span style="color: #CC6600;">if</span> (!wave.create(f)) {
    putstring_nl(<span style="color: #006699;">"Not a valid WAV"</span>); <span style="color: #CC6600;">return</span>;
  }
  
  <span style="color: #7E7E7E;">// ok time to play! start playback</span>
  wave.play();
}