Skip to main content
Respond to edit in the question
Source Link
Dave Tweed
  • 184.9k
  • 17
  • 249
  • 435

You haven't shown us enough of your code, but presumably this fragment is embedded in a loop of some sort, perhaps even an Arduino-specific loop() function.

What this means is that your "off" time is not equal to your "on" time. The "on" time is simply the 10 µs delay, but the "off" time is the 10 µs delay plus the loop overhead — which might include the overhead of exiting the loop() function and then re-entering again.

As you decrease the nominal delay value, that loop overhead becomes a greater fraction of the total period, skewing the duty cycle and therefore the LED brightness.

This will be obvious if you simply look at the output pin with an oscilloscope.


Looking at the "full code" in your edit, there are many ways it could be optimized. I would try something like this:

#include <SPI.h> #include <SD.h> File dataFile; int myData_read; void setup() { pinMode(12, OUTPUT); Serial.begin(210000); if (!SD.begin(10)) { Serial.println("Card failed, or not present"); return; } Serial.println("card initialized."); dataFile = SD.open("DATALOG.dat"); myData_read = dataFile.read(); //Serial.println(myData_read); dataFile.close(); } void loop() { for (int i=0; i < 8; i++) { if (myData_read & (1<<i)) { PIOD->PIO_SODR = 1<<8; //HIGH on pin 12 //digitalWrite(12, HIGH); } else { PIOD->PIO_CODR = 1<<8; //LOW on pin 12 //digitalWrite(12,LOW); } delayMicroseconds(5); } } 

There's no reason to read the file over and over again in the loop, and the>> i in theif statement is completely superfluous.

You haven't shown us enough of your code, but presumably this fragment is embedded in a loop of some sort, perhaps even an Arduino-specific loop() function.

What this means is that your "off" time is not equal to your "on" time. The "on" time is simply the 10 µs delay, but the "off" time is the 10 µs delay plus the loop overhead — which might include the overhead of exiting the loop() function and then re-entering again.

As you decrease the nominal delay value, that loop overhead becomes a greater fraction of the total period, skewing the duty cycle and therefore the LED brightness.

This will be obvious if you simply look at the output pin with an oscilloscope.

You haven't shown us enough of your code, but presumably this fragment is embedded in a loop of some sort, perhaps even an Arduino-specific loop() function.

What this means is that your "off" time is not equal to your "on" time. The "on" time is simply the 10 µs delay, but the "off" time is the 10 µs delay plus the loop overhead — which might include the overhead of exiting the loop() function and then re-entering again.

As you decrease the nominal delay value, that loop overhead becomes a greater fraction of the total period, skewing the duty cycle and therefore the LED brightness.

This will be obvious if you simply look at the output pin with an oscilloscope.


Looking at the "full code" in your edit, there are many ways it could be optimized. I would try something like this:

#include <SPI.h> #include <SD.h> File dataFile; int myData_read; void setup() { pinMode(12, OUTPUT); Serial.begin(210000); if (!SD.begin(10)) { Serial.println("Card failed, or not present"); return; } Serial.println("card initialized."); dataFile = SD.open("DATALOG.dat"); myData_read = dataFile.read(); //Serial.println(myData_read); dataFile.close(); } void loop() { for (int i=0; i < 8; i++) { if (myData_read & (1<<i)) { PIOD->PIO_SODR = 1<<8; //HIGH on pin 12 //digitalWrite(12, HIGH); } else { PIOD->PIO_CODR = 1<<8; //LOW on pin 12 //digitalWrite(12,LOW); } delayMicroseconds(5); } } 

There's no reason to read the file over and over again in the loop, and the>> i in theif statement is completely superfluous.

Source Link
Dave Tweed
  • 184.9k
  • 17
  • 249
  • 435

You haven't shown us enough of your code, but presumably this fragment is embedded in a loop of some sort, perhaps even an Arduino-specific loop() function.

What this means is that your "off" time is not equal to your "on" time. The "on" time is simply the 10 µs delay, but the "off" time is the 10 µs delay plus the loop overhead — which might include the overhead of exiting the loop() function and then re-entering again.

As you decrease the nominal delay value, that loop overhead becomes a greater fraction of the total period, skewing the duty cycle and therefore the LED brightness.

This will be obvious if you simply look at the output pin with an oscilloscope.