Show Posts
Pages: 1 ... 70 71 [72] 73 74 ... 109
1066  Using Arduino / Storage / Logging 100 ksps with the Arduino internal ADC on: July 11, 2012, 02:40:36 pm
I have been experimenting with a fast logger using an Adafruit Data Logging Shield and a sketch to log 8-bit data from an Arduino analog pin. Any SD shield/module could be used. 

I have been able to log data to an SD card at 100,000 samples per second.

If there is any interest in this sketch I will polish it a bit and post it.

Plots, pictures and more info is here http://forums.adafruit.com/viewtopic.php?f=31&t=30557
1067  Development / Other Software Development / Re: what about debugging? on: July 11, 2012, 01:34:19 pm
Here is a recent article on using DebugWIRE with AVR Dragon on an Arduino.   It uses an Eclipse plugin.  AVR Dragon is a programmer/debugger that costs $50.96 from DigiKey.

http://husks.wordpress.com/2012/03/29/hardware-debugging-the-arduino-using-eclipse-and-the-avr-dragon/

You need to modify the Arduino to use DebugWIRE since the cap that does the reset pulse for download interferes with DebugWIRE.  DebugWIRE is a one wire serial protocol at F_CPU/128 using the reset pin.
1068  Development / Other Software Development / Re: what about debugging? on: July 11, 2012, 11:20:53 am
I forgot one comment.  Note the yellow mass of wires above the panel.  That is how the machine back-plane was wired.  Much of the stuff we built back then used wire-wrap instead of PCBs.
1069  Development / Other Software Development / Re: what about debugging? on: July 11, 2012, 09:36:20 am
I am really old and started embedded programming in the 1960s.  We debugged using the front panel switches and lights.  All the registers and some of the processor status was shown.  You could use the switches to enter data into memory or registers.

Single stepping was the last resort.

This machine had eight instructions and the lights on right side decode the instruction.

1070  Development / Other Software Development / Re: ChibiOS/RT 2.4.1 RTOS update on: July 11, 2012, 08:33:43 am
I looked at the problem again and found that dynamic memory in avr-libc will not work with ChibiOS.  Avr-libc assumes there is one stack and the heap is above the stack.  In ChibiOS the workspace for a thread is above the heap so malloc/realloc fails.

To use dynamic memory with ChibiOS you would need to enable the ChibiOS heap manager.

I have not used the ChibiOS driver for AVR, just ARM.  The serial driver is very basic in terms of functionality but is multithread friendly. 

If a thread reads and there is no data or writes and the queue is full, it uses qwait so lower priority threads can run.  This makes programing simple in the thread.

Using chThdYield in your program with Arduino Serial will use round robin scheduling to allow equal priority threads to run if you are waiting for input and will block lower priority threads.  Only higher priority threads will run if you block on output using Arduino Serial.

You would need to add ports three and four to serial_lld.c.
1071  Development / Other Software Development / Re: ChibiOS/RT 2.4.1 RTOS update on: July 10, 2012, 05:56:41 pm
The problem appears to be the Arduino String class. 

I never use the String class or anything that uses malloc/free in my sketches. 

Most coding standards for critical embedded systems forbid use of dynamic memory after system start-up and my background was with this type system.  My libraries do not use dynamic memory.

If you want to use String it appears that you will need to enable the ChibiOS wrapper for dynamic memory and modify the Arduinos String class to be multithreaded.

Note that Serial is the Arduino HardwareSerial driver, not the ChibiOS driver.

This sketch which uses char arrays seems to work.
Code:
#include <ChibiOS.h>
#include <util/atomic.h>

