Ticker class usage inside of while loop

I'm trying to program a sequence using 3 LEDs, using the ticker class to call the function which lights a certain LED and then moves onto the next value in the sequence with a 1 second delay. I only want this sequence to show for as long as the variable 'iterations' is smaller than a constant N.

However, when I run the code, none of the LEDs light up, and I think i may be to do with the fact that cycle_ticker.attach line is inside of a while loop possibly, but I'm not sure, and if so, how to correct it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Ticker cycle_ticker;
float cycle_time_interval = 1.0;

void CycleTicker(void){
    select_led(t);
    t=(t%3)+1;
    }

int main() {
         t=1;
         button.rise(onButtonPress);
         while(iterations < N) {
                cycle_ticker.attach(CycleTicker,cycle_time_interval);
                }


Any help would be greatly appreciated!
(new to this platform)
Thanks :)
yes!
Hello RadhikaGupta,

Your code is incomplete. There is no way to compile or test what you have poses and it poses mores questions than answers.

There is no way of telling where button.rise(...) comes from and the same for cycle_ticker.attach(CycleTicker, cycle_time_interval). Also with the second function call is "CycleTicker" a variable or a function?

"t" is used in void CycleTicker(void) and "main", but where and how is it defined. Also the "void" in the () is not needed. This leads one to believe that you are being taught a very old way of coding which makes people wonder how old the compiler is.

Not understanding what cycle_ticker.attach() is and where you are trying to turn the LEDs on and off it makes it hard to see where the program might be going wrong.

There may be some here that have more experience working with this type of program that might have some suggestions based on what they have done and are guessing when it comes to what you did.

Most times it is better to post the whole program and all the files that make up the program along with any input files or at least a fair sample of the input file. This way people can compile and test your program and everyone will be using the same information.

For what its worth some tips:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Ticker cycle_ticker;

float cycle_time_interval = 1.0;

void CycleTicker()
{
	select_led(t);

	t = (t % 3) + 1;
}

int main()
{
	t = 1;

	button.rise(onButtonPress);

	while (iterations < N)
	{
		cycle_ticker.attach(CycleTicker, cycle_time_interval);
	}

	return 0;
}

I find this use of the {}s along with the blank lines makes the code easier to read and easy to read is more important in the end than running all your code together.

"double" is the preferred floating point type, but if your numbers are small enough a float will work.

Using a single letter for a variable name is not always a good idea. In for loops the variable is local and only lasts as long as the for loop. Otherwise give the variable a name that describes what it is or what it does. It is a great help to others and to yourself in the future when you look back at a program to see what you did.

The "return 0;" at the end of main is not necessary or required, but it makes a good place for a break point when debugging the program.

Hope that helps,

Andy
Dear Andy,

Thankyou for your reply and layout suggestions.
I've simplified my code to better present the issue...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DigitalOut led1(LED1);

const int N = 4;
int iterations = 0;

Ticker cycle_ticker;
float cycle_time_interval = 1.0;


void CycleTicker(void){
    led1 != led1;
    }

int main() {
         while(iterations < N) {
                cycle_ticker.attach(CycleTicker,cycle_time_interval);
                } 
}


Instead of flashing LED1 4 times, this code does not cause any change to the state of the LED at all. If i take the cycle_ticker.attatch line outside the loop, the led will keep flashing forever, which is not what I want.

Thanks again
Radhika
forgot to include iterations++ after led1 != led1;
Your while loop in main is just going to flood your system with tickers.

Look, start with the example Ticker app listed on the web page I found, and
a) tell us whether it works on your board.
b) tell us exactly HOW it doesn't meet your requirements.

Because you seem to be fumbling all over the place getting nowhere.
a) Yes, the code on the website works on my board
b) The code on the website is not what i'm trying to replicate exactly

The simplified code above does not work on my board. It does not do anything/ cause any LEDs to flash whatsoever, probably because my system is being flooded with tickers due to the while loop, as you said.

I just wanted to know how I can fix this issue to get the ticker to work until something is no longer true, without using the while loop, as this clearly does not work.

I'm not going to dig through the manual for syntax detail, but I think you need to do something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void onButtonStopDebouncing(void);

void onButtonPress(void)
{
        cycle_ticker.cancel();  //!! Fix this line to be whatever cancels or erases a ticker.
        button.rise(NULL);
        button_debounce_timeout.attach(onButtonStopDebouncing, debounce_time_interval);

}

void onButtonStopDebouncing(void)
{
        button.rise(onButtonPress);
}


void onCycleTicker(void)
{
        led3 = !led3;
}


int main()
{
        button.rise(onButtonPress);
        cycle_ticker.attach(onCycleTicker, cycle_time_interval);
}

The LED will keep alternating until you press a button.


Or maybe you want your n iterations, in which case
1
2
3
4
5
6
7
void onCycleTicker(void)
{
        led3 = !led3;
        if ( ++iterations == limit ) {
                cycle_ticker.cancel();  //!! Fix this line to be whatever cancels or erases a ticker.
        }
}

Topic archived. No new replies allowed.