Show Posts
Pages: 1 2 [3] 4
31  Using Arduino / Sensors / Re: AMBI sensor and wave shield on: November 22, 2011, 10:50:17 pm
Doesn't work... maybe I am inserting the code into the wrong part of the program?
32  Using Arduino / Sensors / AMBI sensor and wave shield on: November 22, 2011, 09:35:09 pm
Im sure this is really easy but I'm new smiley-wink

Essentially what I want to do is have my wave shield play a song when the value from the AMBI sensor ( hooked up to analog pin 0 ) reaches a certain brightness. Heres the code I've got running the Waveshield:

Code:
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

int LDR = 0; // LDR Pin
int val = 0;       // variable to store the value coming from the sensor



// Function definitions (we define them here, but the code is below)
void lsR(FatReader &d);
void play(FatReader &dir);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  pinMode(LDR, INPUT);       // declare the LDR as an INPUT
  putstring_nl("\nWave test!");  // say we woke up!
 
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam()); 
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!) 
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
 
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
 
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part))
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
 
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
 
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
 
  // Whew! We got past the tough parts.
  putstring_nl("Files found:");
  dirLevel = 0;
  // Print out all of the files in all the directories.
  lsR(root);
}

//////////////////////////////////// LOOP
void loop() {
  root.rewind();
  play(root);
  val = analogRead(LDR);       // read the value from the sensor
}

/////////////////////////////////// HELPERS

// this handy function will return the number of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}

/*
 * print error message and halt if SD I/O error, great for debugging!
 */
void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
/*
 * print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
 */
void printName(dir_t &dir)
{
  for (uint8_t i = 0; i < 11; i++) {     // 8.3 format has 8+3 = 11 letters in it
    if (dir.name[i] == ' ')
        continue;         // dont print any spaces in the name
    if (i == 8)
        Serial.print('.');           // after the 8th letter, place a dot
    Serial.print(dir.name[i]);      // print the n'th digit
  }
  if (DIR_IS_SUBDIR(dir))
    Serial.print('/');       // directories get a / at the end
}
/*
 * list recursively - possible stack overflow if subdirectories too nested
 */
void lsR(FatReader &d)
{
  int8_t r;                     // indicates the level of recursion
 
  while ((r = d.readDir(dirBuf)) > 0) {     // read the next file in the directory
    // skip subdirs . and ..
    if (dirBuf.name[0] == '.')
      continue;
   
    for (uint8_t i = 0; i < dirLevel; i++)
      Serial.print(' ');        // this is for prettyprinting, put spaces in front
    printName(dirBuf);          // print the name of the file we just found
    Serial.println();           // and a new line
   
    if (DIR_IS_SUBDIR(dirBuf)) {   // we will recurse on any direcory
      FatReader s;                 // make a new directory object to hold information
      dirLevel += 2;               // indent 2 spaces for future prints
      if (s.open(vol, dirBuf))
        lsR(s);                    // list all the files in this directory now!
      dirLevel -=2;                // remove the extra indentation
    }
  }
  sdErrorCheck();                  // are we doign OK?
}
/*
 * play recursively - possible stack overflow if subdirectories too nested
 */
void play(FatReader &dir)
{
  FatReader file;
  while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
    // skip . and .. directories
    if (dirBuf.name[0] == '.')
      continue;
   
    Serial.println();            // clear out a new line
   
    for (uint8_t i = 0; i < dirLevel; i++)
       Serial.print(' ');       // this is for prettyprinting, put spaces in front

    if (!file.open(vol, dirBuf)) {       // open the file in the directory
      Serial.println("file.open failed");  // something went wrong :(
      while(1);                            // halt
    }
   
    if (file.isDir()) {                    // check if we opened a new directory
      putstring("Subdir: ");
      printName(dirBuf);
      dirLevel += 2;                       // add more spaces
      // play files in subdirectory
      play(file);                         // recursive!
      dirLevel -= 2;   
    }
    else {
      // Aha! we found a file that isnt a directory
      putstring("Playing "); printName(dirBuf);       // print it out
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
        putstring(" Not a valid WAV");     // ok skip it
      } else {
        Serial.println();                  // Hooray it IS a WAV proper!
        wave.play();                       // make some noise!
       
        while (wave.isplaying) {           // playing occurs in interrupts, so we print dots in realtime
          putstring(".");
          delay(100);
        }
        sdErrorCheck();                    // everything OK?
//        if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
  }
}

