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 » Button ipod help

June 09, 2011
by missle3944
missle3944's Avatar

Hi guys, I have run into a problem with a cool project that I made up today. Does anyone know how to turn a pin on high just once instead of looping through the while loop. My problem is that when the temp_avg gets to a certan point it pulls a pin high. but i only want it to pull it once in the if statement and not loop it. So i only whant it to turn on once each time the temp is achieved. Here's my code if anyone can help :)

   if (temp_avg < 350) {

    // turn on RED LED
        PORTC |= (1<<PC4);

   }

   if (temp_avg > 350) {

    // turn on RED LED

    PORTC &= ~(1<<PC4);
June 09, 2011
by met_fredrik
met_fredrik's Avatar

Hi! It is a bit unclear to me what you are really asking. But if i understand you correctly you want the pin to turn on and off inside the same if statement? To do this, simply turn the pin on and off.

if (temp_avg < 350) {.  
 // turn on RED LED.      PORTC |= (1<<PC4);
//maybe put in a delay?
 // turn it off.
 PORTC &= ~(1<<PC4);

}

I hope I understood you correctly!

June 09, 2011
by missle3944
missle3944's Avatar

Hi met_fredrik,

I already tried that but what I really mean is when the temp is less than 350 I only want it to blink once and not continually loop through it. It's a funny question. Maybe I'll try it in the main loop

-missle3944

June 09, 2011
by Ralphxyz
Ralphxyz's Avatar

missle3944 you might want to use ISR(ADC_vect), that is the ADC Interrupt.

Also if you only want the if statement to run once you can run it in your main but then you have to figure out a reset to do it again in the future.

I think using the interrupt might solve most problems for you.

That's vector 22 ADC program complete.

You might want to Google "AVR adc interrupt" to see lots of discussions.

Ralph

Ralph

June 09, 2011
by Rick_S
Rick_S's Avatar

Or, just use a flag...

if(temp_avg<350&(flag==0)){
  flag=1;
  PORTC |=(1<<PC4);
  delay_ms(50);
  PORTC &= ~(1<<PC4);
}

if(temp_avg>350){
  flag=0;
}

May not be fully correct syntax wise... kinda did it on the fly... but you should get the idea..

Rick

Post a Reply

Please log in to post a reply.

Did you know that the microcontroller's crystal oscillator can be used to keep accurate time? Learn more...