Some basic C issue

I am trying to pass a parameter to a function “every” from arduino-timer library. Here is the main code.

#include <AccelStepper.h>
#include <timer.h>
#include "commons.h"

auto timer = timer_create_default(); // create a timer with default settings
#define LEDPIN 13 // to show action
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  9     // IN1 on the ULN2003 driver 1
#define motorPin2 10     // IN2 on the ULN2003 driver 1
#define motorPin3 11     // IN3 on the ULN2003 driver 1
#define motorPin4 12     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  pinMode(LEDPIN, OUTPUT);
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(200.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(20000);

  //FlashLEDs (create effect of siren);
  timer.every(100, switch_off_led, 13);
  
}//--(end setup )---



void loop() {
   timer.tick(); // tick the timer
  //Change direction when the stepper reaches the target position
  if (stepper1.distanceToGo() == 0) {
    stepper1.moveTo(-stepper1.currentPosition());
  }
  stepper1.run();
}

Code for commons.hs

bool switch_off_led(void *ledpin) {
  int pin = *((int*) ledpin);
  digitalWrite(pin, LOW);
  return true; // keep timer active? true
}

Issue which I get is below:

udhadv/Documents/Arduino/codebase/AccelStepperExample/AccelStepperExample.ino: In function 'void setup()':
/Users/udhadv/Documents/Arduino/codebase/AccelStepperExample/AccelStepperExample.ino:26:38: warning: invalid conversion from 'int' to 'void*' [-fpermissive]
   timer.every(100, switch_off_led, 13);
                                      ^
In file included from /Users/udhadv/Documents/Arduino/codebase/AccelStepperExample/AccelStepperExample.ino:2:0:
/Users/udhadv/Documents/Arduino/libraries/arduino-timer/src/timer.h:74:5: note:   initializing argument 3 of 'bool Timer<max_tasks, time_func>::every(long unsigned int, Timer<max_tasks, time_func>::handler_t, void*) [with unsigned int max_tasks = 16u; long unsigned int (* time_func)() = millis; Timer<max_tasks, time_func>::handler_t = bool (*)(void*)]'
     every(unsigned long interval, handler_t h, void *opaque = NULL)
     ^

Any pointers?

The third argument of timer.every needs to be a pointer. Assign "13" to a static const uint8_t variable and pass a pointer to that variable. You could also use a global variable rather than static/local to setup().

This successfully flashes an LED using the same functions. Maybe it can help you:

#include <timer.h>

auto timer = timer_create_default(); // create a timer with default settings

const byte pinLED = LED_BUILTIN;

void setup() 
{
    pinMode( pinLED, OUTPUT );
    timer.every( 250, toggle_led, (const byte *)&pinLED );
    
}//setup

void loop() 
{
    timer.tick();

}//loop

bool toggle_led( const byte *pin )
{        
    static bool
        state = false;

    digitalWrite( *pin, (state==true)?HIGH:LOW );
    state ^= true;

    return true;
    
}//toggle_led

Blackfin:
static bool
state = false;

Is the above style an example of literate programming?

GolamMostafa:
Is the above style an example of literate programming?

IMO, it looks a little better all on one line:

static bool state = false;

But, that's such a minor thing it's not worth arguing about.

GolamMostafa:
Is the above style an example of literate programming?

I really don't know where I picked that up. Must be 20 or 25 years ago though. I don't always do it. I'm very likely to do it if there are a number of variables of the same type, such as:

uint16_t
    nvar1,
    nvar2,
    nvar3,
    nvar4;
uint8_t
    uvar1,
    uvar2,
    uvar3;
static uint32_t
    ultimer1 = 0,
    ultimer2 = 0,
    ultimer3 = 0;

vs

uint16_t nvar1, nvar2, nvar3, nvar4;
uint8_t uvar1, uvar2, uvar3;
static uint32_t ultimer1 = 0, ultimer2 = 0, ultimer3 = 0;

I like the indented style. In much the same way, I'll do this:

    if( var > 0 )
    {
        //do something
    }//if

rather than

    if( var > 0 ) {
        //do something
        
    }//if

Takes more space but is, IMO, more readable. YMMV of course.

My Programming Teacher clearly said, "Your programming codes should comply with literate style if these are written for human readers."

I don’t always do it. I’m very likely to do it if there are a number of variables of the same type, such as:

In my opinion, that’s the worst possible time to do it. I look to the start of the CURRENT line to see how a variable is defined.

I tried the code from Blackfin, as below:

#include <timer.h>

auto timer = timer_create_default(); // create a timer with default settings

const byte pinLED = LED_BUILTIN;

void setup() 
{
    pinMode( pinLED, OUTPUT );
    timer.every( 250, toggle_led, (const byte *)&pinLED );
    
}//setup

void loop() 
{
    timer.tick();

}//loop

bool toggle_led( const byte *pin )
{        
    static bool
        state = false;

    digitalWrite( *pin, (state==true)?HIGH:LOW );
    state ^= true;

    return true;
    
}//toggle_led

I still get the error:

/Users/udhadv/Documents/Arduino/codebase/ledblinkwithtimer/ledblinkwithtimer.ino: In function 'void setup()':
/Users/udhadv/Documents/Arduino/codebase/ledblinkwithtimer/ledblinkwithtimer.ino:10:57: warning: invalid conversion from 'bool (*)(const byte*) {aka bool (*)(const unsigned char*)}' to 'Timer<>::handler_t {aka bool (*)(void*)}' [-fpermissive]
     timer.every( 250, toggle_led, (const byte *)&pinLED );
                                                         ^
In file included from /Users/udhadv/Documents/Arduino/codebase/ledblinkwithtimer/ledblinkwithtimer.ino:1:0:
/Users/udhadv/Documents/Arduino/libraries/arduino-timer/src/timer.h:74:5: note:   initializing argument 2 of 'bool Timer<max_tasks, time_func>::every(long unsigned int, Timer<max_tasks, time_func>::handler_t, void*) [with unsigned int max_tasks = 16u; long unsigned int (* time_func)() = millis; Timer<max_tasks, time_func>::handler_t = bool (*)(void*)]'
     every(unsigned long interval, handler_t h, void *opaque = NULL)
     ^
/Users/udhadv/Documents/Arduino/codebase/ledblinkwithtimer/ledblinkwithtimer.ino:10:57: warning: invalid conversion from 'const void*' to 'void*' [-fpermissive]
     timer.every( 250, toggle_led, (const byte *)&pinLED );
                                                         ^
In file included from /Users/udhadv/Documents/Arduino/codebase/ledblinkwithtimer/ledblinkwithtimer.ino:1:0:
/Users/udhadv/Documents/Arduino/libraries/arduino-timer/src/timer.h:74:5: note:   initializing argument 3 of 'bool Timer<max_tasks, time_func>::every(long unsigned int, Timer<max_tasks, time_func>::handler_t, void*) [with unsigned int max_tasks = 16u; long unsigned int (* time_func)() = millis; Timer<max_tasks, time_func>::handler_t = bool (*)(void*)]'
     every(unsigned long interval, handler_t h, void *opaque = NULL)

Is there anything to do with my IDE version? I have 1.8.9 version, in case need to know.

Try making the parameter to ‘toggle_led()’ be a ‘void *’ rather than a ‘const byte *’. Then, cast that pointer as necessary inside the function.