Need help with int pointer conversion

Hello all
I'm just starting with cpp for arduino/freeRTOS.
I created a simple program whic creates task to blink
three LEDs are on pin 13,12 and 11.
To Debug i print the pin number in ledControllerTask function.
Output i get is
19
18
17

some how 6 is added to pin numbers 13,12,11 -> 19,18,17

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
28
29
30
31
32
#include <Arduino.h>
#include <Arduino_FreeRTOS.h>

void ledControllerTask(void *pvParameters);

const uint8_t *blueLed = (uint8_t *)13;
const uint8_t *redLed = (uint8_t *)12;
const uint8_t *greenLed = (uint8_t *)11;

void setup()
{
  Serial.begin(115200);
  xTaskCreate(ledControllerTask, "BLUE LED Task", 100, (void *)blueLed, 1, NULL);
  xTaskCreate(ledControllerTask, "RED LED Task", 100, (void *)redLed, 1, NULL);
  xTaskCreate(ledControllerTask, "GREEN LED Task", 100, (void *)greenLed, 1, NULL);
}

void ledControllerTask(void *pvParameters)
{

  int pinNumber = *(uint8_t *)pvParameters;
  Serial.println(pinNumber);
  pinMode(pinNumber, OUTPUT);

  while (1)
  {
    digitalWrite(pinNumber, digitalRead(pinNumber) ^ 1);
    delay(200);
  }
}

void loop(){}
Try
 
int pinNumber = (int)pvParameters;

It is weird that you are manually typing the address of the pointer to a value and you are using that address as a number.

But this should work. Have you tried replacing (void *)blueLed with (void *)13, and using dutch's suggestion with it?

And if not, then try this code
https://www.freertos.org/a00125.html
It is weird that you are manually typing the address of the pointer to a value and you are using that address as a number.


Probably the location of a MMIO register. The pointed-to object ought to be qualified volatile.
Probably the location of a MMIO register.

I was assuming that they are integers being passed through a pointer variable.
But maybe it's best that he pass an actual pointer, in which case he could do something like this (untested code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct LED {
    const char* label;
    int pin;
} leds[3] {
    { "BLUE LED Task",  13 },
    { "RED LED Task",   12 },
    { "GREEN LED Task", 11 }
};

void task(const char* str, void* val) {
    xTaskCreate(ledControllerTask, str, 100, val, 1, NULL);
}

void setup() {
    Serial.begin(115200);
    for (int i = 0; i < ArrSize(leds); ++i)
        task(leds[i].label,  (void*)&leds[i]);
}

void ledControllerTask(void *pvParameters) {
    int pinNumber = ((LED*)pvParameters)->pin;
    //...
}

Oh, it's probably best to ignore my post. I should have read the OP more carefully.
Last edited on
Topic archived. No new replies allowed.