ATtiny85 and time lag (UNSOLVED 25-nov)

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi! I´m Manuel from Argentina. Sorry if i fail with my inglish.

I´m new in this world of AVR PICs and i´ve a problem with an Attiny85.

My project, an automated irrigation crop, activates two sensors each 30 minutes and this is the problem, each turn of the loop is delayed 30 seconds, ergo 08:30:00 > 08:59:30 > 09:29:00 > 09:58:30...

Investigating, I learned about the basic internal structure of a PIC, its peripherals, Von Neumann or Harvard architecture, and the timers / counters ...

So far I have programmed ATtiny through an arduino UNO and its IDE, never touch what would be to modify prescaler or something internal to the PIC, so ...
Could someone help me fix this time lag?

Thank you very much also for reading me!

/*
  o D0: Accionamiento de alimentación de los sensores cada 15 minutos.
  o D1: Accionamiento de la bomba de agua cuando se cumplan las condiciones de baja humedad y luminosidad.
  o D2: Lectura del sensor de humedad.
  o D3: Lectura del sensor de luminosidad.
  o A2: Potenciometro para elegir nivel de riego.

  Sensor de luz... 1 = hay luz // 0 = no hay luz.
  Sensor de humedad... 1 = no hay agua // 0 = hay agua.

  NOTA: conectar el pote mirandolo de frente... izq GND, centro ANALOG, derecha VCC
*/
// PINES a utilizar.
int enableSensors = 0, enablePump = 1, sensorHum = 2, sensorLight = 3, pote = 2;

// VARIABLES PARA MILLIS.
unsigned long elapsedTime = 0;
int valorPote;

void setup() {
  Serial.begin(9600);
  pinMode(enableSensors, OUTPUT);
  pinMode(enablePump, OUTPUT);
  pinMode(sensorHum, INPUT);
  pinMode(sensorLight, INPUT);

  digitalWrite(enablePump,LOW);
  digitalWrite(enableSensors, HIGH);
  delay(5000);
  digitalWrite(enableSensors, LOW);
}
void loop() {
  valorPote = 0;
  elapsedTime = millis() * 0.001;
  if (temp(30 * 60)) {
    digitalWrite(enableSensors, HIGH);
    delay(5000);
    int estadoAgua = digitalRead(sensorHum);
    int estadoLuz = digitalRead(sensorLight);
    if (estadoAgua == 1 && estadoLuz == 0) {

      for (int i = 0; i < 10; i++) {
        valorPote += analogRead(pote);
      }
      valorPote = valorPote / 10;
      // Valor MIN:0 MAX:1023

      if (Rango(valorPote, 0, 170)) {
        digitalWrite(enablePump, HIGH);
        delay(5000);
        digitalWrite(enablePump, LOW);
      } else if (Rango(valorPote, 171, 340)) {
        digitalWrite(enablePump, HIGH);
        delay(10000);
        digitalWrite(enablePump, LOW);
      } else if (Rango(valorPote, 341, 510)) {
        digitalWrite(enablePump, HIGH);
        delay(15000);
        digitalWrite(enablePump, LOW);
      } else if (Rango(valorPote, 511, 680)) {
        digitalWrite(enablePump, HIGH);
        delay(20000);
        digitalWrite(enablePump, LOW);
      } else if (Rango(valorPote, 681, 850)) {
        digitalWrite(enablePump, HIGH);
        delay(35000);
        digitalWrite(enablePump, LOW);
      } else if (Rango(valorPote, 851, 1023)) {
        digitalWrite(enablePump, HIGH);
        delay(40000);
        digitalWrite(enablePump, LOW);
      }
    } else digitalWrite(enablePump, LOW);
  } else digitalWrite(enableSensors, LOW);

  delay(15);
}

