 # Attiny85 how to questions , how to write if statements between functions?

Im new to the whole programming game. I created two functions. one monitors the temperature the second is a switch case for an led (ie modes: HI, MED, LO) the purpose of the program is to keep the temperature of the led below a certain point regardless of the mode its in.

how can i create an if statement that will work?

The statement im using now doesnt work i get an error that say steinhart was not declared in this scope.

``````//date 5/6/2015 4pm

// which analog pin to connect
#define THERMISTORPIN A1
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 11
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3380
// the value of the 'other' resistor
#define SERIESRESISTOR 10000

int samples[NUMSAMPLES];
int counter = 0;
int switchPin = 1;
int LED = 0; // handlebar led high

void setup() {
// put your setup code here, to run once:
pinMode(switchPin, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

Temp();
Mode();
}

void Mode() {
if(switchVal == HIGH)
{
delay(350);
counter ++;

//Reset count if over max mode number
if(counter >= 4)
{
counter = 0;
}
}else{

//Change mode
switch (counter)
{
case 0: // HI Mode
if (steinhart > 30)
{
analogWrite(LED,10);
}else{
if (steinhart < 28) // ramp light on after this temp
analogWrite(LED,255);
}
break;

case 1: // MED Mode
if (steinhart > 30)
{
analogWrite(LED,10);
}else{
if (steinhart < 28) // ramp light on after this temp
analogWrite(LED,155);
}
break;

case 2: // LO Mode
if (steinhart > 30)
{
analogWrite(LED,10);
}else{
if (steinhart < 28) // ramp light on after this temp
analogWrite(LED,85);
}
break;

case 3: //off
analogWrite(LED,0);
break;
}
}
}

void Temp () {
// take N samples in a row, with a slight delay
uint8_t i;
float average;
for (i=0; i< NUMSAMPLES; i++) {

delayMicroseconds(1000);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;

// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;

float steinhart;
steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
steinhart = log(steinhart);                  // ln(R/Ro)``
steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart;                 // Invert
steinhart -= 273.15;                         // convert to C

}
``````

You declared steinhart (as a float) in Temp but you tried to use it in Mode. That is bad.

One way out: Move the line

`````` float steinhart;
``````

to a position right after

``````    int samples[NUMSAMPLES];
int counter = 0;
int switchPin = 1;
int LED = 0; // handlebar led high
``````

but before setup.

A better way out: Mode should return a float (instead of a void) whose value is steinhart. Temp should take an input parameter, a float, which should be the value returned by Mode and which should be stored into the local value of steinhart (which needs definition). Just one problem: You call Temp, followed by Mode, and not the other way around. What value do you expect steinhart to have if you haven't called Mode yet?

moved float steinhart above the int. and moved the mode above the temp. The code compiled without any errors :)

you totally lost me here:

Temp should take an input parameter, a float, which should be the value returned by Mode and which should be stored into the local value of steinhart (which needs definition).

I have no clue how to implement that.

also,

how could you implement an interrupt with the attiny for the switch? whenever i test the program on the circuit board the push button stops working.

all the examples for attiny interupts are written in byte format that i do not understand