If someone could help me out that would be great smiley
33  Forum 2005-2010 (read only) / Syntax & Programs / Re: Automatic loop execution and termination on: October 18, 2009, 11:11:48 am
Hey thanks a lot Mikal! I've got it running now to test it out.
34  Forum 2005-2010 (read only) / Syntax & Programs / Re: Automatic loop execution and termination on: October 18, 2009, 02:57:55 am
Thank you so much Mikal, this is such a great start! Do you think you could show me how to integrate your code into mine?
35  Forum 2005-2010 (read only) / Syntax & Programs / Re: Automatic loop execution and termination on: October 17, 2009, 04:24:35 pm
I thought I could insert another void loop() ending with a  delay(10000); but that messed everything up.
36  Forum 2005-2010 (read only) / Syntax & Programs / Re: Automatic loop execution and termination on: October 17, 2009, 04:21:48 pm
Ok. Thanks everyone. Would someone be able to tell me how I could get the LED's to light for a given length of time then shut off for a given length of time and then loop so they are back on?
37  Forum 2005-2010 (read only) / Syntax & Programs / Re: Automatic loop execution and termination on: October 17, 2009, 03:48:15 pm
I'm happy with the code and I'm planning on running with a battery. Would it be possible to put it on a loop of 86,400,000 milliseconds (number of milliseconds in 24 hours) and specify when it should and shouldn't run?
38  Forum 2005-2010 (read only) / Syntax & Programs / Re: Automatic loop execution and termination on: October 17, 2009, 03:33:52 pm
Thanks for the tip! I'm new at this smiley
39  Forum 2005-2010 (read only) / Syntax & Programs / Automatic loop execution and termination on: October 17, 2009, 03:28:12 pm
Hi guys, I want to have this code set to execute automatically at 8pm and then stop around 1am. I have no clue how to do this so I was wondering if someone would be so kind as to help me out?

Code:
long time;                                                                    // variable for speed debug
float pwmSpeed[14] = {
 0, 0, 1.2, 1.3, 1.4, 1.9, .9, .8, .5, 1.2, 1.37, 1.47, .3, 3.2};             // these constants set the rate of dimming
int pwmVal[14];                                                               // PWM values for 12 channels - 0 & 1 included but not used
float pwmFloats[14];
int i, j, k, l, x, y, z, bufsize, pot;                                        // variables for various counters

unsigned char sinewave[] =        //256 values
{
  0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,

  0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,

  0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,

  0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,

  0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,

  0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,

  0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,

  0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,

  0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,

  0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,

  0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,

  0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,

  0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,

  0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,

  0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,

  0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c

};


void setup(){
  Serial.begin(9600);
  DDRD=0xFC;      // direction variable for port D - make em all outputs except serial pins 0 & 1
  DDRB=0xFF;      // direction variable for port B - all outputs

}