static WORKING_AREA(waSerialHandler,256);
static msg_t SerialHandler(void *arg)
{
  Serial.begin(9600);
  char str[20];
//  String inputStr = "";
//  inputStr.reserve(200);
  volatile uint8_t count = 0;
  char inChar = 0;
  while (true)
  {
    if (!Serial.available()) chThdYield();
    else
    {
      do
      {
        inChar = (char)Serial.read();
        if(inChar=='c')
        {
          str[count] = 0;
          Serial.print("Count:  ");
          Serial.println(count);
          Serial.print("Output string:  ");
          Serial.println(str);
        }
        str[count] = inChar;
  //      inputStr.concat((String)inChar);
        count++;
      }while (Serial.available());
    }
    if (count >= 10)
    {
      str[count] = 0;
      Serial.println(str);
  //  Serial.println(inputStr);
    Serial.flush();
    count=0;
//    inputStr = "";
    }
//    chThdSleep(1);
  }
}
void setup() {
  // put your setup code here, to run once:
  cli();
  halInit();
  chSysInit();

  Serial3.begin(9600);
 
  chThdCreateStatic(waSerialHandler,sizeof(waSerialHandler),NORMALPRIO +100, SerialHandler, NULL);

}

void loop() {
  // put your main code here, to run repeatedly:
  chThdSleep(10);
 
}
1072  Development / Other Software Development / Re: ChibiOS/RT 2.4.1 RTOS update on: July 09, 2012, 08:52:51 am
Nil Rtos examples look interesting.  Many Arduino projects need two or three threads and this should work well with Nil Rtos.

Looks like all development is with ARM CM0 but it should not be too hard to port to AVR and make an Arduino library.

There are many tiny systems like this but Giovanni Di Sirio is a real expert in embedded systems so I expect this will be a very good OS.

Designing a good general RTOS takes a lot of experience and talent and Giovanni has both.  I worked on some the largest embedded systems in physics research for 40 years.  The last system I did design work for was the LHC Atlas experiment at CERN which may have discovered the Higgs Boson.

I think ChibiOS is one of the best free systems available so it will be worth trying Nil Rtos on Arduino.
1073  Using Arduino / Storage / Anyone using SD.h with Leonardo? on: July 05, 2012, 03:30:02 pm
Anyone tried SD.h with Leonardo?

While updating SdFat for Leonardo, I realized that the version of SdFat used in Arduino 1.01 assumes an ATmega32U4 is a Teensy 2.0.

I tried SD.h with a Leonardo and a SD in the Ethernet shield and it failed.

I modified the SD library by editing Sd2Card.h in the SD/utility folder.

I changed this:
Code:
/** The default chip select pin for the SD card is SS. */
uint8_t const  SD_CHIP_SELECT_PIN = SS_PIN;
// The following three pins must not be redefined for hardware SPI.
/** SPI Master Out Slave In pin */
uint8_t const  SPI_MOSI_PIN = MOSI_PIN;
/** SPI Master In Slave Out pin */
uint8_t const  SPI_MISO_PIN = MISO_PIN;
/** SPI Clock pin */
uint8_t const  SPI_SCK_PIN = SCK_PIN;
To this:
Code:
#include <Arduino.h>
/** The default chip select pin for the SD card is SS. */
uint8_t const  SD_CHIP_SELECT_PIN = SS;
// The following three pins must not be redefined for hardware SPI.
/** SPI Master Out Slave In pin */
uint8_t const  SPI_MOSI_PIN = MOSI;
/** SPI Master In Slave Out pin */
uint8_t const  SPI_MISO_PIN = MISO;
/** SPI Clock pin */
uint8_t const  SPI_SCK_PIN = SCK;

That made this version of the ReadWrite example work with Leonardo and a SD in the Ethernet shield.
Code:
/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */
 
#include <SD.h>

File myFile;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // disable Ethernet
   pinMode(10, OUTPUT);
   digitalWrite(10, HIGH);

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
// nothing happens after setup
}
1074  Using Arduino / Storage / Re: Arduino and SD card help on: July 03, 2012, 08:35:36 pm
Using the C++ I/O streams library can be very powerful but most old-timers and hobbyists don't have the knowledge to effectively use this library so maybe sticking with the simpler Arduino SD API will be best for you.

The Arduino API is a simple wrapper for an old version of SdFat that does not expose I/O streams.

