NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » First MCU Program

February 10, 2012
by TheUnfocusedOne
TheUnfocusedOne's Avatar

Hi All,

I'm currently working through the tutorial, and I decided to do a small side track to test out my programming skills thus far. My goal is a simple program, which will light up one of three LEDs depending on the temperature being read by the temperature sensor. It's a good chance for me to utilize what I've learned from both projects to create a new code.

Temp LED combos are as followed:

<75 - Green between 75 and 83 - Yellow

83 - Blinking Red

Here's my code:

/*
 *  LED_Temp_Sense.c *  
 *
 *  Created byon 2/10/12.
 *  Copyright 20. All rights reserved.
 *
 */

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/delay.h"  //expected headers from both Blink and Temp Sense//

void adc_init(){
    //startup for ADC//

    ADMUX = 0
    ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);//setting bits in a registry for the ADC//
    ADCSRA |= (1<<ADSC); //start up the ADC//
}

unint16_t adc_read(){
    //meant to stop the ADC from continuing while processing something//
    while(ADCSRA & (1<<ADSC)){
    }

    unint16_t result = ADCL;
    unint16_t temp = ADCH;
    results = results + (temp<<8);

    ADCSRA |= (1<<ADSC);

    return result;
}

double sampleToFahrenheit(uint16_t sample){
    return sample*(5000.0/1024.0/10.0);
}

#define GREEN 75 //Upper temp limit for green LED//
#define RED 83 //lower temp limit for red LED//

int main(){
    //start the Analog to Digital Converter//
    adc_init();

    //Turn the three LEDS to output//
    DDRC |= (1<<PC4);
    DDRC |= (1<<PC3);
    DDRC |= (1<<PC2);

    //holder variable for temp data//
    unit16_t last_sample = 0;
    double this_temp;
    double temp_avg;
    unint8_t i;

    while(1){
        //take 100 samples and average//
        temp_avg = 0.0;
        for(i=0;i<100; i++){
            last_sample = adc_read(); //read from the adc as definded in the funct above//
            this_temp = sampleToFahrenheit(last_sample);

            //add to average//
            temp_avg = temp_avg + this_temp/100.0;
        }

        if(temp_avg <= GREEN){
            PORTC |= (1<<PC4); //75C or below, green led on..
        }
        if(temp_avg > GREEN && <=RED){
            PORTC |= (1<<PC3) //between 75 and 83, yellow on//
        }
        if(temp_avg > RED){
            PORTC |= (1<<PC2);
            delay_ms(500);
            PORTC &= ~(1<<PC2);
            delay_ms;
        }
    }

    return 0;
}

I think its pretty solid, but I can't get it to work. I think part of the problem is my program not reading prior headers, since I cut and pasted or manually typed them in from another code. The errors I get come from compiling, and usually are stating that functions that should be defined by the headers aren't. How do I get it so I can load from the headers?

I'm not looking for someone to rewrite my code fyi, it's there incase I'm doing something stupid. I'd rather suggestions and advice than a solution.

Thanks in advanced. -TUO

February 10, 2012
by pcbolt
pcbolt's Avatar

Hi TUO -

There could be a few things going on here. First, just make sure the "libnerdkits" folder is in the same parent folder as your project folder. Second, you listed delay.h twice, and I don't think that would be a problem unless you meant to add some other header file. Third, on line 88 above you call "delay_ms" with no argument. The compiler won't like that. On an unrelated matter, on line 81 you have:

 if(temp_avg > GREEN && <=RED){

I don't know for certain if this works as intended. I would go with:

if((temp_avg > GREEN) && (temp_avg <= RED)){

Just to be safe.

February 10, 2012
by dvdsnyd
dvdsnyd's Avatar

Hi TUO,

Could you go into a bit more explanation of what isn't working? Not compiling? Or is it just not behaving as you expected?

February 11, 2012
by TheUnfocusedOne
TheUnfocusedOne's Avatar

Figured it out!

Stupid syntax issues, such as trying to make unint_16 variables instead of the correct uint_16.

Have it loaded, but it doesn't seem to be working... All the LEDs are on...

Back to the drawing board!

Thank to you two for the help. PCbolt I took your advice, much better line of code IMO.

Is there anyway to edit my first post? I hate how wide it is.

Post a Reply

Please log in to post a reply.

Did you know that essentially all power supplies' voltages drop when current is drawn from them? Learn more...