fezder
Well-Known Member
Now that i managed to get ag's circuit to work at last, i figured it would be good excersice to create pair of lm3915's with code.
The circuit is that i feed peak detectors output to arduino's ADC input that then lights leds needed via two daisy chained 74595 shift registers. Main thing that bothers is that i had to manually calculate voltages for each led (3db difference between each led so logharitmic) and that procedure makes code perhaps too long and complicated.....
arduino's ADC seemed to perform well enought, only smallest resolution is 5mv, but i have 12-bit version too (external) but i don't feel confortable using it as of yet. I can also add schematic if that is needed
So, here comes uglee code, it works but could be better, i have that feeling:
The circuit is that i feed peak detectors output to arduino's ADC input that then lights leds needed via two daisy chained 74595 shift registers. Main thing that bothers is that i had to manually calculate voltages for each led (3db difference between each led so logharitmic) and that procedure makes code perhaps too long and complicated.....
arduino's ADC seemed to perform well enought, only smallest resolution is 5mv, but i have 12-bit version too (external) but i don't feel confortable using it as of yet. I can also add schematic if that is needed
So, here comes uglee code, it works but could be better, i have that feeling:
C:
int datapin = 2;
int latchpin = 3;
int clockpin = 4;
int val = 0;
void setup()
{
DDRD = DDRD | B11111100;
}
void loop()
{
val = analogRead (A0);
float voltage = val * (5.0 / 1023.0);
if ( voltage > 2.0)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 1.414000)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B01111111);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.999698)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00111111);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.706786)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00011111);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.499698)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00001111);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.353287)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000111);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.249774)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000011);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.176590)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000001);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if (voltage > 0.124849)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B11111111);
PORTD = B00001000;
}
else if ( voltage > 0.088268)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B01111111);
PORTD = B00001000;
}
else if (voltage > 0.062406)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00111111);
PORTD = B00001000;
}
else if (voltage > 0.044121)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00011111);
PORTD = B00001000;
}
else if (voltage > 0.031193)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00001111);
PORTD = B00001000;
}
else if (voltage > 0.022054)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00000111);
PORTD = B00001000;
}
else if (voltage > 0.015592)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00000011);
PORTD = B00001000;
}
else if (voltage > 0.011024)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00000001);
PORTD = B00001000;
}
else if (voltage < 0.005)
{
PORTD = B00000000;
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
shiftOut(datapin, clockpin, MSBFIRST, B00000000);
PORTD = B00001000;
}
}
Last edited: