7 Segment Digital Thermometer using ATtiny 85

 by rahulkar

Step 3: Code:-

#include "LedControl.h" //  need the library
LedControl lc=LedControl(2,1,0,1); // lc is our object
// pin 2 is connected to the MAX7219 pin 1
// pin 1 is connected to the CLK pin 13
// pin 0 is connected to LOAD pin 12
// 1 as we are only using 1 MAX7219
int buttonState=0;         // current state of the button
int lastButtonState=0;     // previous state of the button
const int  buttonPin=4;
int tempPin=3;
float sample;
float tempC;
float tempF;
void setup()
{
  pinMode(buttonPin,INPUT);
  pinMode(tempPin,INPUT);
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,15);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
}
void printNumber(float num)
{
  int ones;
  int tens;
  int hundreds;
  int v=(int)num;
  float diff=num-v;
  diff=diff*100;
  int fones,ftens;
  fones=(int)diff%10;
  diff=diff/10;
  ftens=(int)diff%10;
  diff=diff/10;
  ones=v%10;
  v=v/10;
  tens=v%10;
  v=v/10;
  hundreds=v;  
  //Now print the number digit by digit
  lc.setDigit(0,4,(byte)hundreds,false);
  lc.setDigit(0,3,(byte)tens,false);
  lc.setDigit(0,2,(byte)ones,true);
  lc.setDigit(0,1,(byte)ftens,false);
  lc.setDigit(0,0,(byte)fones,false);
}
void loop()
{
  buttonState=digitalRead(buttonPin);
  sample=0; 
  for(int i=0;i<150;i++)
  {
    sample+=analogRead(tempPin);  //read the value from the sensor
    delay(2);
  }
  sample=sample/150; 
  tempC=(5.0*sample*100.0)/1023.0;   //convert the analog data to temperature  
  if(buttonState!=lastButtonState)
  {
     lastButtonState=buttonState;
  }
  if(lastButtonState==1)
    {
      printNumber(tempC);
      delay(200);
    }
    if(lastButtonState==0)
    {
      tempF=((tempC*9)/5)+32;
      printNumber(tempF);
      delay(200);
    }
}
 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!