reverse pow

Sorry for the stupid question... but I just can't figure this one out... I have value that increases exponentially ... and I want to decrease the value by the same amount in every step as when it was increasing... my code looks like this:

   if((encoder_A) && (!encoder_A_prev)){
      if(!encoder_B) {   // Clockwise - increasing
fadetarget = constrain((pow((fadetarget + 1), exp_factor)), minimumFade, 255) ;
      } else {              // Counterclockwise - decreasing
fadetarget = constrain((pow(fadetarget, exp_factor)), minimumFade, 255) ;
      }
    }
    encoder_A_prev = encoder_A;

When increasing value, it gives me almost perfect linear output from pwm driven LED with exp_factor = 1.025. How do I decrease the fadetarget? I guess I shoud use logarithm, but how exactly?

The reverse of power is power of the reverse!

if x = ab then a = x(1/b) because a(b*1/b) == a1 == a

logarithm you use when you want to find the exponent

if x = ab then alog(x) = b

to calc alog(x) = b you use log(x) / log(a) or ln(x) / ln(a)

The inverse of your power ... 1/exp_factor

Of course it is! Thank you very much, I should have paid more attention to algebra lessons back at school :(

If your power, which is 1.025, is so close to linear, which is 1, why not using linear function instead of power?

Well.. I even changed it to 1.02, and it is not even close to linear... here is the PWM 255 values in 32 steps I'm using:

what is the function of that graph? Can you post the code that generates this one?

robtillaart:
what is the function of that graph?
Can you post the code that generates this one?

Here you go (to be easier to understand, I will post my whole rotary encoder ISR):

void encoder(){
    encoder_A = digitalRead(pin_A);
    encoder_B = digitalRead(pin_B);   
    if((!encoder_A) && (encoder_A_prev)){
      if(encoder_B) {  
Serial.println("cw >");        // Clockwise
fadetarget = constrain((pow((fadetarget + 2), exp_factor)), minimumFade, 255) ;
      } else {       
Serial.println("ccw <");         // Counterclockwise
fadetarget = constrain((pow((fadetarget - 2), (1/exp_factor))), minimumFade, 255) ;
      }
    }
    if((encoder_A) && (!encoder_A_prev)){
      if(!encoder_B) {
Serial.println("cw 2 >");          // Clockwise
fadetarget = constrain((pow((fadetarget + 2), exp_factor)), minimumFade, 255) ;
      } else {
Serial.println("ccw 2 <");        // Counterclockwise
fadetarget = constrain((pow((fadetarget - 2), (1/exp_factor))), minimumFade, 255) ;
      }
    }
    encoder_A_prev = encoder_A;     // Noglabaajam ieprieksheejo staavokli      
}

exp_factor = 1.02;

BTW, needs only one interrupt, on pin_A on change for encoder operation.

Sorry, off-topic - I just LOVE Google Translate. I wanted to know what "Noglabaajam ieprieksheejo staavokli" meant. Google figured it was Latvian, AND suggested the correct spelling "Saglab?jam iepriekš?jo st?vokli" which it had translated to "Maintain the previous state" - which makes sense for the code shown.

(And now back to the problem :) )

Msquare: Sorry, off-topic - I just LOVE Google Translate. I wanted to know what "Noglabaajam ieprieksheejo staavokli" meant. Google figured it was Latvian, AND suggested the correct spelling "Saglab?jam iepriekš?jo st?vokli" which it had translated to "Maintain the previous state" - which makes sense for the code shown.

(And now back to the problem :) )

Yes, I know I shouldn't use Latvian language in comments, but somehow forgetting it all the time :) +1 to google translate :)

On the contrary - I think programs should be written as nativly as possibly. In fact in C/C++ I could do a number of #defines so even "if", "else", "void" etc was translated to f.eks. Danish. :astonished:

#define hvis if
#define intet void
intet loop() { hvis ( x == 3 ) x++ }

I have just been using English (my 3rd language) for so long now it just comes naturally to do english with the english programming keywords. :open_mouth:

Now, when trying to get help in an international community it helps it is in english. :) But even that could be argued - if the variable names are nonsense to me, then I will only look at the structure and syntax and not be "fooled" into thinking it does something because of the function name or comment. On the other hand, the speed at which I understand the code will be much reduced, precisly because of that - I cant skip any sections because name/comments indicate the function to be trivial.

Arty: ... have value that increases exponentially ... and I want to decrease the value by the same amount in every step as when it was increasing.../quote]

byte ReverceByte (byte b)
{
  int i;
  i=b;
  i=-1*(i-255);
  b=i;
  return b;
}

I think programs should be written as nativly as possibly.

Native to the people who develop/maintain it I assume

robtillaart:

I think programs should be written as nativly as possibly.

Native to the people who develop/maintain it I assume

yeah... it is not something I have a strong opinion on. If you're a developer in a large company then English is probably dictated anyhow (and necessary for multinational cooperation). In a one-person-hobby program, you write what makes sense to yourself. And everything in between correspondingly. (And now, back to the original programming problem ;) )

If you're a developer in a large company then English is probably dictated

Definitely, in my current project we have at least 6 nationalities and we do 95% of the communication in English, only if there is a homogeneous group the mother language is spoken.

robtillaart:

I think programs should be written as nativly as possibly.

Native to the people who develop/maintain it I assume

I have been thinking about it lots of times, but I can not come to the conclusion of what is the best way. To program in English, English + my native language or as natively as possible. Mainly its because of many non-ASCII characters in my native language (which you should not use anyway). But replacing characters like "Š" with "sh" or "Ž" with "zh" isn't truly native anymore. So I think I'll stick to English. :) This thread has gone somewhere far from the subject, and IMO with respect to the community should be locked :) Thank you for sharing you thoughts on this though.

Most people that program have some knowledge of English, so that would be an argument for English too.

:| "Why didn't you comment your code?" 8) "Oh, it is commented. My native language is C."

rujoking: :| "Why didn't you comment your code?" 8) "Oh, it is commented. My native language is C."

a good one! :)

"Why didn't you comment your code?" "Oh, it is commented. My native language is C."

LOL variation: oh(), it == commented; MyNative(Language) == C;