bool temp (int seg) {
  if (elapsedTime > 1) {
    if (elapsedTime % seg == 0) return true;
    else return false;
  } else {
    return false;
  }

 

Last Edited: Thu. Nov 25, 2021 - 03:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Note, If you are using the internal oscillator, your timings can be a wee little bit off (but maybe 1% or 850 seconds per day).  You can calibrate the internal time base to make it better.

But there are likely things to do to your program first to make the timing better.

 

Also, by the way, having a huge delay is not a good idea, since nothing can happen while waiting around (like the system is frozen).....much better to use things like timers and check when time is up to do the next thing.

Then while you wait, you can check buttons, blink leds, sound a buzzer, do many other things, .......check the time again and again...take action when it is time to do so. Time's up!!

There is a fancier method, called interrupts, that does all the checking for you, but that is not for beginners.

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Welcome to AVRFreaks!

 

You can have 30 min timer if you use WDT timer set for 4 s.

When you collect 450 ISRs, it is 30 min elapsed.

If you are not satisfied, do change 450 to be 449, or 451, or whatever.

The chip may "sleep" between ISRs.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Try doing an integer divide instead of a floating point multiply::

  elapsedTime = millis() / 1000;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

elapsed, triggered a memory about a lib for that, yes, install elapsedMillis

here is the wiki on how to use for periodic events like you want to do: https://github.com/pfeerick/elap...

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Sorry had to move the post twice.

 

As you are new please DO NOT post in the Projects forum as it is for finished project and not to ask question.

 

Also please do not call an AVR a PIC, it's blasphemy even though they are now made by the same company. smiley

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Also please do not call an AVR a PIC, it's blasphemy even though they are now made by the same company. smiley

 

If you misbehave, you may get a PICAXE in the mail

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Note, If you are using the internal oscillator, your timings can be a wee little bit off (but maybe 1% or 850 seconds per day).  You can calibrate the internal time base to make it better.

But there are likely things to do to your program first to make the timing better.

 

Also, by the way, having a huge delay is not a good idea, since nothing can happen while waiting around (like the system is frozen).....much better to use things like timers and check when time is up to do the next thing.

Then while you wait, you can check buttons, blink leds, sound a buzzer, do many other things, .......check the time again and again...take action when it is time to do so. Time's up!!

There is a fancier method, called interrupts, that does all the checking for you, but that is not for beginners.

 

 

so how can i calibrate it? modifying the prescaler, counter or what? optimizing the code?

When you say delay, do you mean the use of millis to know how often I spend 30 minutes?

really the project does not have more than every 30 minutes to see the sensors and make the decision whether or not to water, meanwhile there is no button, buzzer or anything else to do.

thanks for the reply AVRCANDIES!

 

grohote wrote:

Welcome to AVRFreaks!

 

You can have 30 min timer if you use WDT timer set for 4 s.

When you collect 450 ISRs, it is 30 min elapsed.

If you are not satisfied, do change 450 to be 449, or 451, or whatever.

The chip may "sleep" between ISRs.

 

 

Thank you for the welcome Grohote!

to configure everything that is internally of the microcontroller, can i do it through an arduino UNO?

ISRs 450, 449, 451... I think I will have to continue researching on these things d:

 

N.Winterbottom wrote:

Try doing an integer divide instead of a floating point multiply::

  elapsedTime = millis() / 1000;

 

Hi Winterbotton! I already tried converting millis to whole seconds,
joint use of millis-delays and so on ... there is still a time lag after a while
Thanks anyway!

 

ki0bk wrote:

elapsed, triggered a memory about a lib for that, yes, install elapsedMillis

here is the wiki on how to use for periodic events like you want to do: https://github.com/pfeerick/elap...

 


Thanks ki0bk! I'll try!

 

js wrote:

Sorry had to move the post twice.

 

As you are new please DO NOT post in the Projects forum as it is for finished project and not to ask question.

 

Also please do not call an AVR a PIC, it's blasphemy even though they are now made by the same company. smiley

 

Sorry! first time!

I was looking in which section to place it and in "arduino" I did not place it since it did not directly use an Arduino board and as it was a project I thought it was the right thing to do in "projects"
I take the opportunity to ask, is not a microcontroller a programmable integrated circuit ... I mean PIC? I've only been with all this for half a year, sorry!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it did not directly use an Arduino board

But the code is Arduino based so you must be using the Arduino framework.

 

I mean PIC

PICs are a series of (inferior) micro controllers manufactured by Microchip who now also owns the Atmel/AVR company. Long standing wars between PICs and AVRs. cheeky

 

EDIT by the way are the plants pumpkins of some kind? I had around 100 plants growing in my garden from seeds in the compost, only kept a few.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Thu. Nov 18, 2021 - 11:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

PICs are a series of (inferior) micro controllers manufactured by Microchip who now also owns the Atmel/AVR company. Long standing wars between PICs and AVRs.

 

EDIT by the way are the plants pumpkins of some kind? I had around 100 plants growing in my garden from seeds in the compost, only kept a few.

 

thanks for the clarification, so for example the ATtiny i should call it a microcontroller and never a pic wink and yes, 5 pumpkin plants, in Argentina we call it "calabaza" and this variety in particular "moschata" or "anco".

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Sounds like "Butternut pumpkin" and that's what I think I have, it's a mystery plant so far. And yes PIC is a swear word here. devil

 

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


 

It is Butternut pumpkin, everybody like it, see those of mine-- I prefer a small size "moschata".

/sorry for the picture quality, but the pumpkin is exquisite/

 

 

Last Edited: Fri. Nov 19, 2021 - 05:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

So 30 seconds error in 30 minutes is 1.67%. That's way beyond the normal error for a crystal, ~Tell us about your choice of clock source.

 

In practise however this doesn't matter much - Your plants will get 1.67% less watering time than you set via the potentiometer. Not a big deal.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

ManoloVC wrote:
to configure everything that is internally of the microcontroller, can i do it through an arduino UNO?

 

Use of WDT is possible on all AVRs, including Arduino-based. Sorry, I am not competent on them.

 

When WDT, you may choose a "deep sleep" which is Power Down-- it is most wanted on battery-powered projects.

So, you sleep, do wakeup each 4s, and after 450 of them --aha-- it is the time of action.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

So 30 seconds error in 30 minutes is 1.67%. That's way beyond the normal error for a crystal, ~Tell us about your choice of clock source.

 

In practise however this doesn't matter much - Your plants will get 1.67% less watering time than you set via the potentiometer. Not a big deal.

 

 

I did not change the clock setting, which is an 8Mhz internal clock!

forgiveness for slow to respond