View previous topic :: View next topic |
Author |
Message |
buckeyes1997
Joined: 12 May 2005 Posts: 15
|
why wont this speed up and then slow down correctly?? |
Posted: Thu Aug 25, 2005 9:00 pm |
|
|
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
|
|
Posted: Thu Aug 25, 2005 9:05 pm |
|
|
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
|
not sure i follow |
Posted: Fri Aug 26, 2005 5:56 am |
|
|
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
|
|
Posted: Fri Aug 26, 2005 6:13 am |
|
|
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
|
thanks mark |
Posted: Fri Aug 26, 2005 7:05 am |
|
|
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
|
|
Posted: Fri Aug 26, 2005 8:36 am |
|
|
They look correct to me. Are you sure that it doesn't reach it? |
|
|
Rocket Guest
|
|
Posted: Fri Aug 26, 2005 8:42 am |
|
|
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. |
|
|
|