The I/O streams library is nice since one line can read text for an entire structure, convert the text to binary, store the result in program variables, and check for errors.

Here is the statement for your data:
Code:
  jessicaFile >> jessica.hours >> jessica.mins >> jessica.secs >> jessica.pillOne >> jessica.pillTwo;
You can test for success of the above read statement like this:
Code:
  if (!jessicaFile) {
    // error occurred
  }


1075  Using Arduino / Storage / Re: Writing binary file to SD (creating BMP files) on: July 03, 2012, 04:52:06 pm
I was lazy so I just hard coded 24.

Each row must be a multiple of four bytes so I think the correct expressions for rowSize and fileSize are

Code:
  int rowSize = 4*((3*w +3)/4);
  int fileSize = 54 + h*rowSize;
This depends on truncation of integer divide.

The number of padding bytes at the end of a row would be:

Code:
  int nPad = rowSize - 3*w;

In your case rowSize is 12 bytes and fileSize is 78 =  (54 + 2*12).

Padding at the end of a row is three bytes.

I have not really tested this very well, just ran this sketch:
Code:
void setup() {
  Serial.begin(9600);
  int h = 2;
  for (int w = 0; w < 10; w++) {
    Serial.print(w);
    Serial.write(' ');
    int rowSize = 4*((3*w +3)/4);
    int fileSize = 54 + h*rowSize;
    int nPad = rowSize - 3*w;
    Serial.print(rowSize);
    Serial.write(' ');
    Serial.print(nPad);
    Serial.write(' ');
    Serial.println(fileSize);
  }
}
void loop() {}

and got this result:
Quote
0 0 0 54
1 4 1 62
2 8 2 70
3 12 3 78
4 12 0 78
5 16 1 86
6 20 2 94
7 24 3 102
8 24 0 102
9 28 1 110
1076  Using Arduino / Storage / Re: Writing binary file to SD (creating BMP files) on: July 03, 2012, 03:40:05 pm
This seems to do the alternating white and black pixels.
You will need to fix the file size to include padding.
Code:
 int filesize = 54 + 24;// 3 * w * h;      //  w is image width, h is image height
Here is the sketch
Code:
//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>

const uint8_t csPin = 8;

char name[] = "9px_00.bmp";
int w = 3;
int h = 2;
int px[] = {
  255, 0, 255, 0, 255, 0 };
boolean debugPrint = true;

SdFat sd;
SdFile file;