void loop(){

  // time = millis();               // this was to test the loop speed
  // for (z=0; z<1000; z++){        // ditto

  //  pot = analogRead(0);          // this implemented a potentiometer speed control to control speed of fading

  for (y=0; y<14; y++){             // calculate one new pwm value every time through the control loop
    j = (j + 1) % 12;              // calculate a new j every time - modulo operator makes it cycle back to 0 after 11
    k = j + 2;                      // add 2 tp tje result - this yields a cycle of 2 to 13 for the channel (pin) select numbers

    pwmFloats[k] =  (pwmFloats[k] + pwmSpeed[k]);
    // pwmFloats[k] =  (pwmFloats[k] + ((pwmSpeed[k]  * 15 * (float)pot) / 1023));    // implements potentiometer speed control - see line above

      if (pwmFloats[k] >= 256){                  // wrop around sinewave table index values that are larger than 256
      pwmFloats[k] = pwmFloats[k] - 256;
    }
    else if  (pwmFloats[k] < 0){
      pwmFloats[k] = pwmFloats[k] + 256;        // wrop around sinewave table index values that are less than 0
    }

    pwmVal[k] = sinewave[(int)pwmFloats[k]];                   // convert the float value to an integer and get the value out of the sinewave index
  }

  PORTD = 0xFC;              // all outputs except serial pins 0 & 1
  PORTB = 0xFF;              // turn on all pins of ports D & B

for (z=0; z<3; z++){         // this loop just adds some more repetitions of the loop below to cut down on the time overhead of loop above
                             // increase this until you start to preceive flicker - then back off - decrease for more responsive sensor input reads
  for (x=0; x<256; x++){
    for( i=2; i<14; i++){    // start with 2 to avoid serial pins
      if (x == pwmVal[i]){
        if (i < 8){    // corresponds to PORTD
          // bitshift a one into the proper bit then reverse the whole byte
          // equivalent to the line below but around 4 times faster
          // digitalWrite(i, LOW);
          PORTD = PORTD & (~(1 << i));
        }  
        else{  
          PORTB = PORTB & (~(1 << (i-8)));         // corresponds to PORTB - same as digitalWrite(pin, LOW); - on Port B pins
        }

      }
    }
  }
}
  //    }
  //    Serial.println((millis() - time), DEC);     // speed test code

}



40  Forum 2005-2010 (read only) / Syntax & Programs / Re: LED Fading question on: July 12, 2009, 08:42:29 am
Please realize that I'm a beginner and I can't just take your code and roll with it. I didn't really appreciate the sarcasm.
41  Forum 2005-2010 (read only) / Syntax & Programs / Re: LED Fading question on: July 11, 2009, 11:54:06 am
How would I integrate that into the the code i've got?
42  Forum 2005-2010 (read only) / Syntax & Programs / Re: LED Fading question on: July 10, 2009, 07:47:22 pm
Ok, so this code is probably super inefficient and terrible, but it semi works. I would really like to fade up a series of leds in succession which is what this code does but its very choppy. Is there any way I can smooth the transition? I tried lowering the value+ down to one and that helped a bit, I soon discovered you can't use numbers less than one (noOb) lol.

int value = 0;                            // variable to keep the actual value
int ledpin = 3;                           // light connected to digital pin 9
int ledpin1 = 4;
int ledpin2 = 12;
int ledpin3 = 13;
void setup()
{
  for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
  {
    analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
    delay(2);                            // waits for 30 milli seconds to see the dimming effect
  }
 
  for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
  {
    analogWrite(ledpin1, value);           // sets the value (range from 0 to 255)
    delay(2);                            // waits for 30 milli seconds to see the dimming effect
  }

for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
  {
    analogWrite(ledpin2, value);           // sets the value (range from 0 to 255)
    delay(2);                            // waits for 30 milli seconds to see the dimming effect
  }
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
  {
    analogWrite(ledpin3, value);           // sets the value (range from 0 to 255)
    delay(2);                            // waits for 30 milli seconds to see the dimming effect
  }



}




void loop()
{
  // nothing to loop
}  
43  Forum 2005-2010 (read only) / Syntax & Programs / LED Fading question on: July 05, 2009, 04:21:50 pm
Hello everyone, I'm very new to the arduino development scene and I could sure use some help. I just have a couple of basic questions.

1) I need code that will fade up an LED but keep it lit (not dim it back down). I tried modifying the fading sketch built into the program but I didn't have any luck.

2) Is it possible to fed an LED on pins that dont have PWM?

3) Is there a resource available that will teach me how to program in C for arduino?
44  Forum 2005-2010 (read only) / Troubleshooting / Re: Can't upload to sanguino :<( on: June 18, 2009, 06:20:10 pm
I cut the line and it works great. Strange that they would send it to me soldered that way...anyway thanks!
45  Forum 2005-2010 (read only) / Troubleshooting / Re: Can't upload to sanguino :<( on: June 18, 2009, 10:31:52 am
Well I planned on having its usual power source be the usb, I dont have an external power adapter. Could you please elaborate on the loopback test, I just tried looking up how to do it for this particular device and I can't really find any documentation.
Pages: 1 2 [3] 4