# SparkFun Forums

Your source for all things Atmel.

By keithg
#84557
LED's will change state when analog input is five or six numbers from the threshold, making accurate transitions impossible. Is there some better way to resolve this problem?
Code: Select all
``````unsigned long starTime;
int flashRed = 20000;

int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;

#define blueLED 11
#define redLED 12
#define greenLED 13
#define AnIn 1
#define redThresh 670
#define blueThresh 660
#define greenThresh 0

int val;

void setup()
{

starTime = millis();  //timer

pinMode(redLED,OUTPUT);
pinMode(blueLED,OUTPUT);
pinMode(greenLED,OUTPUT);

Serial.begin(9600);
}
void loop()
{
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
// ...wrap around to the beginning:
index = 0;
// calculate the average:

starTime = millis();  //timer

// val = val/2;
if(average >= redThresh){
digitalWrite(redLED,HIGH);
digitalWrite(blueLED,LOW);
digitalWrite(greenLED,LOW);
delay(1000);
}
else {
digitalWrite(redLED,LOW);

if((average>= blueThresh) && (average <= redThresh))
{
digitalWrite(blueLED,HIGH);
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
delay(1000);
}
else {
digitalWrite(blueLED,LOW);

if((average>= greenThresh) && (average <= blueThresh))
{
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,LOW);
digitalWrite(redLED,LOW);
delay(1000);
}
else {
digitalWrite(greenLED,LOW);
}
}
Serial.println(average);
}

}

``````

### #84614

By Liencouer
#84614
The biggest thing I can see is that your thresholds are very close. 10 adc counts on a 10bit adc is only about 1% variation of input, or about 50mV difference on a 5V system. What is the input to the ADC? I would suggest looking at that portion of the system first.

Also, you don't need the second conditional in the average threshold comparisons - this should work:
Code: Select all
``````// if redThresh > blueThresh > greenThresh, the following is valid:
if(average >= redThresh)
{
// do red thing
}
else if( average >= blueThresh) // we know its less than red, 'cause we wouldn't get here otherwise
{
// do blue thing
}
else if( average >= greenThresh) // we know its less than blue and reg, 'cause we couldn't get here otherwise
{
// do green thing
}
else
{
// none of the above
}
``````

By keithg
#84615
Oh no! This presents serious trouble because of the many variations of this sketch, none will respond with more than just a few counts, not more than three or so apart. Here is the schematic. Is it possible to make the hardware more responsive? A transistor or another opamp? http://i189.photobucket.com/albums/z215 ... onitor.jpg[/img]

### #84699

By Liencouer
#84699
Increasing the gain of the op-amp should help increase the output range, if you're not already clipping. What is the purpose of the mash of diodes (D1, D2, D3, D5 on the right side of the schematic)? I think you need to take a look at the portion of the circuit between the output of the op amp OUT1 and make sure it does what you intend it to do.

### analog filtering#84821

By keithg
#84821
Here is the schematic from a web source. Please scroll to the bottom to find it. The diodes, according to the author, are intended to rectify the circuit, although "Physical Computing" pictures the same circuit, minus the diodes. Should I pull these?

http://itp.nyu.edu/physcomp/sensors/Rep ... icrophones

### #84962

By Liencouer
#84962
I dont think you need the diodes. The rectifier bridge doesn't make any sense - you don't have a positive and negative going signal to need rectifying. In your circuit, it should be obvious that D1, D2 and D3 will never turn on. I think that D5 doesn't help you either.

I'd also mention sparkfun has a breakout board for something like this. I'd recommend taking a peek at http://www.sparkfun.com/commerce/produc ... ts_id=8669
###### long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

###### Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

###### Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

###### Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]