cycle and PORTD

int potPin    = 0;
int lastpot, delayPot;
byte  kan6[] =   {B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101};

void setup() {
  // put your setup code here, to run once:
  pinMode(potPin, INPUT);
  DDRD  = DDRD  | B11111111;
}

void loop() {
  // put your main code here, to run repeatedly:
  lastpot = analogRead(potPin);
  delayPot  = map(lastpot, 0, 1023, 600, 6);
  for (int j = 0; j <= 23; j=j+1)
  {
    PORTD = kan6[j];
    delayMicroseconds(delayPot );

  }

}

Hello. How to get an even signal?

SergV:

int potPin    = 0;

int lastpot, delayPot;
byte  kan6[] =  {B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101, B10101010, B01010101};

void setup() {
  // put your setup code here, to run once:
  pinMode(potPin, INPUT);
  DDRD  = DDRD  | B11111111;
}

void loop() {
  // put your main code here, to run repeatedly:
  lastpot = analogRead(potPin);
  delayPot  = map(lastpot, 0, 1023, 600, 6);
  for (int j = 0; j <= 23; j=j+1)
  {
    PORTD = kan6[j];
    delayMicroseconds(delayPot );

}

}




Hello. How to get an even signal?

What do you mean?

Please don't waste your valuable time looking for this thread's twin; I deleted it.

Do not cross-post, cross-posting wastes time.

How to get rid of this delay? I think this delay = cycle time. how to speed up the cycle? output signal is not balanced

SergV:
How to get rid of this delay?

Stop timer 0.

please write more . I do not understand how to do.

(deleted)

Well, every 23 inner loop cycle, you fall into the outer loop, that does an analogRead(), which is a pretty slow function...

westfw:
Well, every 23 inner loop cycle, you fall into the outer loop, that does an analogRead(), which is a pretty slow function...

how to replace analogRead ()?

With what do you want to replace analogRead()?

SergV:
how to replace analogRead ()?

The best way is to not use the analog to digital converter at all, but then it's awfully handy when you need it!

So if you really, really need it, you can speed up the timer that the ADC uses. Try this in setup (untested):

  ADCSRA &= ~((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));
  ADCSRA |= (1 << ADPS2);

Now analogRead() should be several times faster.

Like me, you're a budding registerian, so why does this do what it does?

ChrisTening. thank.
removed analogRead (). The picture on the oscilloscope is better, but asymmetry (delay) is also visible. I will study datasheet

Isn't it possible to set the ADC in free running mode, and then just read the register when you need it? That way the conversions should happen in the background, not affecting the sketch itself.

Alternatively: instead of the for loop set a timer interrupt that at fixed intervals calls a function that sets the pin patterns. That should also do the job of getting a more regular output pattern.

wvmarle:
Isn't it possible to set the ADC in free running mode, and then just read the register when you need it?

Maybe. (That contraction at the front of your sentence makes a definitive answer awkward.)

Yes, it is possible to set the ADC in free running mode then just read the current value at will. I recall it is not particularly difficult (i.e. set a bit or two then fire it up). It is also not troublesome when dealing with a single channel.

[quote author=Coding Badly link=msg=3920743 date=1540543456]
Maybe. (That contraction at the front of your sentence makes a definitive answer awkward.)[/quote]

That's because I wasn't sure myself - heard about the option (make it do conversions all the time - can also trigger an interrupt when a conversion is done), never used it.