Show Posts
Pages: 1 [2] 3 4 ... 15
16  Using Arduino / Programming Questions / Re: Here's a very simple way of randomly seeding rand() on: August 21, 2011, 09:57:01 pm
I don't have a working Arduino at the moment, so please forgive/correct any errors - But would this be a good idea:

Code:

random_number = 0;

for(char i = 0; i < 8; i++) {
  random_number |= ((analogRead(0) & 1) << i);
}


Take eight samples and see if they are odd or even.

Note that this doesn't take any data types into account, and assumes that that analogue's "pin 0" is not grounded.
17  Using Arduino / Programming Questions / Re: Console printf? on: August 21, 2011, 09:30:15 pm
Is this the sort of thing that you are after?

http://www.sparkfun.com/products/10054

18  Forum 2005-2010 (read only) / Bugs & Suggestions / Example code on site on: July 27, 2010, 11:18:09 pm
There seems to be a problem with the example code on the arduino website.

19  Forum 2005-2010 (read only) / Syntax & Programs / Re: matlab arduino serial on: August 20, 2010, 07:09:11 am
Try this basic sending of a string from the MATLAB program.

http://www.arduino.cc/playground/Interfacing/Matlab
20  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help With I2C gyro  ITG 3200 on: July 14, 2010, 04:54:23 am
Was that just by changing the voltage to 3V3?...

How did you make it work?
21  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help With I2C gyro  ITG 3200 on: July 13, 2010, 01:17:12 am
wire.requestFrom(uint8_t address, uint8_t quantity)

returns a value called "read" (uint8_t). //as seen in library...

This is the amount of data read and might be useful to you.

