Show Posts
Pages: [1]
1  Using Arduino / Sensors / Re: Using an MMA7361 to measure G forces on: January 27, 2013, 01:16:12 pm
After 53 views no one knows the answer? :-(

Maybe I'm using the wrong sensor, and in that case, could someone point out the right direction?

Thanks!
2  Using Arduino / Sensors / Using an MMA7361 to measure G forces on: January 20, 2013, 09:35:56 am
Hello,

I know that there are dozens of threads related to this accelerometer but all of the responses that I see  talk about angles and inclinations.

I'm trying to measure the G forces in a car.  It doesn't matter which direction, I just want to measure the maximum G forces applied for that instant.

I've tried mutiple snippets of code but nonw of them worked as expected.

My last attempt was made using this library:

http://code.google.com/p/mma7361-library/downloads/detail?name=AcceleroMMA7361_v0.8b.zip

But still couldn't get somewhere. I tried to use a function called getTotalVector () and use the difference between the actual and previous call to calculate the actual G forces but I don0t think it is working as expected :-(

This is how the accelerometer is connected to the arduino:

X -> A0
Y -> A1
z -> A2
GND -> GND
3.3 -> 3.3
SL -> 3.3

G0 is not used and so is the Sensitivity selector ( LOW = 1.5G ).

And this is the piece of code I'm trying to use (ignore some of the pins I'm passing in the begin function because, just as I told you before, I'm not using them even though I need to point them to initialize the library):
Code:
#include <AcceleroMMA7361.h>

AcceleroMMA7361 accelero;
int x;
int y;
int z;
double G;
int current=0;
void setup()
{
  Serial.begin(9600);
  accelero.begin(13, 12, 11, 10, A0, A1, A2);
  accelero.setARefVoltage(3.3);                   //sets the AREF voltage to 3.3V
  accelero.setSensitivity(HIGH);                   //sets the sensitivity to +/-6G
  accelero.setAveraging(100);
  accelero.calibrate();
}

void loop()
{
//  g = [mV/analogRead unit] * ([units] - ([Aref/2]) / [mvPerG]
 
 current = abs(current - accelero.getTotalVector());
 
 G = 3.22 * ((current - 3.3 / 2)  / 2450);
 

  x = accelero.getXAccel();
  y = accelero.getYAccel();
  z = accelero.getZAccel();
 
  Serial.print("\nx: ");
  Serial.print(x);
  Serial.print(" \ty: ");
  Serial.print(y);
  Serial.print(" \tz: ");
  Serial.print(z);
  Serial.print("\tG*10^-2");
  Serial.print( " -> " );
  Serial.print( G );
 
  delay(500);                                     //make it readable
}

The G formula was copied from another post in this forum and it is giving me the following results:

Quote

Calibrating MMA7361011..................................................
DONE
x: 0    y: -8    z: 101   G*10^-2 -> 0.13
x: 0    y: -5    z: 97   G*10^-2 -> 0.01
x: 0    y: -6    z: 100   G*10^-2 -> 0.11
x: 0    y: -4    z: 97   G*10^-2 -> 0.00
x: 1    y: -6    z: 100   G*10^-2 -> 0.12
x: 0    y: -4    z: 97   G*10^-2 -> -0.00
x: 1    y: -8    z: 99   G*10^-2 -> 0.12
x: -1    y: -5    z: 96   G*10^-2 -> 0.00
x: 1    y: -9    z: 99   G*10^-2 -> 0.12
x: -3    y: -7    z: 98   G*10^-2

Then I flip the accelerometer, put it on a table without any movement and the last column gives-me high values:

Quote
x: 115    y: 592    z: 5   G*10^-2 -> 0.34
x: 118    y: 600    z: 10   G*10^-2 -> 0.46
x: 107    y: 601    z: 7   G*10^-2 -> 0.34
x: 103    y: 604    z: 8   G*10^-2 -> 0.46
x: 103    y: 606    z: 7   G*10^-2 -> 0.34
x: 104    y: 609    z: 11   G*10^-2 -> 0.47
x: 105    y: 609    z: 8   G*10^-2 -> 0.34
x: 105    y: 607    z: 10   G*10^-2 -> 0.47
x: 98    y: 610    z: 8   G*10^-2 -> 0.33
x: 105    y: 615    z: 8   G*10^-2 -> 0.48
x: 95    y: 614    z: 9   G*10^-2 -> 0.33
x: 96    y: 613    z: 8   G*10^-2 -> 0.47

Obviously I'm not calculating this correctly, the question is, how can I do it?
3  Using Arduino / Displays / Re: LCD lagging after SD.begin() on: January 18, 2013, 06:26:31 pm
After searching a bit more I found out the reason why this happens (Thanks afasias!):

This is due to some SPI Control and SPI Status problems (registers are changed and they are not reverted after using the SD begin). You can read more here: http://arduino.cc/forum/index.php/topic,122207.0.html

Thanks!
4  Using Arduino / Displays / LCD lagging after SD.begin() on: January 18, 2013, 04:44:30 pm
Hello,

I'm facing some problems related to the responsiveness of an LCD when it is used with an SD Card reader at the same time. They both use SPI to communicate and the LCD is quite fast until I start using the SD Card.

I did a small benchmark to test the difference and it is quite big!  smiley-cry

Code:
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <SD.h>

Adafruit_ST7735 tft = Adafruit_ST7735(10, 9, 8);
int timelapse;
void setup()
{
  Serial.begin(9600);
  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
}

void loop()
{
  Serial.println( "Press any key to start..." );
  while (!Serial.available()>0 ) {}
  Serial.read();
 
  Serial.print( "Time spent to print 90 interations to screen: " );
  timelapse = millis();
  test();
  Serial.print( (millis() - timelapse) );
  Serial.println( "ms." );
 
  Serial.print( "Time spent to print 90 interations to screen (after SD.begin()): " );
  SD.begin(4);
  timelapse = millis();
  test();
  Serial.print( (millis() - timelapse) );
  Serial.println( "ms." ); 
}

void test()
{
  tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
  tft.setTextSize(5);
  for ( int i = 10; i < 100; i++ )
  {
    tft.setCursor(40, 50);
    tft.print( i );
  }
}

And these were the results:
Code:
Press any key to start...
Time spent to print 90 interations to screen: 1929ms.
Time spent to print 90 interations to screen (after SD.begin()): 34613ms.

I need to be able to show data on the screen without lag while saving a few bytes (about 100) every 5 seconds. Any ideas? Thanks!
5  Using Arduino / Displays / Re: SOLVED - unable to make it work 1.8 SPI TFT 128x160 on: January 18, 2013, 07:31:55 am
Thanks for the replies but I've found out that the problem was due to the stickduino. I switched to an ardunino nano v3 and everything is working perfectly but now  I have another problem.

The LCD works great and so the SD card reader. But when I try to use them at the same time the LCD becomes extremely slow.  smiley-sad

Pleae take a look at this sample of code:

Code:
#define cs_lcd   10
#define cs_sd    7
#define dc       9
#define rst      8

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs_lcd, dc, rst);
File myFile;

