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 » Need help with some temperature sensing

June 03, 2012
by chaosatom
chaosatom's Avatar

Hi,

I am running a motor, which goes faster if I increase the temperature by pinching the temperature sensor.

I am trying to check if "Current_Temperature > Original Temperature + 2" to sense the increase in temperature. So initially in while loop, I set temp1 to be original temperature. Then I check if the average temperature has gone over original temperature+2. The problem is that it goes through the if statement instantaneously. What could I do?

  temp_avg = 0.0;
    for(i=0; i<100; i++) {
      last_sample = adc_read();
      this_temp = sampleToFahrenheit(last_sample);

      // add this contribution to the average
      temp_avg = temp_avg + this_temp/100.0;
    }

    if(j = 0){
        temp1= temp_avg;
        j++;
        }

    if(temp_avg > (temp1+2)){
            lcd_write_string(PSTR("HELLO"));
            pwm_set(pos);
            }
June 03, 2012
by Ralphxyz
Ralphxyz's Avatar

The problem is that it goes through the if statement instantaneously. What could I do?

Well at the very least you could put in a delay.

delay_ms(500);

Ralph

June 03, 2012
by pcbolt
pcbolt's Avatar

chaosatom -

Your first "if" statement will never execute the code inside the braces. When you write:

if(j = 0){

You're assigning j the value 0, which evaluates to "false". For a comparison you need a double "=" sign:

if(j == 0){

The rest looks good.

June 03, 2012
by chaosatom
chaosatom's Avatar

Thanks for the advice guys.

I got it to work. That if statement was a problem. Also the time delay code would help me as well.

June 03, 2012
by pcbolt
pcbolt's Avatar

Great.

Did you get the PWM output to vary the motor speed? I'm curious which transistors and flyback diodes you decided on and how it was wired.

June 03, 2012
by chaosatom
chaosatom's Avatar

Yes, I decided to go with the 2N7000 transistor. For diode, I had one lying around (not sure about the name).

The wiring was done exactly as shown in this tutorial. http://www.nerdkits.com/videos/motors_and_microcontrollers_101/

I had a separate 9V battery powering this.

Post a Reply

Please log in to post a reply.

Did you know that a piezoelectric buzzer can be used in reverse as a microphone? Learn more...