Also, I noticed that there is no overloaded event for the address being an "unsigned char", you may want it to be changed to uint8_t (just to be sure that the compiler isn't doing funny things).

Have you tried the example code?

Someone on the "comments" section said that they had to use address 0x69.  Have you tried that?
22  Forum 2005-2010 (read only) / Syntax & Programs / Re: SD card shield <FAT.h> library help on: August 15, 2010, 10:28:13 pm
I have been spending a lot of time lately going through the FAT.h library, so I'm glad to help someone.
23  Forum 2005-2010 (read only) / Syntax & Programs / Re: SD card shield <FAT.h> library help on: August 14, 2010, 08:50:29 pm
The problem with this code is that the info for it is in the actual header file.  You can open the header file with WordPad, and go to the function that you want to look at by searching using ctrl+f.

To enter a new line, just put \n in your [edit]code string[/edit].  It doesn't print in Notepad, but it works in WordPad.

It is also a problem for people who don't have a lot of coding experience, as it isn't an object based class (which people who have solely used the Arduino are comfortable with)

(I know how you feel about posting code, PaulS, but this is quite a problem)

I found problems with the examples, and have came up with my own subroutines.

I was having trouble creating a few new files at once, so I added this to my create file:
Code:
void create_new_file(char *file_save_name) {

  if(!fat_create_file(dd, file_save_name, &dir_entry)){
    fat_delete_file(fs, &dir_entry);
    fat_create_file(dd, file_save_name, &dir_entry);
  }

  sd_raw_sync();

}

This is a subroutine that writes what is in buffer to your file.  I was having trouble creating a default value to _offset, so I just decided to keep it as an entry and write 0 there when I don't want to offset where I am writing to (you may want to overwrite the last 2 characters: To do this, just change _offset to -2).
Code:
void AddBufferToFile(char *FileWritingTo, int32_t _offset){

  int32_t OffSet = _offset;

  //Open the file at the beginning of the file
  file_handle = open_file_in_dir(fs, dd, FileWritingTo);

  if(file_handle)
  {

    //Now go to the end of the file to write some data.
    if(fat_seek_file(file_handle, &OffSet, FAT_SEEK_END))
    {

      //Write the new 'buffer' string to the end of the file
      fat_write_file(file_handle, (const uint8_t*)buffer, strlen(buffer));

      //sychronise SD (Required)
      sd_raw_sync();
    }

    //Close the file
    fat_close_file(file_handle);
  }

}

This next on gets info from a file (name is in a variable "char file_name[30];"

Note that you can change file_name to an entry to the function.  However, I do not change the file that I am writing to, so I was just saving some code by taking the name from the same place each time.

Code:
int AddFileToBuffer(int32_t OffSet){
  int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card.
  //Open the file      
  file_handle = open_file_in_dir(fs, dd, file_name);
  //Read up to 512 bytes from the file

    if(file_handle) {

    fat_seek_file(file_handle, &OffSet, FAT_SEEK_SET);

    bytes_read = fat_read_file(file_handle, (uint8_t*)buffer, BUFFERSIZE);

    //Close the file before moving on to the next one.
    fat_close_file(file_handle);

    return(bytes_read);
  }
}

Here is an example on how to use it.  It once again uses the variable file_name as a file destination, and an input file called "_xml_file_name[]".  The idea of this function is to save data in a separate file, and at the end of recording data, all the different records are put together in an XML format.
Code:
void printDataFromFile(char _xml_file_name[]) {
  int bytes_read = 0;
  int32_t OffSet = 0;

  do {
    OffSet += (int32_t)bytes_read;
    bytes_read = AddFileToBuffer(OffSet);
    buffer[bytes_read] = '\0';
    AddBufferToFile(_xml_file_name,0);
  
  }
  while(bytes_read == BUFFERSIZE);

  strcpy_P(buffer, (char*)pgm_read_word(&(string_table[13])));
  bytes_read > 2? OffSet = -2: OffSet = 0;

  AddBufferToFile(_xml_file_name,OffSet);

}

You need to be careful that you don't omit the '\0' (EOT) character to the string you are entering.  

If you have any questions, please feel free to send me a message

smiley
24  Forum 2005-2010 (read only) / Syntax & Programs / Re: Setting up registers on: October 27, 2010, 12:40:18 am
(I don't remember why) but I was told to use timer 2 instead. (Maybe someone can refresh my memory as to what Arduino function/s use timers 0 and 1)

And to avoid errors there is a way you should manipulate bits.  I'll give you a sample of setting up timer 2:

Code:
void SetupTimerISR() {
  // Disable interrupts while setting registers
  cli();

  // Reset control registers
  TCCR2A = 0;
  TCCR2B = 0;

  // Clear Timer on Compare Match (CTC) Mode
  TCCR2A |= (1 << WGM21);

  // Prescaler x1
  TCCR2B |= (1 << CS20);

  // Interrupt every 160/16e6 = 10 usec
  OCR2A = 159;

  // Use system clock for Timer/Counter2
  ASSR &= ~(1 << AS2);

  // Reset Timer/Counter2 Interrupt Mask Register
  TIMSK2 = 0;

  // Enable Output Compare Match A Interrupt
  TIMSK2 |= (1 << OCIE2A);

  // Enable interrupts once registers have been update
  sei();
}

// This ISR is run when Timer/Counter2 reaches OCR2A
ISR(TIMER2_COMPA_vect)
{
  int_count++;
}


I hope that this helps
smiley
25  Forum 2005-2010 (read only) / Syntax & Programs / Re: help with serial and counter on: October 27, 2010, 01:23:15 am
I think that Groove could be on to something....
26  Forum 2005-2010 (read only) / Syntax & Programs / Re: Can't get class-version working on: October 27, 2010, 06:26:21 am
You may want to look at this one:
http://www.arduino.cc/en/Hacking/LibraryTutorial

Because there are a few things wrong with your code, and I believe that you just need to look over the tutorial again.

smiley
27  Forum 2005-2010 (read only) / Syntax & Programs / Re: Serial.read() array? on: October 27, 2010, 01:05:27 am
If the program can not run without the serial input, it needs to be stopped and wait for input.

This can be done by using a 'while' statement.  

i.e.
while(!serial.available) { /* Do Nothing... */ }

And something that I have started to do is put incoming strings in talking marks ("string1" ).  That way my program knows when to start recording values and stop recording values.

I hope this helps
smiley
28  Forum 2005-2010 (read only) / Syntax & Programs / Re: Reset Clock on: August 12, 2010, 07:12:38 am
Short answer "no".

However, you can make it seem like it has been reset to your code.

You basically use an offset (as suggested by the other posts).

Take a look at "stopwatch" and I'm sure that you will get a few ideas

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1280262760
<--! Please pardon the cross post -->
29  Forum 2005-2010 (read only) / Syntax & Programs / Re: My PID Line follwing code. What do you think :) on: August 10, 2010, 07:23:06 am
Hey there;

I was in a "Sumo Robot" competition the other year for uni, and one think that did get me by surprise was the false reading on the line sensors.

Basically, a little bit of electronic noise, or a non clean surface triggered a routine (stop, turn away from line, and then drive forward).

The solution to this is to not take the first analogue reading as fact.  I used a variable that started at 0; every time a positive reading was sensed the variable incremented, and decremented on a negative reading.  I any time the variable was above 5, I deemed the sensor to be reading a positive detection.

This technique worked so well, that the robot kept in a ring of white masking tape on brown and white carpet without detecting errors.

I don't know if this helps you, but it's definitely worth considering.

[edit]I forgot to mention that the variable wasn't incremented when it got to 10, and wasn't decremented when it got to 0[/edit]
30  Forum 2005-2010 (read only) / Syntax & Programs / Re: Led Fading sketch crashes after a few loops on: September 07, 2010, 08:37:41 pm
Probably not causing the problem, but isn't "patterns" a 3D array? It has been declared as a 2D array.  
Pages: 1 [2] 3 4 ... 15