CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

why wont this speed up and then slow down correctly??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
buckeyes1997



Joined: 12 May 2005
Posts: 15

View user's profile Send private message

why wont this speed up and then slow down correctly??
PostPosted: Thu Aug 25, 2005 9:00 pm     Reply with quote

Here is my code. im trying to make an LED pattern light up with the delay between the three LED's speeding up then slowing down. I want the time to go from 700ms between down to 250ms and then from 250ms up to 700ms. I want it to be a smooth transistion from speeding up to slowing down and then start over.

when i compile the code and test it the LED's speed up and then start over and never get to the slowing down part. its like they speed all the way up and then BREAK to start the entire MAIN over.

please give me some thoughts

Code:
#include "C:\Documents and Settings\matt\My Documents\PICDESIGNS\gradcap.h"


void main()
{
int16 x;
   /*port_b_pullups(TRUE);*/
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
x=0;
while(1){

for(x=700; x>250; x=x-40)
{
   output_high(PIN_B0);
   DELAY_MS(x);

   OUTPUT_LOW(PIN_B0);
   OUTPUT_HIGH(PIN_B1);
   DELAY_MS(x);

   OUTPUT_LOW(PIN_B1);
   OUTPUT_HIGH(PIN_B2);
   DELAY_MS(x);
   OUTPUT_LOW(PIN_B2);
}

for(x=250; x<700; x=x+40)
{
output_high(PIN_B0);
   DELAY_MS(x);

   OUTPUT_LOW(PIN_B0);
   OUTPUT_HIGH(PIN_B1);
   DELAY_MS(x);

   OUTPUT_LOW(PIN_B1);
   OUTPUT_HIGH(PIN_B2);
   DELAY_MS(x);
   OUTPUT_LOW(PIN_B2);
}
   }


}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 25, 2005 9:05 pm     Reply with quote

Quote:
Syntax:
delay_ms (time)



Parameters:
time - a variable 0-255 or a constant 0-65535




time is an 8 bit var therefore you can't delay more than 255ms
buckeyes1997



Joined: 12 May 2005
Posts: 15

View user's profile Send private message

not sure i follow
PostPosted: Fri Aug 26, 2005 5:56 am     Reply with quote

even though i declared the variable as an int16, you are saying i cannot call that variable in the time parameter??

i know i can put values of over 255 in the delay_ms() function so how would i do that with it being a variable that changes each iteration of the loop? could i keep X less than 255 and just add an offset before putting it in the delay_ms() call?

is the time variable causing it to never perform the two FOR statements correctly? other than time being an 8bit does the rest look okay?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Aug 26, 2005 6:13 am     Reply with quote

The quote is from the manual. It clearly states that if you pass a variable its 8 bits but if you pass a constant then 16 bits is allowed.

Since you are happy with 40ms increments then why not do a nested for with the outer for loop the maximum number of 40ms and the inner for delaying those 40ms. Another approach would be to wrap the delay in another function. This would give just a slight error but for what you are doing it might not matter.

Code:

void mydelay_ms(int16 time)
{
  int8 i;

  i = (int8)(time>>8);
  while (i)
  {
    delay_ms(256);
    // if you wanted to improve this even more, you could do a
    // delay_ms(255) and delay_us() where delay_us() would be
    // adjusted to account for the while loop overhead.
    i--;
  }
  delay_ms((int8)time);
}
buckeyes1997



Joined: 12 May 2005
Posts: 15

View user's profile Send private message

thanks mark
PostPosted: Fri Aug 26, 2005 7:05 am     Reply with quote

here is what im trying to do. i have three outputs that i want to switch on and off in sequence to make an animation on a graduation cap. it works fine at a constant speed, but since it looks like a propeller i wanted to make it get faster andthen slower and then faster and then slower....etc.

here is a video clip of what i have.
http://www.allthingsrc.com/ebay/caps.avi

i figured the variable for loops would be the easy way.
my problem is it doesnt seem to get out of the first for loop. could you tell me if the syntax of my for loops is correct? should i use "()" instead of "{}" after the for statement? i want it to execute the first four statement until it gets to the limit and then go on to the next for statement and then start back over at the top of MAIN. other than the time being an 8bit i dont understand why it didnt do that properly.

thanks a ton for your help
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Aug 26, 2005 8:36 am     Reply with quote

They look correct to me. Are you sure that it doesn't reach it?
Rocket
Guest







PostPosted: Fri Aug 26, 2005 8:42 am     Reply with quote

Hi,
this is NOT tested, but you can try it..


Code:

#include "C:\Documents and Settings\matt\My Documents\PICDESIGNS\gradcap.h"

int counter, x;

void delay_time()
{
  for(counter=0; counter < x; counter++)
  delay_ms(40);
}


void main()
{
   /*port_b_pullups(TRUE);*/
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
x=0;
while(1){

   for(x=18; x>5; x=x-1)
    {
       output_high(PIN_B0);
       delay_time();
     
       OUTPUT_LOW(PIN_B0);
       OUTPUT_HIGH(PIN_B1);
       delay_time();

       OUTPUT_LOW(PIN_B1);
       OUTPUT_HIGH(PIN_B2);
       delay_time();
       OUTPUT_LOW(PIN_B2);
     }

    for(x=5; x < 18; x++)
     {
        output_high(PIN_B0);
        delay_time();

        OUTPUT_LOW(PIN_B0);
        OUTPUT_HIGH(PIN_B1);
        delay_time();

        OUTPUT_LOW(PIN_B1);
        OUTPUT_HIGH(PIN_B2);
        delay_time();
        OUTPUT_LOW(PIN_B2);
      }
   }
}


and give some feedback please.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group