ATTiny85; order of magnitude loss of speed when enabling map AnalogRead function

lost_bro:
The code now produces a potentiometer controlled variable ramp marktime of 4-24ms :slight_smile: :slight_smile: :slight_smile:

Woot!

and a potentiometer controlled variable DeadTime (time off between ramps) :slight_smile: :slight_smile: :slight_smile:

Hollah!

But.......... now the third potentiometer (ramp height control) is NOT working :confused: :frowning:

What?

Were you able to control the pulse height before? Because looking over you code it looks like it was malfunctioning from the start. It appears that your intention is to ramp up to pwmmax, reverse direction, then ramp down to pwmmin, then pause for the dead-time. But the scope traces you are posting show the ramp going all the way to Vcc then straight back down to 0. I think the problem's here:

//////////////////////////////////////////////////////////
void loop()
{

int  Dir=2;
 
////// Increment/decrement PWM on ledPin with a period of 'x RampLength' in us. /////

Why do you set Dir on every pass through loop() here on line 99? Why do you set it to 2 here when every other place uses 1? How about you just do enum ramp_direction {FALLING=-1, PAUSED=0, RISING=1}; and use those values? You probably also intended Dir to be [b]static[/b].

        StartUs=micros();

Minor point here on line 136, this is not the recommended way of advancing the interval. Use StartUs += rampLength; instead.

EDIT: I did run the one second led flash script, and it scopes out to about 980-990ms both Marktime & Deadtime. So I guess we are good with the timing........

Also, I did change the code to reflect Micros instead of Millis.... when I use Millis, I do NOT get a response on the scope equal to the correct/calculated in code timing values.

I will have to spend more time on this tomorrow after work again.

That is because I told you to replace the delay statement with the do-while loop, not supplement it.

if(Pwm==pwmMin)
                                                     //if PWM reaches the minimum:
        {                                             
          digitalWrite(outPin, LOW);
          delay(INTERVAL);   <-- PITCH IT. Throw it out the door.
          Dir=+1;                                 //delay *INTERVAL* & change direction
          delayStartTime = micros();
              do
              {
                INTERVAL   = map(analogRead(intervalPin),0,1023,800,50000); 
                pwmMax     = map(analogRead(pwmMaxPin),0,1023,55,255);   
                ramplength = map(analogRead(ramplengthPin),0,1023,2,11800);
              }
              while(micros()-delayStartTime<=INTERVAL); 
                                               
        }