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();
}
}
}
millis()
before comparison right, because tijd is fixed at the start.."Starting.."
to"Start"
it will run fine! This probably has something to do with filling up the RAM of the Tiny, right?