jasmine2501:
unsigned int brightness=0xffff;
while (brightness > 255) {
setLED(brightness>>8);
brightness -= brightness>>2;
delay(10);
}
There's only gonna be 4 steps in that though, right?
No, because it compounds. You are subtracting a quarter of the current value, the second time you do it, you do not reduce it to half, but to 0.5625 (or whatever approximates that as a 16 bit integer), the third time to 0.422, the fourth to 0.311, thence 0.237, 0.177, 0.133 and so on.
My previous comment got a little muddled. The algorithm will work quite well in 16-bit arithmetic, it is the 8-bit PWM that becomes too coarse at the lowest levels. You do of course have to limit the shift-and-subtract algorithm once it approaches a value where it no longer reduces at all.
The 25% per step reduction is however, a little coarse for smooth dimming where the steps are slow enough to be seen. This can be demonstrated using the PWM values on a MAX7219.
/* Fading demonstration
Code for max 7219 from maxim, reduced & optimised for using multiple 7219 cascaded.
______________________________________
General notes:
- if using only one max7219, then use maxSingle function to control
the module --- maxSingle(register (1-8), column (0-255))
- if using more then one max7219, and all should work the same, use maxAll
function --- maxAll(register (1-8), collum (0-255))
- if using more than one max7219 and want to change something on one module only,
then use maxOne function
--- maxOne(module you want to control [1=first], register [1-8], column [0-255])
During initiation, be sure to send every part to every max7219 and then upload it.
For example, if you have five max7219's, you have to send the scanLimit 5 times
before you load it, otherwise not every max7219 will get the data. the (fixed)
variable maxInUse keeps track of this, just tell it how many max7219 you are using.
*/
#include <Wire.h>
int dataIn = 2; // "DIN" on module
int load = 3; // "CS" on module
int clock = 4; // "CLK" on module
const int ledPin = 13; // LED pin number
int maxInUse = 1; // set how many MAX7219's used
int ledState = LOW; // initialise the LED
int e = 0; // just a varialble
// define max7219 registers
byte max7219_reg_noop = 0x00;
byte max7219_reg_digit0 = 0x01;
byte max7219_reg_digit1 = 0x02;
byte max7219_reg_digit2 = 0x03;
byte max7219_reg_digit3 = 0x04;
byte max7219_reg_digit4 = 0x05;
byte max7219_reg_digit5 = 0x06;
byte max7219_reg_digit6 = 0x07;
byte max7219_reg_digit7 = 0x08;
byte max7219_reg_decodeMode = 0x09;
byte max7219_reg_intensity = 0x0a;
byte max7219_reg_scanLimit = 0x0b;
byte max7219_reg_shutdown = 0x0c;
byte max7219_reg_displayTest = 0x0f;
void putByte(byte data) {
byte i = 8;
byte mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
digitalWrite( clock, LOW); // tick
if (data & mask) { // choose bit
digitalWrite(dataIn, HIGH);// send 1
} else {
digitalWrite(dataIn, LOW); // send 0
}
digitalWrite(clock, HIGH); // tock
--i; // move to lesser bit
}
}
// maxSingle is the "easy" function to use for a single max7219
void maxSingle( byte reg, byte col) {
digitalWrite(load, LOW); // begin
putByte(reg); // specify register
putByte(col); //((data & 0x01) * 256) + data >> 1); // put data
digitalWrite(load,HIGH);
}
// initialize all MAX7219's
void maxAll( byte reg, byte col) {
int c = 0;
digitalWrite(load, LOW);
for ( c =1; c<= maxInUse; c++) {
putByte(reg); // specify register
putByte(col); //((data & 0x01) * 256) + data >> 1); // put data
}
digitalWrite(load,HIGH);
}
// for adressing different MAX7219's while cascaded
void maxOne(byte maxNr, byte reg, byte col) {
int c = 0;
digitalWrite(load, LOW); // begin
for ( c = maxInUse; c > maxNr; c--) {
putByte(0); // no operation
putByte(0); // no operation
}
putByte(reg); // specify register
putByte(col); //((data & 0x01) * 256) + data >> 1); // put data
for ( c = maxNr-1; c >= 1; c--) {
putByte(0); // no operation
putByte(0); // no operation
}
digitalWrite(load,HIGH);
}
void putCol( byte colno, byte coldat) {
// Interprets colno as (zero ref) index in combined array
byte t;
t = colno >> 3;
byte u;
u = colno & 0x07;
maxOne(t+1, u+1, coldat);
}
void dispon() {
maxAll(max7219_reg_shutdown, 0x01); // Display on
}
void dispoff() {
maxAll(max7219_reg_shutdown, 0x00); // Display off
}
byte pattern; // bit mask
byte dimstate; // state pointer
void worker () {
switch (dimstate) {
case 0:
maxAll(max7219_reg_intensity, 0x0f & 0x0f); // middle argument is intensity value
dimstate++;
break;
case 1:
maxAll(max7219_reg_intensity, 0x0b & 0x0f);
dimstate++;
break;
case 2:
maxAll(max7219_reg_intensity, 0x08 & 0x0f);
dimstate++;
break;
case 3:
maxAll(max7219_reg_intensity, 0x06 & 0x0f);
dimstate++;
break;
case 4:
maxAll(max7219_reg_intensity, 0x05 & 0x0f);
dimstate++;
break;
case 5:
maxAll(max7219_reg_intensity, 0x04 & 0x0f);
dimstate++;
break;
case 6:
maxAll(max7219_reg_intensity, 0x05 & 0x0f);
dimstate++;
break;
case 7:
maxAll(max7219_reg_intensity, 0x06 & 0x0f);
dimstate++;
break;
case 8:
maxAll(max7219_reg_intensity, 0x08 & 0x0f);
dimstate++;
break;
case 9:
maxAll(max7219_reg_intensity, 0x0b & 0x0f);
dimstate = 0;
break;
default:
maxAll(max7219_reg_intensity, 0x0f & 0x0f);
dimstate = 0;
break;
}
}
// the follow variable is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 200; // interval at which to step (milliseconds)
long previousMillis = 0; // will store last time LED was updated
void setup () {
pinMode(dataIn, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);
pinMode(ledPin, OUTPUT);
//Serial begin(9600);
digitalWrite(13, HIGH);
//initiation of the max 7219
maxAll(max7219_reg_displayTest, 0x00); // no display test
maxAll(max7219_reg_scanLimit, 0x07); // all columns in use
maxAll(max7219_reg_decodeMode, 0x00); // using a LED matrix (not digits)
maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode
putCol(0,0); // blank.
pattern = 0x7e; // 6 middle bits
for ( int j = 1; j < 7; j++ ) { // 6 middle rows
putCol(j,pattern); } // Show the column.
putCol(7,0); // blank.
maxAll(max7219_reg_intensity, 0x08 & 0x0f); // middle argument is intensity value
// range: 0x00 to 0x0f
dimstate = 0;
}
void loop () {
unsigned long currentMillis = millis();
// Active waiting for next event
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; }
// Timed process:
worker();
// set the LED according to ledState:
digitalWrite(ledPin, ledState);
}
}
I do think that the 6.25% per step reduction (shift right by 4 and subtract) would be preferable.