I have this code (I simplified it a bit) that get stuck if you leave it with bias = sum, while it runs if you uncomment bias = 99.123
It is part of a random number generator, this code is taken from a C linux program .. it runs just fine on arduino UNO, the only difference here is SoftwareSerial that is used on the attiny85.
Maybe I'm using too much memory or there is some programming error ?
Thank you,
Robse
#include <SoftwareSerial.h>
SoftwareSerial mySerial(1, 2); // RX, TX
int avalanchePin = 3;
void setup() {
pinMode(avalanchePin, INPUT);
mySerial.begin(9600);
delay(1000);
mySerial.println("I Begin");
}
void loop() {
unsigned long i, j, k, m;
double sum, bias, avgbias, vbias[20];
uint8_t avalancheBit, block[1000];
char mybuf[100];
char biasbuf[10];
char avgbiasbuf[10];
i = 0;
j = 0;
m = 0;
bias = 0;
for(k = 0; k< 20; k++)
vbias[k] = 0;
while (true) {
avalancheBit = digitalRead(avalanchePin);
block[j++] = avalancheBit;
if (j == 1000) {
j = 0;
sum = 0.0;
for (k = 0; k < 1000; k++)
sum += block[k];
bias = sum;
//bias = 99.123;
dtostrf(bias, 2, 2, biasbuf);
sprintf(mybuf, "bits: %8ld bias: %s%%\n", i+1, biasbuf);
mySerial.print(mybuf);
}
delayMicroseconds(500);
i++;
}
I would expect you to get an error in the compile process saying you're out of ram, unless you are using a very old version of the ide that doesnt check ram usage.
I think that when you replace that value with a constant, the compiler is optimizing out unused variables, including that huge array
You don't get a message because it is not a global variable, it is an automatic variable.
If you define it outside loop() you'll get an error.
The compiler can't total up the automatic variables because only at runtime is it clear which are being used simultaneously.
What is the smallest way to store 1000 bits ? that block[1000] is going to store DigitalRead() that gives 0/1 ... I tried to declare it bool but it gets stuck anyway
Maybe i can work without that block, I have to review the code
robse:
What is the smallest way to store 1000 bits ? that block[1000] is going to store DigitalRead() that gives 0/1 ... I tried to declare it bool but it gets stuck anyway
Maybe i can work without that block, I have to review the code
125 bytes is the smallest way to store 1000 bits. But then you have to take responsibility for packing the 8 separate bits into a byte and unpacking them afterwards. Look at bitRead() and bitWrite() to treat a byte as 8 separate bits.
Some other things you can do. Program the chip with a uno as isp programmer that way you save 2000 bytes or so of flash for your program, since you will not need the bootloader.
Use Nick Gammons Send only software serial. It will save you more flash and uses only 1 pin on your tiny.
Place your constants in flash (your output messages).
bool is just a byte where only the low bit is used, so you don't save memory by using bool instead of byte.
If you manually handle the packing of bits into bytes, you could switch to a 125-member array to store 1000 bits - though you'd have some overhead associated with that, it would probably not be much. The packing and unpacking can be handled easily with the bitshift and bitwise operators (eg, >>, <<, &, and | )