SteveMann:
If you want more eyeballs on the project, post the code. Not a link. Many forum members will not open websites or download files.
Thanks, Steve! Here's the code:
[ltr][color=#222222][code]// Stroboscope with 4-digit 7-segment display
// The display can be directly soldered to the Arduino Nano on pins A0-A5, D4-D9
// the frequency can be changed with two push-buttons
// the pulselength can be changed by pressing both buttons simultaneously
//for common anode display
#define DIGON HIGH
#define DIGOFF LOW
#define SEGON LOW
#define SEGOFF HIGH
//for common cathode display
//#define DIGON LOW
//#define DIGOFF HIGH
//#define SEGON HIGH
//#define SEGOFF LOW
const byte ndig=4;
const byte nseg=8;
//breadboard setup
//const byte digs[ndig] = { 9, 6, 5,A5}; // digs 1 2 3 4
//const byte segs[nseg] = { 8, 4,A3,A1,A0, 7,A4,A2}; // segs ABCDEFGH
//nano setup
const byte digs[ndig] = { 4, 7, 8,A0}; // digs 1 2 3 4
const byte segs[nseg] = { 5, 9,A2,A4,A5, 6,A1,A3}; // segs ABCDEFGH
const byte button_up=12;
const byte button_dw=11;
//translate number into corresponding segements
const byte val2seg[12]={
B11111100, //0
B01100000, //1
B11011010, //2
B11110010, //3
B01100110, //4
B10110110, //5
B10111110, //6
B11100000, //7
B11111110, //8
B11110110, //9
B00000000, //nothing
B01001110, //mu
};
int freq=200; //frequency in units of 0.1Hz
byte len=1; //pulselength in units of 64 microseconds
void setup() {
//setup TIMER1B for fast PWM with (output on pin D10)
TCCR1A=B00100010;
TCCR1B=B00011000;
setfreq(freq,len);
pinMode(10,OUTPUT);
// set to output and switch off all digits
for (byte idig=0; idig<ndig; idig++){
pinMode(digs[idig], OUTPUT);
digitalWrite(digs[idig],DIGOFF);
}
// set to output and switch off all segments
for (byte iseg=0; iseg<nseg; iseg++){
pinMode(segs[iseg], OUTPUT);
digitalWrite(segs[iseg],SEGOFF);
}
//set the pushbuttons to input with pull-up
pinMode(button_up,INPUT_PULLUP);
pinMode(button_dw,INPUT_PULLUP);
}
//set frequency in Hz, pulse length in units of 64mus
void setfreq(int f, int len){
//calculate what to put into the timing registers
long unsigned int ticks=(160000000+f/2)/f; // f_clock/0.1f
unsigned int ps=1;
if (ticks> 0xFFFF)ps= 8;
if (ticks> 8*0xFFFF)ps= 64;
if (ticks> 64*0xFFFF)ps= 256;
if (ticks>256*0xFFFF)ps=1024;
unsigned int val_ICR1=ticks/ps-1;
unsigned int val_OCR1B=len*(1024/ps)-1;
unsigned int val_TCCR1B=B00011001;
if (ps== 8) val_TCCR1B=B00011010;
if (ps== 64) val_TCCR1B=B00011011;
if (ps== 256) val_TCCR1B=B00011100;
if (ps==1024) val_TCCR1B=B00011101;
//set the actual values in the timing registers
//noInterrupts();
if (TCNT1>val_ICR1)TCNT1=0; //do not allow timer to exceed 'top'
ICR1=val_ICR1;
OCR1B=val_OCR1B;
TCCR1B=val_TCCR1B;
//interrupts();
}
//set a digit to a certain value
void setdig(byte dig, byte val, bool dot){
//switch off all digits
for (byte idig=0; idig<ndig; idig++){
digitalWrite(digs[idig],DIGOFF);
}
//set the segments
for (byte iseg=0; iseg<nseg; iseg++){
if ((val2seg[val]&(1<<(nseg-1-iseg)))>0){
digitalWrite(segs[iseg], SEGON);
} else {
digitalWrite(segs[iseg], SEGOFF);
}
}
// set the decimal dot if needed
if (dot){
digitalWrite(segs[7], SEGON);
} else{
digitalWrite(segs[7], SEGOFF);
}
//set the digit
digitalWrite(digs[dig],DIGON);
}
byte dispdig=0;
byte prevbutstat=0;
unsigned long millis_butchanged=0;
unsigned long millis_valchanged=0;
void loop() {
//check for buttons
long unsigned millis_current=millis();
bool change=false;
byte butstat=(digitalRead(button_up)==LOW)*1+(digitalRead(button_dw)==LOW)*2;
if (butstat!=prevbutstat)millis_butchanged=millis_current;
int dt=200; int df=1;
if (millis_current-millis_butchanged>1000)dt=100;
if (millis_current-millis_butchanged>2000)dt=50;
if (millis_current-millis_butchanged>3000)dt=20;
if (millis_current-millis_butchanged>4000)dt=10;
if (millis_current-millis_butchanged>5000)dt=5;
if (millis_current-millis_butchanged>6000)dt=2;
if (millis_current-millis_butchanged>7000)dt=1;
if (millis_current-millis_butchanged>8000)df=2;
if (millis_current-millis_butchanged>9000)df=5;
if (millis_current-millis_butchanged>10000)df=10;
if (butstat==3)dt=1000;
if(millis_current-millis_valchanged>dt){
if(butstat==1) freq=freq+df;
if(butstat==2) freq=freq-df;
if(butstat==3) len++;
if(butstat>0){
change=true; millis_valchanged=millis_current;
}
}
prevbutstat=butstat;
//keep frequency and pulselength wihin allowed domain
freq=max(freq,3);
freq=min(freq,9999);
if (len>15)len=1;
if (len*freq>15625)len=1; //corresponds to 0.1 duty cycle
if (change) setfreq(freq,len);
//display a digit
byte val=10; // by default nothing
if (butstat==3){
if (dispdig==0 and len>1)val=((len*64)/100)%10;
if (dispdig==1)val=((len*64)/10)%10;
if (dispdig==2)val=(len*64)%10;
if (dispdig==3)val=11; //the symbol mu
} else {
if (dispdig==0 and freq>999)val=(freq/1000)%10;
if (dispdig==1 and freq>99)val=(freq/100)%10;
if (dispdig==2)val=(freq/10)%10;
if (dispdig==3)val=freq%10;
}
setdig(dispdig,val,(dispdig==2 and butstat!=3));
delayMicroseconds(100);
dispdig=(dispdig+1)%ndig;
}
[/color][/ltr]
[/code]
UPDATE:
I cut the negative cable and fired it up, I can complete the connection and it lights right up, no warm up. So physically, using a relay I could accomplish the timed flashing I'm after.
However, at 40hz, for example, that's switching every 12.5ms, which isn't a sustainable solution with a relay.
Also, to clarify, when I am saying frequency and hz, I'm referring to the flashing of this led like a strobe, so 40hz would be: 12.5ms ON, 12.5ms OFF. The frequency is flashing of light, here 40 times per second, or 40hz.
I'll test using a mosfet as a switch next. I have some logic level mosfets on the way soon and will report back with the answer. As aarg pointed out, there is already a mosfet in the mix, so it could throw things off, we will know soon!