void setup()
{
  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
  tft.setTextWrap(false);
}


void loop()
{
  tft.setTextSize(5);
  tft.setTextColor(ST7735_RED, ST7735_BLACK);
  for ( int i = 0; i < 100; i++ )
  {
    tft.setCursor(40, 50);
    tft.print( i );
    delay( 100 );
  }
  
}

This sample of code works perfectly without any problem. But if I add a simple function to open and write data to a file it will start lagging severely:


Code:
#define cs_lcd   10
#define cs_sd    7
#define dc       9
#define rst      8

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>

Adafruit_ST7735 tft = Adafruit_ST7735(cs_lcd, dc, rst);
File myFile;

void setup()
{
  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
  tft.setTextWrap(false);
}


void loop()
{
  tft.setTextSize(5);
  tft.setTextColor(ST7735_RED, ST7735_BLACK);
  for ( int i = 0; i < 100; i++ )
  {
    tft.setCursor(40, 50);
    tft.print( i );
    delay( 100 );
  }
  writeCard();
  
}

void writeCard()
{
  if (!SD.begin(cs_sd)) {
    //Error accessing SD CARD.
    return;
  }
  
  myFile = SD.open("tmp.txt", FILE_WRITE);
   if (myFile) {
    //WRITING TO FILE
    myFile.println("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
    // close the file:
    myFile.close();
  }  
}

I though that this could have something to do with the SS, after all I'm enabling the SD pin without disabling the LCD one but still no good.  smiley-sad-blue

Code:
void writeCard()
{
  //TRY TO DISABLE THE LCD SS PIN
  digitalWrite( cs_lcd, HIGH );
  
  if (!SD.begin(cs_sd)) {
    //Error accessing SD CARD. Re-enable LCD SS pin, force the SD SS pin to be disabled and exit.
    digitalWrite( cs_lcd, LOW );
    digitalWrite( cs_sd, HIGH );
    return;
  }
  
  myFile = SD.open("tmp.txt", FILE_WRITE);
   if (myFile) {
    //WRITING TO FILE
    myFile.println("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
    // close the file:
    myFile.close();
  }
  
  //Writing finished. Lets re-enable LCD SS pin and disable the SD SS pin.
  digitalWrite( cs_lcd, LOW );  
  digitalWrite( cs_sd, HIGH );
}


I would really love not to have to hack (aka mess with)  the Sd.h but right now I feel like I've hit a wall.

Any ideas?  smiley-sad
6  Using Arduino / Displays / Re: SOLVED - unable to make it work 1.8 SPI TFT 128x160 on: January 15, 2013, 10:23:45 am
That configuration is working perfectly to handle the TFT. What about the card reader?  smiley-razz
If using the card reader then you need to connect it to SPI. MOSI, MISO and SCK to respective lines. I used D4 for the SD Card CS. If you connect the TFT to hardware SPI too (much faster update) then what I did was connect the SD card through to hardware SPI (11, 12, 13 on UNO) and then connect the TFT SDA to the SD MOSI, TFT SCL to SD SCK, then TFT CD & AO to your defined pins.

I'll try and find time to put a more comprehensive example together of how to connect it all shortly.

Please do it because I'm having alot of trouble to get it to work.

Code:
#include <SD.h>
File myFile;
int cspin = 10;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
 
  if (!SD.begin(cspin)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("Demotext.");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }
   Serial.println( "Closed!");
}
 

void loop()
{
//NOP
}


I can't make this simple piece of code to work.  The LCD is disconnected but I can perfectly use it. This is how it is wired to the arduino:

SD Reader:
SCK -> 13
MISO -> 12
MOSI -> 11
CS -> 10

I'm using a stickduino with an atmega168. The memory card is a generic one with 1GB formatted with FAT. I'm using both power connections (5v to the LCD and the 3.3v of the backlight).
7  Using Arduino / Displays / Re: SOLVED - unable to make it work 1.8 SPI TFT 128x160 on: January 10, 2013, 03:11:13 pm
That configuration is working perfectly to handle the TFT. What about the card reader?  smiley-razz
8  Using Arduino / Programming Questions / Re: Read String from Serial Input, parse it, and write it to the EEPROM. on: November 28, 2012, 11:02:16 am
Quote
I've changed all my code to use char arrays instead of strings but I'm still struggling to parse and store data inside the EEPROM.

I couldn't get any of the posted stuff to work either. Below is simple code to captrure a string. It uses the String class thing, but it actually works (at least for me).

Code:
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}