void setup() {

  // SD setup
  Serial.begin(9600);
  if (!sd.init(SPI_FULL_SPEED, csPin)) {
    sd.initErrorHalt();
  }

  // if name exists, create new filename
  for (int i=0; i<1000; i++) {
    name[4] = i/10 + '0';
    name[5] = i%10 + '0';
    if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) {
      break;
    }
  }

  // create image data
  // heavily modified version via: http://stackoverflow.com/a/2654860
  unsigned char *img = NULL;          // image data
  // need to fix this so padding is included
  int filesize = 54 + 24;// 3 * w * h;      //  w is image width, h is image height
  if (img) {
    free(img);
  }
  img = (unsigned char *)malloc(3*w*h);
  //memset(img,0,sizeof(img));        // not sure if I really need this; runs fine without...

  for (int y=0; y<h; y++) {
    for (int x=0; x<w; x++) {
      int colorVal = px[y*w + x];
      img[(y*w + x)*3+2] = (unsigned char)(colorVal);
      img[(y*w + x)*3+1] = (unsigned char)(colorVal);
      img[(y*w + x)*3+0] = (unsigned char)(colorVal);
    }
  }

  // print px and img data for debugging
  if (debugPrint) {
    Serial.print("Writing \"");
    Serial.print(name);
    Serial.print("\" to file...\n");
    for (int i=0; i<w*h; i++) {
      Serial.print(px[i]);
      Serial.print("  ");
    }
  }

  // create file headers (also taken from above example)
  unsigned char bmpFileHeader[14] = {
    'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0             };
  unsigned char bmpInfoHeader[40] = {
    40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0             };
  unsigned char bmpPad[3] = {
    0,0,0             };

  bmpFileHeader[ 2] = (unsigned char)(filesize    );
  bmpFileHeader[ 3] = (unsigned char)(filesize>> 8);
  bmpFileHeader[ 4] = (unsigned char)(filesize>>16);
  bmpFileHeader[ 5] = (unsigned char)(filesize>>24);

  bmpInfoHeader[ 4] = (unsigned char)(       w    );
  bmpInfoHeader[ 5] = (unsigned char)(       w>> 8);
  bmpInfoHeader[ 6] = (unsigned char)(       w>>16);
  bmpInfoHeader[ 7] = (unsigned char)(       w>>24);
  bmpInfoHeader[ 8] = (unsigned char)(       h    );
  bmpInfoHeader[ 9] = (unsigned char)(       h>> 8);
  bmpInfoHeader[10] = (unsigned char)(       h>>16);
  bmpInfoHeader[11] = (unsigned char)(       h>>24);

  // write the file!
  // this is a combination of the bmp example above and
  // one from the SdFat library (it doesn't create a usable
  // bmp file, though)...
  file.write(bmpFileHeader, sizeof(bmpFileHeader));    // write file header
  file.write(bmpInfoHeader, sizeof(bmpInfoHeader));    // " info header
  
  // sizeof(img) is always 2 must want h
  for (int i=0; i<h; i++) {                  // iterate image array
    file.write(img+(w*(h-i-1)*3), 3*w);                // write px data
    file.write(bmpPad, (4-(w*3)%4)%4);                 // and padding as needed
  }
  file.close();
  if (debugPrint) {
    Serial.println("\n---");
  }
}

void loop() { }
1077  Using Arduino / Storage / Re: Writing binary file to SD (creating BMP files) on: July 03, 2012, 03:04:38 pm
Try this:
Code:
//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>

const uint8_t csPin = 8;

char name[] = "9px_00.bmp";
int w = 3;
int h = 2;
int px[] = {
  255, 0, 255, 0, 255, 0 };
boolean debugPrint = true;

SdFat sd;
SdFile file;


void setup() {

  // SD setup
  Serial.begin(9600);
  if (!sd.init(SPI_FULL_SPEED, csPin)) {
    sd.initErrorHalt();
  }

  // if name exists, create new filename
  for (int i=0; i<1000; i++) {
    name[4] = i/10 + '0';
    name[5] = i%10 + '0';
    if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) {
      break;
    }
  }

  // create image data
  // heavily modified version via: http://stackoverflow.com/a/2654860
  unsigned char *img = NULL;          // image data
