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

Good day Jiggy-Ninja

Thanks for the advice:

I have modified the code again, and have made a minor advance, (I think).

Please see attached scope shot.

I am using the following code to turn off the square wave at the apex of the triangle wave:

ramplengthpause=(ramplength/3);
  if((Dir==-1) && (Dir != +1)) 
       {
         digitalWrite(outPin,LOW);
         delay(ramplengthpause);
       }

The result is seen in the same attached scope shot. The code will commence the square wave exactly where it should. :slight_smile:

And, the code will terminate the square wave at the apex.........

But.... simultaneous to the falling edge of the triangle wave, the code produces a fast pwm cycling (seen as the blue shaded area). Once pwmMin is reached, both waves seem to behave themselves.
If I can eliminate the fast pwm on the falling edge, then maybe I will have it working.

I also tried this code:

do
          {
            digitalWrite(outPin, LOW);
          }
            while((Dir==-1) && (Dir != +1));

It does not respond at all........

Here is the complete code as I ran it:

/*
    This Sketch will generate a variable volatage ramp signal 
    simultaneous to a syncronized *boxed* square wave.
    //----------------------------------------------------//
    Output: PP 7; P2 of ATTiny85 to a 'Low Pass Filter'
    Values of cap & resistor to be calculated according to 
    roll-off frequency appropriate for working PWM freq. 

    ARDUINO_ATTiny ---/\/\/\--- | ---- INPUT COMPARATOR
                              |
                             ---
                             ---
                              |
                              |
                            GROUND 
                           
   This will produce a *nice* voltage ramp/saw tooth waveform.
   Can be made *variable* by substituting appropriate potentio-
   meter for resistor.   
*/
//////////////////////////////////

#include <stdlib.h>
#include <avr/io.h>           // Adds useful constants


/// Define Variables ////

int pwmMin=30;               // minimum pwm voltage (0-255)
int pwmMax=255;               // maximum pwm voltage (0-255)
int ramplength=4;            // us
int INTERVAL=30000;          // the repeat delay interval; ms.
int delayStartTime;
int rampDown;
int ramplengthpause;
static uint32_t StartUs=micros();
static uint32_t StartMs=millis();
static uint8_t Pwm=0;
//static uint8_t Dir=1;

/// Define pins for ATTiny85  ///

volatile int outPin = 0;             // PhysicalPin 5; P0
int ledPin = 1;                      // PP 6; P1   
int pwmMaxPin = 1;                   // PP 7; P2           Sets Max Ramp hieght.
int ramplengthPin = 2;               // PP 3; P4           Sets Ramp length / Mark Time.
int intervalPin = 3;                 // PP 2; P3           Sets DeadTime inbetween Ramp signals.



///////////////////////////////////////////////////
void setup()
{
    
    /*
    Control Register A for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
    TCCR0A is 8 bits: [COM0A1:COM0A0:COM0B1:COM0B0:unused:unused:WGM01:WGM00]
    2<<COM0A0: sets bits COM0A0 and COM0A1, which (in Fast PWM mode) clears OC0A on compare-match, and sets OC0A at BOTTOM
    2<<COM0B0: sets bits COM0B0 and COM0B1, which (in Fast PWM mode) clears OC0B on compare-match, and sets OC0B at BOTTOM
    3<<WGM00: sets bits WGM00 and WGM01, which (when combined with WGM02 from TCCR0B below) enables Fast PWM mode
    */
    TCCR0A = 2<<COM0A0 | 2<<COM0B0 | 3<<WGM00;
   
    /*
    Control Register B for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
    TCCR0B is 8 bits: [FOC0A:FOC0B:unused:unused:WGM02:CS02:CS01:CS00]
    0<<WGM02: bit WGM02 remains clear, which (when combined with WGM00 and WGM01 from TCCR0A above) enables Fast PWM mode
    1<<CS00: sets bits CS01 (leaving CS01 and CS02 clear), which tells Timer/Counter-0 to not use a prescalar
    */
    TCCR0B = 0<<WGM02 | 1<<CS00;
   
    /*
    Control Register for Timer/Counter-1 (Timer/Counter-1 is configured with just one register: this one)
    TCCR1 is 8 bits: [CTC1:PWM1A:COM1A1:COM1A0:CS13:CS12:CS11:CS10]
    0<<PWM1A: bit PWM1A remains clear, which prevents Timer/Counter-1 from using pin OC1A (which is shared with OC0B)
    0<<COM1A0: bits COM1A0 and COM1A1 remain clear, which also prevents Timer/Counter-1 from using pin OC1A (see PWM1A above)
    1<<CS10: sets bit CS11 which tells Timer/Counter-1  to not use a prescalar
    */
    TCCR1 = 0<<PWM1A | 0<<COM1A0 | 1<<CS10;
   
    /*
    General Control Register for Timer/Counter-1 (this is for Timer/Counter-1 and is a poorly named register)
    GTCCR is 8 bits: [TSM:PWM1B:COM1B1:COM1B0:FOC1B:FOC1A:PSR1:PSR0]
    1<<PWM1B: sets bit PWM1B which enables the use of OC1B (since we disabled using OC1A in TCCR1)
    2<<COM1B0: sets bit COM1B1 and leaves COM1B0 clear, which (when in PWM mode) clears OC1B on compare-match, and sets at BOTTOM
    */
    GTCCR = 1<<PWM1B | 2<<COM1B0;
    
    pinMode(outPin, OUTPUT);
    pinMode(ledPin, OUTPUT); 
    
  
}


//////////////////////////////////////////////////////////
void loop()
{
  int static Dir=1; 
  digitalWrite(outPin,HIGH); 
  ramplengthpause=(ramplength/3);
  if((Dir==-1) && (Dir != +1)) 
       {
         digitalWrite(outPin,LOW);
         delay(ramplengthpause);
       }
  
  /*do
          {
            digitalWrite(outPin,LOW);
          }
            while((Dir==-1) && (Dir != +1));
  */
  if((micros()-StartUs) >= ramplength) 
      {                                           
        Pwm+=Dir;                 //increment or decrement PWM depending of sign of Dir 
        analogWrite(ledPin, Pwm); //Update built-in LED
        if(Pwm==pwmMax) Dir=-1;   //if PWM reaches the maximum: change direction 
       
       if(Pwm==pwmMin) 
        {                                         
          delay(INTERVAL);
          Dir=+1;                 //delay *INTERVAL* & change direction 
          delayStartTime = micros();
              do
              {
                INTERVAL   = map(analogRead(intervalPin),0,1023,800,50000);               //800=16ms          // adjust all maps according to
                pwmMax     = map(analogRead(pwmMaxPin),0,1023,100,255);                    // circuit layout & potentiomenters
                ramplength = map(analogRead(ramplengthPin),0,1023,2,11800);               // adjust map for packetcount
              }
              while(micros()-delayStartTime<=INTERVAL);
              
         }                        
          StartUs += ramplength;  //loose control of ramplengthPin & pwmMaxPin // only intervalPin works // with int Dir=2; min 4ms
                                  //loose control of ramplengthPin  -- pwmMaxPin && intervalPin works  // with int static Dir=1; 
         
        //StartUs=micros();         //loose control of pwmMaxPin // ramplenghtPin & intervalPin works      // with int Dir=2; min 4ms
  }                                 // All three work!!!! but....now Triangle wave!!!!!                   // with int static Dir=1; 
  
}

What am I missing?
What should I try next?

Thanks again for your time.
take care, peace
lost_bro