Thanks for your response, but my problem wasn't related to reading strings from serial input but parsing them after reading smiley
9  Using Arduino / Programming Questions / Re: Read String from Serial Input, parse it, and write it to the EEPROM. on: November 28, 2012, 11:00:50 am
Quote
Everytime the strncpy is executed my arduino is resetedand I have no idea why
Nor do I, since the code you posted doesn't call strncpy(). The usual culprit, though, is copying outside the bounds of the destination array. Copy 12 bytes into a 2 element array is not a good thing. Nor is copying to a location pointed to be a non-initialized pointer.

The comma variable should be named delimterCounter, so that it doesn't matter what the delimiter is.

Thanks for the tip related to the variable name. You are right and it is much more clearer that way smiley-grin

But the strncpy is in fact called. If you look closely you will see that it is the first thing called after I check if the minimum number of colons is present.

Anyway thanks for the help. I was using wrong index values and now everything seems to work perfectly smiley

But now I have another question:

I have my code protected agains buffer overrun, but how can I validate my code against invalid input?

i.e:
 
Lets imagine that the user pass the following argument:

Quote
1:2:3:1:1:a:!:9

How can I parse the content and be sure I will only read numbers (preferably between 0 and 255)?

What I'm currently doing is using the atoi() function to parse the string, and everytime atoi() returns 0 I compare this value to the original string just to make sure that the "zero" wasn't actually the real value being passed (due to the fact that atoi() returns 0 everytime it wasn't able to parse the string into an int and this may be confused the a real zero).

Is this the most effective way of doing it?
10  Using Arduino / Programming Questions / Re: Read String from Serial Input, parse it, and write it to the EEPROM. on: November 27, 2012, 06:52:53 pm
Quote
  for(int i=0; i < strlen(myCommand); i++)
  {
    if( myCommand == ':' ) comma++;
  }
This is a comma --> ,
This is a colon --> :

See the difference?

Code:
    EEPROM.write(counter, int(tmpBuff));
The int() macro is for people that think (int)tmpBuff looks weird. Personally, I'd have told those people to get over it. The int() macro does NOT convert a char array to an int. The atoi() function does that. All that it does is cast the address of the token to an int, which is hardly the data that you want to be storing in the EEPROM.

Hellom PaulS, thank you for your reply smiley

About the first part, please bear with me. I was actually thinking in colon ( as you can see, I'm using commas in the hardcoded command and also when I'm searching for it) but switched the names (I'm Portuguese).  smiley-wink

I've also corrected the problem related to the cast but that still doesn't fixe the original issue.

Everytime the strncpy is executed my arduino is resetedand I have no idea why smiley-sad
11  Using Arduino / Programming Questions / Re: Read String from Serial Input, parse it, and write it to the EEPROM. on: November 27, 2012, 06:12:42 pm
Hello guys,

I've changed all my code to use char arrays instead of strings but I'm still struggling to parse and store data inside the EEPROM.

This is my current code:

Code:
void writeConfig()
{

  char *myCommand = "saveConfig 2:2:2:2:2:2:2:2";
  char *tmpBuff;
  char configuration[64];
  int comma=0;
 
 
  for(int i=0; i < strlen(myCommand); i++)
  {
    if( myCommand[i] == ':' ) comma++;
  }

  if ( (comma < 7)  || (strlen(myCommand) < 26) )
  {
    Serial.println("Invalid configuration.");
    return;
  }
   
  strncpy(configuration, (myCommand+11), sizeof(myCommand) - 11);
 
  tmpBuff = strtok(configuration, ":");
    int counter=0;
    while (tmpBuff != NULL)
  {
    EEPROM.write(counter, int(tmpBuff));
    tmpBuff = strtok (NULL, ":");
    counter++;
  }
 
 
  Serial.println("Configuration saved!");
}

Please ignore the lines below "strncpy(configuration, (myCommand+11), sizeof(myCommand) - 11);" because they werent tested yet. This line is crashing the arduino and it is reseting everytime it is executed and I don't know why!

All i'm trying to do is:

1st - Receive a string (I've hardcoded it for testing purposes -> char *myCommand = "saveConfig 2:2:2:2:2:2:2:2"smiley-wink
2nd - Cut the head leaving just the configuration argument.
3rd - Split the configuration argument using the commas as delimiter
4th - Save each of those numbers in the EEPROM. Any of them will always be a number ranging from 0 to 255

How can i accomplish this?
12  Using Arduino / Programming Questions / Re: Read String from Serial Input, parse it, and write it to the EEPROM. on: November 26, 2012, 05:38:03 pm
Hello,

I'm trying to do something that should be very simple but it seems much more complicated. I need to receive a String like this:

12:22:-1:30:123:112

This string should then be splitted by the colons and each byte stored in a specific position of the EEPROM:

Please take a look at the following code:

Code:
  byte tmpBuff[6];
  int pos=0;
  int previousStartConfig=0;
  String configuration = command.substring(9);
  for (int i=0; i < configuration.length(); i++)
  {
    if ( configuration.charAt(i) == ':' )
    {
        tmpBuff[pos] = tmpBuff, configuration.substring(previousStartConfig, i);
        previousStartConfig = i+1;
        pos++;
    }
  }
 
  if ( pos < 6 )
  {
    Serial.println("Bad configuration!");
    return;
  } 

Now, I know that this piece of code doesn't work and I know that the problem is due to the conversion from the string to byte. But how can I fi this?

PS: Feel free to improve this piece of code because I know it sucks (full of variables ).  smiley-lol

Does the string have any end of packet marker? New line? carriage return?

Get rid of the String object and use null-terminated char arrays. A finite state machine should be used to keep track of where in the string you are.

Hi Arrch,

Thank you for your reply.

The String ends with a carriage return but this is not passed to my function. Let me show you how I'm parsing the commands:
This is the code that I have in the loop() subroutine:
Code:
if ( Serial.available() > 0 )
 {
      c = Serial.read();
      if (c==13)
        processCommand();
      else
        command.concat(c);
   
   Serial.print(c); //echo purposes
 }

I keep appending chars to a global variable called command and when I receive a CR I process the command. I didn't used char arrays because I find them harder to handle but I'm willing to give it a try. Can you show me some example? Can I use string functions like substring with chars?

Thanks!
13  Using Arduino / Programming Questions / Read String from Serial Input, parse it, and write it to the EEPROM. on: November 26, 2012, 05:24:06 pm
Hello,

I'm trying to do something that should be very simple but it seems much more complicated. I need to receive a String like this:

12:22:-1:30:123:112

This string should then be splitted by the colons and each byte stored in a specific position of the EEPROM:

Please take a look at the following code:

Code:
  byte tmpBuff[6];
  int pos=0;
  int previousStartConfig=0;
  String configuration = command.substring(9);
  for (int i=0; i < configuration.length(); i++)
  {
    if ( configuration.charAt(i) == ':' )
    {
        tmpBuff[pos] = tmpBuff, configuration.substring(previousStartConfig, i);
        previousStartConfig = i+1;
        pos++;
    }
  }
 
  if ( pos < 6 )
  {
    Serial.println("Bad configuration!");
    return;
  } 

Now, I know that this piece of code doesn't work and I know that the problem is due to the conversion from the string to byte. But how can I fi this?

PS: Feel free to improve this piece of code because I know it sucks (full of variables ).  smiley-lol
14  Using Arduino / Project Guidance / Arduino project with a fast and responsive LCD on: October 09, 2012, 12:56:51 pm
Hello,

I'm trying to develop a circuit to read the speed/rpm/acceleration using a GPS module a tachometer and an accelerometer. I've assembled the circuit and everything works as expected except for the LCD. An old 16x2 hd44780 based LCD is not fast enough to show all the details in realtime (like a bar graph when I rev up the engine) so I've been researching to see if I could find a replacement but couldn't find anything.

Does anyone know any screen that don't suffer from extreme lag? I've seen some 126x64 cheack screens on ebay but they all seem to slow (at least on youtube).

Thanks!
15  Using Arduino / Project Guidance / Detect someone in front of a computer using the smallest available sensor. on: May 09, 2012, 05:00:16 am
Hello,

I'm trying to create my first project using a tiny arduino but I still don't know how am I going to accomplish that...

I need to be able to detect a person in front of a computer. PIR is out of the question because I need to be able to detect the person as long as he keeps in front of the computer.  I though of using the Parallax's Ping but it is too big for my project.


So I'm not really sure of how am I going to do this so I'm asking you: What is the smallest sensor that I can use to detect someone in front of a computer? Something with the size of a 10 cents coin (euros) would be nice.

Thanks!
Pages: [1]