//  int filesize = 54 + 3 * w * h;      //  w is image width, h is image height
  int filesize = 54 + 4 * w * h;      //  w is image width, h is image height  
  if (img) {
    free(img);
  }
  img = (unsigned char *)malloc(3*w*h);
  //memset(img,0,sizeof(img));        // not sure if I really need this; runs fine without...

  for (int y=0; y<h; y++) {
    for (int x=0; x<w; x++) {
      int colorVal = px[y*w + x];
      img[(y*w + x)*3+2] = (unsigned char)(colorVal);
      img[(y*w + x)*3+1] = (unsigned char)(colorVal);
      img[(y*w + x)*3+0] = (unsigned char)(colorVal);
    }
  }

  // print px and img data for debugging
  if (debugPrint) {
    Serial.print("Writing \"");
    Serial.print(name);
    Serial.print("\" to file...\n");
    for (int i=0; i<w*h; i++) {
      Serial.print(px[i]);
      Serial.print("  ");
    }
  }

  // create file headers (also taken from above example)
  unsigned char bmpFileHeader[14] = {
    'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0             };
  unsigned char bmpInfoHeader[40] = {
    40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0             };
  unsigned char bmpPad[3] = {
    0,0,0             };

  bmpFileHeader[ 2] = (unsigned char)(filesize    );
  bmpFileHeader[ 3] = (unsigned char)(filesize>> 8);
  bmpFileHeader[ 4] = (unsigned char)(filesize>>16);
  bmpFileHeader[ 5] = (unsigned char)(filesize>>24);

  bmpInfoHeader[ 4] = (unsigned char)(       w    );
  bmpInfoHeader[ 5] = (unsigned char)(       w>> 8);
  bmpInfoHeader[ 6] = (unsigned char)(       w>>16);
  bmpInfoHeader[ 7] = (unsigned char)(       w>>24);
  bmpInfoHeader[ 8] = (unsigned char)(       h    );
  bmpInfoHeader[ 9] = (unsigned char)(       h>> 8);
  bmpInfoHeader[10] = (unsigned char)(       h>>16);
  bmpInfoHeader[11] = (unsigned char)(       h>>24);

  // write the file!
  // this is a combination of the bmp example above and
  // one from the SdFat library (it doesn't create a usable
  // bmp file, though)...
  file.write(bmpFileHeader, sizeof(bmpFileHeader));    // write file header
  file.write(bmpInfoHeader, sizeof(bmpInfoHeader));    // " info header
  /*
  // sizeof(img) is always 2
  for (int i=0; i<sizeof(img); i++) {                  // iterate image array
    file.write(img+(w*(h-i-1)*3), w);                  // write px data
    file.write(bmpPad, (4-(w*3)%4)%4);                 // and padding as needed
  }
  */
  int n = w*h;
  for (int i = 0; i < n; i++) {
    file.write(img + 3*i, 3);
    file.write((uint8_t)0);
  }
  file.close();
  if (debugPrint) {
    Serial.println("\n---");
  }
}

void loop() { }

This make a valid bmp but not what you intended. The write is still not correct.


Here is a sketch that will dump a small bmp file:
Code:
#include <SdFat.h>
const uint8_t csPin = 8;
SdFat sd;
SdFile file;
char* fileName = "9PX_03.BMP";

class BmpHeader {
public:
  char signature[2];
  uint32_t fileSize;
  uint32_t reserved;
  uint32_t dataOffset;
};
class BmpInfo {
public:
  uint32_t size;  // 40
  uint32_t width;
  uint32_t height;
  uint16_t planes;
  uint16_t bitsPerPixel;
  uint32_t compression;
  uint32_t imageSize;  // compressed size
  uint32_t xPixelsPerM;
  uint32_t yPixelsPerM;
  uint32_t colorsUsed;
  uint32_t importantColors;
};
BmpHeader header;
BmpInfo info;

void printHexU8(uint8_t h) {
  if (h < 16) Serial.write('0');
  Serial.print(h, HEX);
}
void setup() {
  Serial.begin(9600);
  if (!sd.init(SPI_FULL_SPEED, csPin)) sd.initErrorHalt();
  if (!file.open(fileName, O_READ)) {
    Serial.println("openError");
    return;
  }
  if (file.read(&header, sizeof(header)) != sizeof(header) ||
    file.read(&info, sizeof(info)) != sizeof(info)) {
    Serial.println("read error");
    return;
  }
  Serial.print("header.fileSize: ");
  Serial.println(header.fileSize);
  Serial.print("header.dataOffset: ");  
  Serial.println(header.dataOffset);
  Serial.print("info.size: ");  
  Serial.println(info.size);
  Serial.print("info.width: ");  
  Serial.println(info.width);
  Serial.print("info.height: ");  
  Serial.println(info.height);
  Serial.print("info.planes: ");  
  Serial.println(info.planes);  
  Serial.print("info.bitsPerPixel: ");  
  Serial.println(info.bitsPerPixel);  
  Serial.print("info.compression: ");  
  Serial.println(info.compression);  
  Serial.print("info.imageSize: ");  
  Serial.println(info.imageSize);  
  Serial.print("info.xPixelsPerM: ");  
  Serial.println(info.xPixelsPerM);    
  Serial.print("info.yPixelsPerM: ");  
  Serial.println(info.yPixelsPerM);  
  Serial.print("info.colorsUsed: ");  
  Serial.println(info.colorsUsed);  
  Serial.print("info.importantColors: ");  
  Serial.println(info.importantColors);
  int b;
  int i = 0;
 while ((b = file.read()) >= 0) {
   printHexU8(i++);
   Serial.write(' ');
   printHexU8(b);
   Serial.println();
 }
  
}
void loop() {
}
1078  Using Arduino / Storage / Re: Arduino and SD card help on: July 03, 2012, 12:17:35 pm
Reading files into variables is often easier using the C++ Standard I/O Streams Library.  SdFat implements much of this library.

