8 years, 1 month ago.

Ticker not Ticking

Hi all

I'm currently running code on two LPC1768 boards. I have a ticker running on both, both using the same base code. One ticker is running succesfully, and one isn't going into the method I'm attaching the ticker to. Can anyone help at all? I'm sure it's a stupid mistake, I just can't seem to spot it!

Cyclictic Class:

  1. include "mbed.h"
  2. include "cyclictic_in.h"

CyclClassIn::CyclClassIn() {

}

void CyclClassIn::setup() { Serial pc(USBTX,USBRX); pc.baud(460800); pc.printf("I am setting up\n\r"); }

void CyclClassIn::CyclEx() { Serial pc(USBTX,USBRX); pc.baud(460800);

pc.printf("I have started");

if(ticks % 3 == 0) { pc.printf("Accelerometer \n\r");

} else if(ticks % 3 == 1) { pc.printf("Linear Potentiometer\n\r"); } else {

}

ticks++; }

Main:

  1. include "mbed.h"
  2. include "cyclictic_in.h"

int main() { Serial pc(USBTX, USBRX);

pc.baud(460800);

CyclClassIn* cyc = new CyclClassIn();

pc.printf("Setting up...\n\r"); pc.printf("********"); wait_ms(100);

cyc->setup();

Ticker ticker;

ticker.attach(cyc,&CyclClassIn::CyclEx,0.01);

pc.printf("I assume I am getting here\n\r");

while(1) { } }

1 Answer

8 years, 1 month ago.

Please use <<code>> and <</code>> to format code correctly. Your code:

#include "mbed.h"
#include "cyclictic_in.h"
CyclClassIn::CyclClassIn()
{
}

void CyclClassIn::setup()
{
    Serial pc(USBTX,USBRX);
    pc.baud(460800);
    pc.printf("I am setting up\n\r");
}

void CyclClassIn::CyclEx()
{
    Serial pc(USBTX,USBRX);
    pc.baud(460800);
    pc.printf("I have started");

    if(ticks % 3 == 0) {
        pc.printf("Accelerometer \n\r");

    } else if(ticks % 3 == 1) {
        pc.printf("Linear Potentiometer\n\r");
    } else {

    }
    ticks++;
}

#include "mbed.h"
#include "cyclictic_in.h"
int main()
{
    Serial pc(USBTX, USBRX);

    pc.baud(460800);

    CyclClassIn* cyc = new CyclClassIn();

    pc.printf("Setting up...\n\r");
    pc.printf("********");
    wait_ms(100);

    cyc->setup();

    Ticker ticker;

    ticker.attach(cyc,&CyclClassIn::CyclEx,0.01);

    pc.printf("I assume I am getting here\n\r");

    while(1) { }
}

A couple of questions:

Why are you creating a new instance of a serial port 10 times per second? Especially when it conflicts with the instance created in main?

Where is ticks initialized? This shouldn't cause any big problems but it's a good habit to always initialize a counter.

The second issue is trivial to fix, set it to 0 in CyclClassIn::CyclClassIn()

I would guess that the root problem is the serial port stuff, two ports using the same hardware and then calling printf (which isn't thread safe) is just asking for problems. I would have expected it to work but it's odd enough that it's worth fixing before going further.

In cyclictic_in.h add

extern RawSerial pc;

and then in the main file change things to be:

RawSerial pc(USBTX, USBRX);

int main() { 
  pc.baud(460800);
...

In your other code remove all of the constructor and baud rate settings for the serial port.

Now pc will be initialized once (using RawSerial which is thread safe) and be visible in any files that include your header.