Show Posts
Pages: [1] 2 3
1  Using Arduino / Storage / Re: New SdFat optimized for Mega and Teensy 3.0 on: November 05, 2012, 08:59:09 am
1) Great work on the awesome library!

2) I may be out to lunch here, but I have a pickle of a problem driving me nuts with SPI;
--If I understand correctly, the Teensy 3.0 has multiple SPI ports. (yay/nay?)  They are numbered on the pinout sheet but there isn't a lot of data available yet -- and the proper datasheet is WAY above my understanding. (appreciate the patience with my noobtacular questions)
--Can the SDFat library be pointed to the secondary spi port (1 or 2 instead of 0)?

Long story short I'm interfacing an SD card and a non-standard spi device (addressable led strand that accepts clock/data) with FastSPI library.  It worked on arduino but I needed more speed, so I moved to Teensy.  I fake chip/slave select with a transistor interrupting the clock line while the sd reads - so far so good. -- the new library reads the files exceptionally well (an order of magnitude faster!) but when I go to use the spi port while I'm not reading/writing anything, I still get garbled data out.  I was hoping to move one of the competing libraries to a separate spi port, or (gasp) bitbang it. smiley-sad  I don't think I can maintain the immense datarate of hardware spi bitbanging it.

Thanks in advance and again great work!
2  Using Arduino / LEDs and Multiplexing / Re: Shift Registry Question on: October 16, 2012, 04:49:15 am
There are different versions of the ws2801 chip; some for 5v and some for 12v.

Daniel Garcia made a good library to use SPI EXTREMELY efficiently to drive most led strands (in the megahertz) the cost of some chip functionality because of how low level the code is.

LadyAda made a great library for SPI/and bitbang control of ws2801 - it's not nearly as efficient but it easily outputs hundreds of frames per second on a 64 rgb strand.
3  Using Arduino / LEDs and Multiplexing / Re: big led project on: October 16, 2012, 04:41:16 am
There might be some residual current leaking from the led pin on the arduino depending on how you have the pin set up in code.

Make sure you have
pinMode(ledpin,OUTPUT); //7 8 9 in the example circuit.
in your setup function - change out ledpin with whatever your pins are, you could even just use the numbers.

Also, likewise you may need a pulldown resistor from the pin directly to ground to make sure the capacitor in the mosfet doesn't cause you any trouble.  Try adding a 10k resistor from each led output pin directly to ground.  This higher value resistor will make sure the output truly sees 0v ground, but when the pin turns on, the 5v will easily overpower that resistor and go to the mosfet gate.
4  Using Arduino / LEDs and Multiplexing / Re: 8 * 16 RGB LED MATRIX on: October 12, 2012, 09:25:52 am
What level of control do you want for these leds?
RGB on/off (RGBCMYKWBl) 7 colour +white and black is most reasonable with shift registers.
Keep in mind the 595 can't source or sink much current.  If you want to drive multiple leds at the same time (you do) you want to have individual buffer transistors on EACH output (row/column) to drive enough current. 8 * .02 = 160mA = more current than one column driver could support.

You have CA and CC leds -- ideally you want not-common...isolated rgb cathode and anode so you can drive all 3 colours at the same time, but if they are common, you'll have to drive the reds, then the greens, then the blues separately.

Are you driving the columns AND rows with shift registers?  If so you need to address them all, every frame.
an output frame would look like
latch low
redColumn16b, greenColumn16b, blueColumn16b (6 bytes with only 1 bit set at a time), then 8 bits for that colour
latch high
might look like {01000000,00000000,00000000,00000000,0000000,000000000,10101010} //so only one colour column is driven at a time, with the last byte mattering.
You can do it more efficiently I suppose with 8bit columns and 16bit (unsigned integer) colour rows, but it takes more ram, your choice.  Since a max of 16 leds can be on at a time instead of 8 it might change things a bit.
How much other code do you plan to do?  Do you need all the other arduino pins?  You could easily skip a register for just using 8 pins and output 8 shifted bits faster.  You can set an entire port of pins with one line of code (such as the 'row' byte)...

