I'm trying to control a 9v audio signal through a self made optocoupler(200k dark LDR and random blue LED, temporarily). They're all taped up together and dont seem to be getting any interference. The LDR is in parallel with a 10kohm resistor as I need a linear 0-10k resistance.
But I'm having a really hard time getting a nice linear fade in the resistance in the values I need. I tried to use logarithm and it helps a tad but I don't think I'm doing it right, and I seem to still be having other problems.
This is my equation
255-(log(256-i)*46))
I literally just added to it until I got about what I was going for. I assume it's not done correctly. I'm just looking for my 0-255 value to have a curve. It's still not curved enough according to my multimeter. But even more troubling is that it reports from 10k to IMMEDIATELY 5K when the LED PWM goes from 0 to 1 or 2. So I literally cannot get any more resolution out of that, and I need the 5k-10k to have at least close to as much resolution as 5k-0.
int j = -1;
void setup(){
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
Serial.begin(9600);
Serial.println("reset");
}
void loop(){
//analog input 0 connected to pot just for testing
int i = map(analogRead(0),0,1023,5,255);
analogWrite(3,255-(log(256-i)*46));//optocoupler LED
analogWrite(5,255-(log(256-i)*46));//test LED
if(i!=j){//Only print if value changes
Serial.println(255-(log(256-i)*46));//print value
j=i;
}
delay(10);
}
So basically I'm having nothing but trouble with this so I could really just any advice.
Thanks