Show Posts
Pages: [1]
1  Using Arduino / Sensors / MMA7361 Accelerometer Library on: July 28, 2011, 01:36:10 pm
I have created a library for the MMA7361 library.There already was a library for the chip, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291717903/all, but it was missing the ability to select a 1.5g range and the ability to choose between 3.3V and 5V.

Those selections can be made with my library and it can output the force on an axis in gs, raw A/D values, and A/D values centered on 0.

When preforming an analog read, the axis will be sampled a set number of times and averaged over that period. The number of points is user modifiable.

This library does not make use of the sleep, 0g, g-select, or select test pins.

The library is hosted on bitbucket and can be found at https://bitbucket.org/dcm684/mma7361/src.

Any suggestions for improving the library are appreciated.
2  Using Arduino / Programming Questions / Re: Help with project on: July 27, 2011, 07:29:30 am
Here's one option for you. I am sure that there are more graceful and efficient ways to do it, but here is an example just using if else blocks. What you'll first need to do is make a list of what numbers use what LED segments. My code will assume that A is at the 12 o'clock position and the segments proceed clockwise around the center. G is the center bar.

Code:
void updateSevenSeg(unsinged int inNum)
{
    //A - Segment
    //Turn on A-segment for all numbers that use it
    if ((inNum == 2) || (inNum == 3) || (inNum == 5) || (inNum == 6) || (inNum == 7) || (inNum == 8) || (inNum == 9) {
        digitalWrite(A, HIGH);
    } else {
        //Turn off A-segement for all that don't
        digitalWrite(A, LOW);
    }
   
    //B - Segment
    //Turn on B-segment for all numbers that use it
    if ((inNum == 1) || (inNum == 2) || (inNum == 3) || (inNum == 4) || (inNum == 7) || (inNum == 8) || (inNum == 9) {
        digitalWrite(B, HIGH);
    } else {
        //Turn off B-segement for all that don't
        digitalWrite(B, LOW);
    }

    //C - Segment
    //Turn on C-segment for all numbers that use it
    if ((inNum == 1) || (inNum == 3) || (inNum == 4) || (inNum == 5) || (inNum == 6) || (inNum == 7) || (inNum == 8) || (inNum == 9) {
        digitalWrite(C, HIGH);
    } else {
        //Turn off C-segement for all that don't
        digitalWrite(C, LOW);
    }

//Repeat the same process for the remaining 4-segments
}

Looking at what I just wrote, I would actually write my conditionals like so:
Code:
//A-Segment
if ((inNum ==1) || (inNum ==4) {
    digitalWrite(A, LOW);
} else {
    digitalWrite(A, HIGH);
}
I would write them like that because the majority of the numbers use that segment. With fewer parts in the conditional, debugging is easier, execution of the program is faster, and the program size is reduced.
3  Using Arduino / Programming Questions / Re: Time loop does not stop on: July 27, 2011, 07:07:17 am
While you cannot get the program to stop executing, you can keep it from doing anything else until it is manually reset. To do this you would need the following code:
Code:
while(1){}
The while loop will run until infinity and do absolutely nothing except keep the loop routine from looping any more.
4  Using Arduino / Programming Questions / Re: Preprocessor processes defines unexpectedly on: July 26, 2011, 08:36:54 pm
Quote
Code:
#if G_VAL < GS_90
The processor directives are not really designed for mathematics.
The goal with the comparison was to see what value was assigned to G_VAL. Would it be better to replace the above code with #ifndef GS_90 and just use #define GS_90 in place of #define GS_VAL GS_90?

Quote
Quote
If I remove the #defines from the first part when G_VAL == GS_90, the compiler throws a couple of errors stating that G_CONVERSION is not in the scope. There is no error with MODE.
But, it's probably not your value of MODE that is in scope.
Actually that was what baffled me, the value assigned to MODE was in fact 39, the correct value. G_CONVERSION was 0, the wrong value.

Quote
Quote
When I move the #define to a pde file, prior to the #inlcude<foo.h> line, I have problems
The Arduino IDE does some stuff to your code prior to invoking the preprocessor. Try putting a dummy include statement before your #defines. The file to be included must actually exist, but not have to contribute anything useful to the code.
I created a file , dummy.h, and placed #include <dummy.h> at the top of the pde file. It did not fix the problem.

Thanks for your thoughts.

I think I might use constants in place of the #defines and allow the program to change the mode at run time. That said if there are any other ideas as to why I got the behavior that I did, I am still very interested.
5  Using Arduino / Programming Questions / Re: Elementary Arduino Question on: July 26, 2011, 02:58:22 pm
There needs to be a delay after the led turns off. It is turning off, but for a very, very short period, somewhere on the order of microseconds, too fast to see.
6  Using Arduino / Programming Questions / Preprocessor processes defines unexpectedly on: July 26, 2011, 02:47:03 pm
I am in the process of writing a library and I am not getting the results that I am expecting when from some preprocessing commands.

Below is the block of preprocessor code in my header file, foo.h:
Code:
#if G_VAL < GS_90
#define G_CONVERSION 0
#define MODE 31

#else
#define G_CONVERSION 50
#define MODE 39
#endif

Relevant PDE source:
Code:
//#define G_VAL GS_15
#define G_VAL GS_90

#include <MMA7361.h>

If I define G_VAL in foo.h, I have no issue. When I move the #define to a pde file, prior to the #inlcude<foo.h> line, I have problems. MODE which is only used in the pde file is properly set, but G_CONVERSION which is used in foo.cpp is not set correctly. They are always set to 1 and 0 respectively regardless of the value assigned to G_VAL. If I remove the #defines from the first part when G_VAL == GS_90, the compiler throws a couple of errors stating that G_CONVERSION is not in the scope. There is no error with MODE.

Are the preproccesor commands in the pde file processed after foo.cpp and foo.h are compiled regardless of the fact that the include is after those commands? Or, am is there something that I should/can fix?

Thanks
7  Forum 2005-2010 (read only) / Troubleshooting / Problem Getting Program to Print Text on: July 30, 2009, 10:23:23 pm
I am trying to clock data from an array out to three SIPO chips.  I've had some problems getting things to come out right so I thought I should debug the output of the various functions.  Unfortunately this has led me to some strange problems.  For example when I leave the function "writeData" in the code, nothing in the entire program prints even though that particular function is never called, and when it was called it was called after some text should have printed.  When I remove that the "writeData" function the program progresses a little more and prints some text, but the program still freezes after displaying "beginWriteBoard."

I am relatively proficient in writing C code and cannot for the life of me figure what is causing these two problems.  The only thing that I could think of is that the RAM is being filled up as when I remove print strings things sometimes work, but that does not seem reasonable since I am only using a small amount of it.  Any and all help is appreciated.

Below is my code:
Quote

/*
* SIPO_LedBoard
*
*/
 
//#define SER_DEBUG //Print debug text when reading from serial
 
#define SIPO1_OFF  0b0000000
#define SIPO2_OFF  0b0001110
#define SIPO3_OFF  0b1111111
#define TOGGLE_DELAY 1       // us to delay in toggle
#define DISPLAY_LOOPS 5
#define MAX_LENGTH 34
#define INCOMING_DATA_CHAR 's'

int SIPO1 = 7;                // Serial Input for SIPO 1
int SIPO2 = 6;                // Serial Input for SIPO 2
int SIPO3 = 5;                // Serial Input for SIPO 3
int STROBE = 4;
int OE = 3;
int CLOCK = 2;                // Clock Pin
int LED_PIN = 13;            //Pin that on board LED is attached

int playbackLength;

int allOnScreen[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

#define DEFAULT_PLAYBACK_LENGTH 1

int outputArray[MAX_LENGTH][10] = {
{ 0x3FF, 0x3FF, 0x3FF, 0x3FF, 0x3FF, 0x3FF, 0x3FF, 0x3FF, 0x3FF, 0x3FF }
};

int delayArray[MAX_LENGTH] = { 5 };


void setup()                    // run once, when the sketch starts
{
  Serial.begin(57600);
  Serial.print("\r\nINIT");
  
  playbackLength = DEFAULT_PLAYBACK_LENGTH;
  
  pinMode(SIPO1, OUTPUT);      // sets the digital pin as output
  pinMode(SIPO2, OUTPUT);      // sets the digital pin as output
  pinMode(SIPO3, OUTPUT);      // sets the digital pin as output
  pinMode(OE, OUTPUT);      // sets the digital pin as output
  pinMode(STROBE, OUTPUT);      // sets the digital pin as output
  pinMode(CLOCK, OUTPUT);      // sets the digital pin as output
  pinMode(LED_PIN, OUTPUT);
  
  digitalWrite(STROBE, LOW);
  digitalWrite(OE, HIGH);
  digitalWrite(CLOCK, HIGH);
      
  digitalWrite(LED_PIN, LOW);
  
  //writeData(SIPO1_OFF, SIPO2_OFF, SIPO3_OFF);
}

void loop()  // run over and over again
{
  int i,j;
  char c;
  
  Serial.print("\r\nStart");
  for(i = 0; i < playbackLength; i++){
    for(j = 0; j < delayArray; j++){
      writeBoard(outputArray);
    }
  }
  Serial.print("\r\nFinish");
  
}

void writeBoard(int ledArray[10])
{
  int k;
  
  int sipoTwo;
  int sipoOne;
  int sipoThree;
  
  Serial.print("\r\nBegin writeBoard");
  for(k = 0; k < 10; k++) {
   Serial.println(k);
  
   sipoOne = (1 << k);
   sipoThree = ~(ledArray[9-k]);
   sipoTwo = (sipoOne >> 7) | (sipoThree << 3);
   sipoThree = sipoThree >> 3;
    
   //writeData(sipoOne, sipoTwo, sipoThree);
   //delay(1);
   }
   Serial.print("\r\nEnd writeBoard");

}

#ifdef exclude
void writeData(int sipoOneIn, int sipoTwoIn, int sipoThreeIn)
{
  int m;
  
  Serial.print("\r\nBegin writeData");


  Serial.print("\r\nEnd writeData");
}
#endif

Pages: [1]