the code will be very similar to the code for the 8x8;

I HIGHLY recommend understanding the 595 shift register tutorial

Also good to learn would be bitwise math, because shifting out is done in bits not bytes.  Your rgb matrix can be stored in 3 arrays of 16 bytes (48 bytes total).

byte red[15]; //each byte represents a row of 8 bits of that colour
byte blue[15];
byte green[15];

unsigned int frame[7]; //7 byte frame to be shifted out

//to simplify procedural assertion of column bits within a row, you could declare various named patterns
byte bits[8] = {1,2,4,8,16,32,64,128}; //binary equivalent of single bits

a simple for loop would count 0-15
1 byte would represent a row of r, or g, or b

in your main loop you need to address
-setting an animation pattern (static pattern to begin) - choose 1 frame at a time of course
-outputting the pattern
I'd consider generating the frame[] with 2 nested for loops
column and colour

the first 6 bytes are set using the bits[] array
frame[] = {0,0,0,0,0};
int temp[2] = {0,0};
for (int column = 0; column < 16; column++)
temp[] = {0,0}; //convert the column into 2 bytes
if (colour < 8) {temp[0] = bits[column];}
else {temp[1] = bits[(column-8)];}

for (int colour = 0; colour < 3; colour++)

if (colour = 0){//red
frame[0] = temp[0];
frame[1] = temp[1;]
frame[6] = red[column];

if (colour = 1){//green
frame[2] = temp[0];
frame[3] = temp[1];
frame[6] = green[column];

if (colour = 2){//blue
frame[4] = temp[0];
frame[5] = temp[1];
frame[6] = blue[column];

}end for
}end for
5  Using Arduino / LEDs and Multiplexing / Re: 2 pin RGB rainbow-LED on: October 12, 2012, 08:04:12 am
it would work but they don't have accurate timing in their internal processors, so the temporal drift would make them do random things as they heated and cooled differently.  It only takes a miniscule change at 'megahertz' to multiply quickly into a real distortion.
6  Using Arduino / Programming Questions / Re: Debounce Problem, please help on: October 12, 2012, 07:52:29 am
I'm supposing that since there is no wait between debouncing the button and looping through the code, you just keep looping and WHEN the button is pressed a pseudoRANDOM part of your code activates, "hey I got a button press after the red but before the green section so I'll light up green'.

doesn't do exactly as you say, because it doesn't wait for the old animation before mixing colours...
Includes debounce code from

I don't have my duino plugged in, or any buttons/leds handy, so I can't actually test it but I'm fairly confident this will work.

const int triggerPin = 2;
const int rledPin = 9;
const int gledPin = 10;
const int bledPin = 11;

byte rValue = 255; //global values for the leds
byte gValue = 255;
byte bValue = 255;

int animStep = 0; //value from 0-3, referring to white, red, green, blue
int dimmingTimer = 2;   //number of mS between updates (ish).  higher = slower
boolean lastButtonState = LOW; //used for debounce, previous reading of button state
boolean buttonState = LOW;     //current reading of button state

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup ()
  pinMode (triggerPin, INPUT);
  pinMode (rledPin, OUTPUT);
  pinMode (gledPin, OUTPUT);
  pinMode (bledPin, OUTPUT);

void loop()
//update leds

//dim led values

//check for a button press, assigns values to buttonState (global)

//act on button if pressed, setting appropriate led value high
if (buttonState) {
buttonState = LOW;
} //end if

//animation delay

}//end loop

void updateLedValues() //button detected, update led values
switch (animStep) { //do cool stuff in here.
  case 0:   //first step, white
  rValue = 255;
  gValue = 255;
  bValue = 255;
  case 1: //add red
  rValue = 255;
  case 2: //add green
  gValue = 255;
  case 3: //add blue
  bValue = 255;

    // statements
}//end switch animStep

