How to change millis() timer

Is there a not so complicated way to change millis() timer from timer 0 to 1 or 2 on the atmega168? I'm using timer 0 to get the fastest PWM possible but this makes timing functions nearly unusable.

Why not use Timer1 or Timer2 for your PWM?

...R

Because they work with half the frequency... I could use, but It would be nice to know if there’s a way to count millis with the 8 bit timers.

You can change the PWM frequency.

Yes I know, I’m doing that already, timer 0 has a 60Khz max and timer 1 and 2 have a 30Khz... But anyways, I haven’t found any answers on google about my question. It would be nice if someone explained it.

caiopoit:
But anyways, I haven’t found any answers on google about my question. It would be nice if someone explained it.

My guess is the reason that you have not found any answers is because nobody bothers to do that when there are easier ways using the other Timers.

...R

Ok then, if somebody ever finds out how to do that I hope they share it.

I wouldn't try to change the millis() function as you would break all other sketches.

You can make your own timing function using any timer you like.
but you will have to learn to use them directly. AVR timer

make your own timing function

that seems like a good solution, I use my own and disable Arduino’s....

caiopoit:
that seems like a good solution, I use my own and disable Arduino’s....

What would be gained by disabling the Arduino standard features?

I can't help feeling you are suffering from a major misconception about how to solve your problem.

If you tell us what your project is all about it will a great deal easier to give useful advice. At the moment this is an XY problem.

...R

You can get faster open than that with timer 1 or 2. Look at the datasheet - the only reason timer0 has higher frequency all else being equal is that it's set to fast pwm mode instead of phase correct; you can just set timer 1 or 2 to that wgm. You can get much faster pwm than that with the wgm that lets you set TOP, hence make it count to less than 255 before overflow

hence make it count to less than 255 before overflow

wouldn’t that reduce the resolution of the PWM?

Yes, but since you've told us nothing about your project, we can't make useful suggestions.

Why would you need 8-bit resolution?

Here it is… It’s a wake up light and it works.

#include <Wire.h>
#include <TimeLib.h>
#include <DS3232RTC.h>
#define LightLevelPin 6
#define LightOffPin 3
#define AlarmPin 2
#define IndicatorLED 13
double LightLevel = 0;
double Base = 0.0039215;
byte AlarmH = 06;
byte AlarmM = 5;     // Set 30 minutes before wake time

void wakeshining();

void setup() {

  pinMode(LightLevelPin, OUTPUT);
  pinMode(IndicatorLED, OUTPUT);
  pinMode(AlarmPin, INPUT_PULLUP);
  pinMode(LightOffPin, INPUT_PULLUP);
  //  Serial.begin(9600);
  //  while (!Serial) ;                    // wait for serial
  //delay(200);
  //  RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
  RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
  //  RTC.alarm(ALARM_1);
  RTC.alarm(ALARM_2);
  RTC.alarmInterrupt(ALARM_1, false);
  RTC.alarmInterrupt(ALARM_2, false);
  RTC.squareWave(SQWAVE_NONE);
  RTC.setAlarm(ALM2_MATCH_HOURS, 0, AlarmM, AlarmH, 0);
  RTC.alarm(ALARM_2);
  RTC.squareWave(SQWAVE_NONE);
  RTC.alarmInterrupt(ALARM_2, true);
  attachInterrupt(digitalPinToInterrupt(AlarmPin), waketheboss, FALLING);
  attachInterrupt(digitalPinToInterrupt(LightOffPin), buttonpressed, FALLING);
  setPwmFrequency(LightLevelPin, 1);
}


void loop() {


}

void waketheboss() {
  digitalWrite(IndicatorLED, HIGH);
  wakeshining();

}

void wakeshining() { // I know that this ISR takes half an hour LOL, should be no longer than microseconds.

  while (LightLevel < 255) {                  // 1020 increments up to 255, 1,764 secondsX1020 = 30 mins.
    for (float f = 0; f < 83000; f++) {}       // About 1 sec of delay per line
    for (float f = 0; f < 63470; f++) {}       // About 0,764 sec of delay per line
    analogWrite(LightLevelPin, LightLevel);
    LightLevel = pow(Base, 4) + 1 ;            //y=x^4...
    //    Serial.println(LightLevel);
    Base = Base + 0.0039215;
  }
}

void buttonpressed() {

  digitalWrite(LightLevelPin, LOW);
  digitalWrite(IndicatorLED, LOW);
  RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
  RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
  RTC.alarm(ALARM_1);
  RTC.alarm(ALARM_2);
  RTC.alarmInterrupt(ALARM_1, false);
  RTC.alarmInterrupt(ALARM_2, false);
  RTC.setAlarm(ALM2_MATCH_HOURS, 0, AlarmM, AlarmH, 0);
  digitalWrite(IndicatorLED, LOW);

}


void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if (pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch (divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if (pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if (pin == 3 || pin == 11) {
    switch (divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}

I know I made an ISR that takes half an hour…
The reason I wanted another way to count time is because now I want to upgrade the project and add buttons to adjust the alarm time, show the time in an OLED display and use the light also as a Lamp.
Because I made it initially only interrupt based (not recomended I know), now there is this issue. I think I’ll make my own time counter.

Why would you need 8-bit resolution?

Because 8 bit is already low to simulate sunrise.

caiopoit:
Here it is... It's a wake up light and it works.

What do you mean by "wake up light"?

It sounds like something that does not need greater resolution than +/- 1 minute.

...R

It depends… It doesn’t actually need but it’s quite unpleasant to notice the steps… A wake up light starts shining 30 minutes before your wake up time to stimulate your retina and help synchronize the sleep cycle with the wake up time.

caiopoit:
It depends... It doesn’t actually need but it’s quite unpleasant to notice the steps... A wake up light starts shining 30 minutes before your wake up time to stimulate your retina and help synchronize the sleep cycle with the wake up time.

Getting information from you is like trying to pull teeth.

What steps are you noticing that are unpleasant?

...R

What steps are you noticing that are unpleasant

the 255 steps from the 8 bit timer, that’s why I said that 8 bit was already low.'But it’s fine, less than 8 would be the problem.

...or even if it is a problem. Why would a light on an alarm clock need the "fastest PWM possible"?

caiopoit:
the 255 steps from the 8 bit timer, that's why I said that 8 bit was already low.'But it's fine, less than 8 would be the problem.

This is ridiculous ... it seems like you are being deliberately obstinate. If so I have no intention of wasting any more time on your problem.

"255 steps from the 8 bit timer" is NOT an answer to my question "What steps are you noticing that are unpleasant?"

...R