Here is a sketch that read a line from the jessica.txt file into the details structure, prints the structure, rewinds the file and lists the file as ascii text.

Code:
#include <SdFat.h>
SdFat sd;
const int chipSelect = 4;

class details {
public:
  int hours;
  int mins;
  int secs;
  int pillOne;
  int pillTwo;
};

void setup( ) {
  Serial.begin(9600);

  // SdFat will initialize pin 10, SS, as an output and set it high
  if (!sd.init(SPI_FULL_SPEED, chipSelect)) sd.initErrorHalt();
 
  // open file
  ifstream jessicaFile("jessica.txt");
  if (!jessicaFile.is_open()) {
    Serial.println("open failed");
    return;
  }
  // structure for data
  details jessica;

  // read line
  jessicaFile >> jessica.hours >> jessica.mins >> jessica.secs >> jessica.pillOne >>jessica.pillTwo;

  // check for read error
  if (!jessicaFile) {
    Serial.println("read error");
    return;
  }
  // print result
  Serial.print("Hours: ");
  Serial.println(jessica.hours);
  Serial.print("Minutes: ");
  Serial.println(jessica.mins);
  Serial.print("Seconds: ");
  Serial.println(jessica.secs);
  Serial.print("Pill One: ");
  Serial.println(jessica.pillOne);
  Serial.print("Pill Two: ");
  Serial.println(jessica.pillTwo);
 
  // list the file content
  Serial.println("___________________");
  // rewind the file
  jessicaFile.seekg(0);
  int c;
  // print file
  while ((c = jessicaFile.get()) >= 0) Serial.write(c);
}
void loop() {}

Here is the output from the sketch:
Quote
Hours: 12
Minutes: 1
Seconds: 0
Pill One: 1
Pill Two: 0
______________________
12 01 00 1 0

The SdFat library is here http://code.google.com/p/sdfatlib/downloads/list
1079  Using Arduino / Storage / Re: Is there a SD Tutorial? on: June 30, 2012, 02:39:35 pm
The specification for SD cards is

Supply voltage, Vdd, in the range 2.7 - 3.6 volts.

Voltage on all signal lines from -0.3 V to Vdd + 0.3 V.

Logic levels on input lines:

Input high voltage 0.625*Vdd to Vdd + 0.3.

Input low voltage -0.3 to 0.25*Vdd.

You need level converters to convert the Arduino outputs Chip Select, SCK, and MOSI to this range.

Resistor voltage dividers do not perform well on many cards since they have long rise/fall times.  IC level converters are preferred since they work better with cards that use edge detectors.

No level converter is needed for the Arduino input MISO.
1080  Development / Suggestions for the Arduino Project / Re: bug in wiring.c millis() fctn, Sc2Card.cpp on: June 28, 2012, 09:29:59 pm
There is no problem with rollover for timeouts.  You can even use 16 bit variables for short times like this.

Code:
  uint16_t t0 = millis();
  // do something that takes up to a few seconds
  uint16_t t1 = millis();

   // print elapsed time
  Serial.println(t1 - t0);

Here is an example where rollover happens between t0 and t1.
Code:
uint16_t t0 = 0XFFFF;
uint16_t t1 = 1;

void setup() {
  Serial.begin(9600);
  Serial.println(t1 - t0);
}

void loop() {}
This sketch prints "2", the correct elapsed time.
Pages: 1 ... 70 71 [72] 73 74 ... 109