Hi All,
I have a quite simple code which is to big for an Attiny13 (max, 1024 bytes).
#include <math.h>
int piezo_pin = 3; // the number of the input pin
int FET_pin = 10; // the number of the output pin 0
int LED_pin = 7; // the number of the control led pin 1
int maxTemp = 28; // the maximum allowed FEt temperature
int Nfet_TC_pin = A1; // the number of the N-FET thermistor
int Nfet_thermistor_adc_val; // N-FET thermistor read value
double N_output_voltage; // N-FET thermistor output voltage
double N_thermistor_resistance; // N-FET thermistor resistance
double N_therm_res_ln; // N-FET thermistor log resistance
double N_temperature; // N-FET temperature
int Pfet_TC_pin = A2; // the number of the P-FET thermistor
int Pfet_thermistor_adc_val; // P-FET thermistor read value
double P_output_voltage; // P-FET thermistor output voltage
double P_thermistor_resistance; // P-FET thermistor resistance
double P_therm_res_ln; // P-FET thermistor log resistance
double P_temperature; // P-FET temperature
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// the follow variables are long's because the time, measured in miliseconds,
unsigned long previousMillis = 0; // the last time the output pin was toggled
unsigned long currentMillis = 0; // current time in ms
const long debounce = 50; // the debounce time, increase if the output flickers
void setup()
{
pinMode(piezo_pin, INPUT);
pinMode(FET_pin, OUTPUT);
pinMode(LED_pin, OUTPUT);
pinMode(Nfet_TC_pin, INPUT);
pinMode(Pfet_TC_pin, INPUT);
}
void loop()
{
// N-FET thermistor temperature calculation
Nfet_thermistor_adc_val = analogRead(Nfet_TC_pin);
N_output_voltage = ( (Nfet_thermistor_adc_val * 5.0) / 1023.0 );
N_thermistor_resistance = ( ( 5 * ( 10.0 / N_output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
N_thermistor_resistance = N_thermistor_resistance * 1000 ; /* Resistance in ohms */
N_therm_res_ln = log(N_thermistor_resistance);
/* Steinhart-Hart Thermistor Equation: */
/* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3) */
/* where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8 */
N_temperature = ( 1 / ( 0.001129148 + ( 0.000234125 * N_therm_res_ln ) + ( 0.0000000876741 * N_therm_res_ln * N_therm_res_ln * N_therm_res_ln ) ) ); /* Temperature in Kelvin */
N_temperature = N_temperature - 273.15; /* Temperature in degree Celsius */
//delay(100);
// P-FET thermistor temperature calculation
Pfet_thermistor_adc_val = analogRead(Pfet_TC_pin);
P_output_voltage = ( (Pfet_thermistor_adc_val * 5.0) / 1023.0 );
P_thermistor_resistance = ( ( 5 * ( 10.0 / P_output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
P_thermistor_resistance = P_thermistor_resistance * 1000 ; /* Resistance in ohms */
P_therm_res_ln = log(P_thermistor_resistance);
/* Steinhart-Hart Thermistor Equation: */
/* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3) */
/* where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8 */
P_temperature = ( 1 / ( 0.001129148 + ( 0.000234125 * P_therm_res_ln ) + ( 0.0000000876741 * P_therm_res_ln * P_therm_res_ln * P_therm_res_ln ) ) ); /* Temperature in Kelvin */
P_temperature = P_temperature - 273.15; /* Temperature in degree Celsius */
// switch when piezzo button is oushed
currentMillis = millis();
if (currentMillis - previousMillis >= debounce){
previousMillis = currentMillis;
// read the pushbutton input pin:
buttonState = digitalRead(piezo_pin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
// turns on the heating at every two button pushes
if (buttonPushCounter == 0){
analogWrite(FET_pin, 0);
analogWrite(LED_pin, 0);
}
//only switches on if the temperature is lower then the threshold
if (buttonPushCounter == 1){
analogWrite(FET_pin,255);
analogWrite(LED_pin,255);
}
if (buttonPushCounter == 2){
buttonPushCounter=0;
}
//swith off heating if temperature threshold is reached
if (buttonPushCounter == 1 and (N_temperature > maxTemp or P_temperature > maxTemp)){
for (int i = 0; i <= 10; i++) {
analogWrite(LED_pin, 0);
delay(100);
analogWrite(LED_pin, 255);
delay(100);
}
buttonPushCounter=0;
}
//swith off heating in case of thermistor error
if (buttonPushCounter == 1 and (N_temperature < 0 or P_temperature < 0)){
for (int i = 0; i <= 10; i++) {
analogWrite(LED_pin, 0);
delay(500);
analogWrite(LED_pin, 255);
delay(500);
}
buttonPushCounter=0;
}
}
Sketch uses 2380 bytes (232%) of program storage space. Maximum is 1024 bytes.
Global variables use 20 bytes (31%) of dynamic memory, leaving 44 bytes for local variables. Maximum is 64 bytes.
The strange thing is that tha last 2 if statements makes to code too big.
If I remove these lines from the code, it needs only 438 bytes
//swith off heating if temperature threshold is reached
if (buttonPushCounter == 1 and (N_temperature > maxTemp or P_temperature > maxTemp)){
for (int i = 0; i <= 10; i++) {
analogWrite(LED_pin, 0);
delay(100);
analogWrite(LED_pin, 255);
delay(100);
}
buttonPushCounter=0;
}
//swith off heating in case of thermistor error
if (buttonPushCounter == 1 and (N_temperature < 0 or P_temperature < 0)){
for (int i = 0; i <= 10; i++) {
analogWrite(LED_pin, 0);
delay(500);
analogWrite(LED_pin, 255);
delay(500);
}
buttonPushCounter=0;
}
avrdude: 438 bytes of flash written
I do not see any reason why these few lines needs much more space than the whole code itself.
Any suggestions?
As a final solution I will use an Attiny85.