if (animStep > 3) animStep = 0;//loop animation

}//end updateLedValues

void checkButton() //includes debounce code from
{ //saves a debounced button state to global buttonState
  // read the state of the switch into a local variable:
  int reading = digitalRead(triggerPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise: 

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;


void dimLedValues()//move r g b values down one notch if above zero
if (rValue>0) rValue--;
if (gValue>0) rValue--;
if (bValue>0) rValue--;

void displayColour() //set the analog outputs to the values set elsewhere
}//end displayColour
7  Using Arduino / Storage / Re: Read data from .txt file (from SD card) to run pan & tilt servos on: September 30, 2012, 06:20:32 am
PaulS put it very simply, you have to read the file from end to end, stopping at the data you want.

If you save your data in CSV -- comma separated values -- you can store columns in a program like excel as

Then have a sketch read the file, one byte at a time.  if it encounters a comma, it moves on to the next data value.  If it encounters asterisk, it stops reading until the next timestamp.

Not super useful code but it gets what I mean across I hope;  Won't work unless the file is explicitly designed to do this.  Might also trigger false positives if the values of "," and "*" come up...
//assumes a binary file with "," separating values in bytes, and "*" and the end of each line.
char dataByte[2];
int myservos[4];
int i = 0;
while dataByte[0] != "*"{
dataByte[0] = dataByte[1];
dataByte[1] =;
//do something to act on that byte, this is where Paul's suggestion of atoi(); //ascii to integer comes in since you
//might need to load more than one byte to make a number bigger than 255
if (dataByte[0] == ",") {
myservos[i] = dataByte[1];

8  Using Arduino / Storage / Re: Read data from .txt file (from SD card) to run pan & tilt servos on: September 27, 2012, 09:24:38 pm
Should be doable; remember, readable by human and readable by computer are 2 very different things. 

you have floating point values there for each number; Those numbers aren't too bad but they represent what the sensor said and not what a servo would have to do to replicate that number.  Also, they need to (ought to) be quantized into integers that a servo would understand if you used the servo library to ease input/output math.  That value could be stored as 1 or 2 bytes in a formatted binary file instead of as ascii text in a human readable file.

The numbers you have represented there indicate an orientation in x, y, and z assuming gravity pulls down, there is 1 g of acceleration on the z, and x/y are both very low values. 

You need to do a bit of math to convert those acceleration amplitudes into an angle that can be represented as pan and tilt.  It would be a good plan to import these values into excel and convert the x and y values into a number between 0-255 for full negative deflection (-1) and full positive deflection (1).
Numbers over 1g indicate that more than gravity was acting on this thing, and it was accelerating on its own accord.
9  Using Arduino / Storage / Re: SD Card bitmap read error/bug? on: September 27, 2012, 06:18:00 am
Cross posted to the FastSPI code page:

I'm pretty sure the FastSPI code/pointers are stepping on the memory buffer area of the SD library.
If I never call the function it reads the bitmap and serial.print's it just fine.  If I call (the whole point of this exercise) every 8 and 8+2 lines have errors in them -- seemingly totally random data...amounts to every 1500 or so bytes.

Attached are the files for your viewing pleasure;

The log files are legend:
line number of the bitmap file : byte address requested of the SD library:  byte address returned : 192 data bytes

Found a fix at a big performance hit:
David suggested putting a myFile.peek(); before each; .  No idea why it works, but it does.  It costs 17ms / 192 bytes read instead of just 7ms/ 192 bytes, but it works.
10  Using Arduino / Storage / Re: Using WaveShare's Micro SD Storage Board on: September 27, 2012, 01:42:24 am
I'm using that board right now...

I've got it plugged into the 3v3 line on my mega, works a treat.  They don't recommend it but the resistor divider built in seems to work.

on the pic: 

Power and Ground are 3.3v and gnd; rest of the data lines are SPI connections as regular.
*(I should mention make sure to read the SPI side of the board (back silkscreen) not the SDIO (front side) for connection names)

Edit 6: After reading the sparkfun site comments, some people were having problems because 10k was too high for some sensors; try 1k in series with mosi and CLK...I probably should too.  Not too worried about my 64MB microsd though.
11  Using Arduino / Storage / Re: Can't send data to SD card on: September 26, 2012, 08:03:28 pm
You have a whole lot of stuff in the 'action' section of printDate();

myFile.print blablabla   <<that all looks good

at the end of every time you want to actually write all your data to the card you need to either

myFile.flush();   //writes the current buffer to the card leaving the file open
myFile.close();  //write the current buffer and close the file

If you just print, you are printing to the buffer data, and if you don't close the transaction, it will never be written to the card.

12  Using Arduino / Storage / Re: SD Card bitmap read error/bug? on: September 26, 2012, 07:52:48 pm
Newest version attached; now has a the mentioned bug but doesn't crash at least.

Any thoughts on how this can be mitigated?  I've tried everything I can think of!
13  Using Arduino / Storage / Re: SD Card bitmap read error/bug? on: September 23, 2012, 03:54:11 pm
Once again thanks for the help;

My biggest trouble is I'm using the fastSPI library, and that pointer was set up by the library example - it would stand to reason if I want to use the spi output (addressable RGB leds are painfully slow to bitbang) -- I would need the data to be in the format required by the library.  

*Should have posted before:

is the code I can't seem to edit because it has no line breaks.  I wouldn't know where it begins or ends - it does seem well commented though.

EDIT: So, I got it working, sort of with the existing code, and I remember after I changed it now why it was the way it was;

I'm using SPI to read the card.
I'm using SPI to shift the data out (for speed).  I need to close the file before calling the shiftout function or it crashes.  It could also be my shiftout library that just plain crashes with other stuff on the SPI bus also.  Guess I'm stuck not using the SPI shiftout code or I'm hooped.  Having to close the file every iteration definitely slows things down.  Right now I can read the 192 bytes I need into the array (pointer) in about 6 ms, reliably.  shifting out is now the problem.
14  Using Arduino / Storage / Re: SD Card bitmap read error/bug? on: September 23, 2012, 01:30:44 pm
Since I'm opening a potentially huge file, I can only ever load a line or two of it at a time.  >192 bytes goes by fast when you only have 2k, and the SD library uses 1k of it.

Totally agreed, thats something I've needed to learn for a while, but 'struct' doesn't come up often in beginner coding
I'm using Daniel Garcia's 'fastspi' library to output the data to an led strip.  It operates using this pointer and I can't eliminate it.  In the past I've been able to write to leds (what I thought was an array but is actually a pointer)...procedurally for days on end, one byte at a time.  the .h and .cpp files are unreadable as its just one block of code (no line endings).

Problem is, this is to load single lines into a persistence of vision led strip, and the SPI bus is used to keep things speedy.  FASTSPI library defines the pointer, not the code I'm using, and is used in the background.

1)Would you recommend I create another actual array to load the data into from the sd card then copy it over/move the pointer somehow to that memory?
2) I can't seem to open the file either globally or in one function and have it available from another function, hence it needs to be opened every call of the for loop.  Is there a good way around this? (nest the code in the main loop so it's parented?)
edit: 3) I get the same result whether I file.close or not (you're right, I should be closing it each time).
15  Using Arduino / Storage / Re: SD Card bitmap read error/bug? on: September 23, 2012, 03:06:11 am
Sorry for double post, previous post full.

If I hook this up with my mega1280, it works 'more' -- it loads the entire file twice before failing (same problem, 'random' pointer for file.position)  After about 2.1 runs of the file it crashes entirely.

Makes me think more and more there is a memory leak and it's only manifesting because I'm loading so much at once
Pages: [1] 2 3