1

The following code works fine until I turn the if (tijd - action >= 500) into if (tijd - action >= 1000)

I wonder why this fails.

If I just loop and print the tijd - action it has no problem with it and runs well beyond 1000.

The Logic Analyzer on the software serial only returns '255' when it fails, I also wonder what that means.

The core I use is this one
The chip I use is an ATtiny85
I run this using the Arduino IDE 1.8.2

[update]

with "start"

Sketch uses 3070 bytes (37%) of program storage space. Maximum is 8192 bytes.
Global variables use 146 bytes (28%) of dynamic memory, leaving 366 bytes for local variables. Maximum is 512 bytes.

with "starting"

Sketch uses 3072 bytes (37%) of program storage space. Maximum is 8192 bytes.
Global variables use 148 bytes (28%) of dynamic memory, leaving 364 bytes for local variables. Maximum is 512 bytes.

script

#include <SoftwareSerial.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

#define TX_PIN 4
#define RX_PIN -1

#ifndef cbi
  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
  #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

SoftwareSerial mySerial(RX_PIN, TX_PIN);

unsigned long tijd;
unsigned long action = 0;

volatile boolean f_wdt = 1;
ISR(WDT_vect) {
  f_wdt=1;
}

void system_sleep() {
  cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF

  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  sleep_enable();

  sleep_mode();                        // System sleeps here

  sleep_disable();                     // System continues execution here when watchdog timed out
  sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
}

void setup_watchdog(int ii) {
  byte bb;
  int ww;
  if (ii > 9 ) ii=9;
  bb=ii & 7;
  if (ii > 7) bb|= (1<<5);
  bb|= (1<<WDCE);
  ww=bb;

  MCUSR &= ~(1<<WDRF);
  // start timed sequence
  WDTCR |= (1<<WDCE) | (1<<WDE);
  // set new watchdog timeout value
  WDTCR = bb;
  WDTCR |= _BV(WDIE);
}

void setup() {
  mySerial.begin(2400);
  mySerial.print("Starting..");
  setup_watchdog(8);
  pinMode(0, OUTPUT);
}

int zie = 0;

void loop() {
  if (f_wdt==1) { 
    digitalWrite(0, HIGH);
    tijd = millis();
    mySerial.print(".");
    mySerial.print(tijd - action);
    zie+=1;
    if (tijd - action >= 500) { // fails when set to 1000
      digitalWrite(0, LOW);
      action = tijd;
      mySerial.print("z");
      mySerial.print(zie);
      mySerial.print(".");
      system_sleep();
    }
  }
}
9
  • Which ATtiny are you using? Commented Apr 26, 2017 at 9:37
  • the ATtiny85 is what I use
    – Thijs
    Commented Apr 26, 2017 at 9:39
  • Try Serial.println(tijd) before your comparison. See how many ms it takes till it gets to comparison. maybe it is bigger than 500, but lower than 1000
    – user33153
    Commented Apr 26, 2017 at 10:23
  • You mean printing millis() before comparison right, because tijd is fixed at the start..
    – Thijs
    Commented Apr 26, 2017 at 11:37
  • 1
    You will not be believe this; if I change the string "Starting.." to "Start" it will run fine! This probably has something to do with filling up the RAM of the Tiny, right?
    – Thijs
    Commented Apr 26, 2017 at 12:44

1 Answer 1

0

if I change the string "Starting.." to "Start" it will run fine! This probably has something to do with filling up the RAM of the Tiny, right?

That symptom does suggest RAM overflow, in spite of the relatively low RAM use (28%) reported during compilation.

To reduce memory use slightly more, you can move the string "Starting.." to PROGMEM by changing from line 1 below to line 2.

  mySerial.print("Starting..");     // line 1
  mySerial.print(F("Starting.."));  // line 2

To save another handful of RAM bytes, change lines like mySerial.print("."); and mySerial.print("z"); to mySerial.print('.'); and mySerial.print('z');. That is, you are printing single-character strings; each string has a null terminating byte; instead just print single characters.

References and discussion of the F() macro include 1, 2, 3, 4, 5 and also see How do I squeeze code to fit onto an ATTiny. Also, there are some really compact software serial libraries targeted for ATtinies, which may save hundreds of bytes of flash and dozens of bytes of RAM.

4
  • I'm not going to disagree but I'd be interested to understand why its happening at such a low threshold. (unless the values reported are [rubbish]) Commented Apr 26, 2017 at 15:19
  • Adding the F already does the trick. I would like to know why this is a solution. I still have a library to add to this code, I will probably run into the same trouble again..
    – Thijs
    Commented Apr 26, 2017 at 15:31
  • 1
    @CodeGorilla, that I don't know, which is why I say “does suggest RAM overflow” instead of proving it. I've seen somewhat larger ATtiny85 programs, and don't know what's wrong here. Answers to How do I squeeze code to fit onto an ATTiny might suggest an approach Commented Apr 26, 2017 at 15:33
  • 1
    @Thijs, I added some F() links, but as noted I don't know what's wrong. Perhaps read the assembly code looking for surprises. Commented Apr 26, 2017 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.