String to uint8_t Help!

Hi all.

I want to cast an String to unit8_t.
I searched the net and got some answers but none of them suited for my needs,
or I don’t know how to use it properly.
here is my code

#ifndef Pins_Arduino_h
#define Pins_Arduino_h

#define EXTERNAL_NUM_INTERRUPTS 16
#define NUM_DIGITAL_PINS        17
#define NUM_ANALOG_INPUTS       1

#define analogInputToDigitalPin(p)  ((p > 0)?NOT_A_PIN:0)
#define digitalPinToInterrupt(p)    (((p) < EXTERNAL_NUM_INTERRUPTS)?p:NOT_A_PIN)
#define digitalPinHasPWM(p)         (((p) < NUM_DIGITAL_PINS)?p:NOT_A_PIN)

static const uint8_t SDA = 4;
static const uint8_t SCL = 5;

static const uint8_t SS    = 15;
static const uint8_t MOSI  = 13;
static const uint8_t MISO  = 12;
static const uint8_t SCK   = 14;

static const uint8_t BUILTIN_LED = 16;

static const uint8_t A0 = 17;

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use.  For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR        Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL     Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE    Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN  Hardware serial ports which are open for use.  Their RX & TX
//                            pins are NOT connected to anything by default.
//#define SERIAL_PORT_MONITOR        Serial
//#define SERIAL_PORT_USBVIRTUAL     Serial
//#define SERIAL_PORT_HARDWARE       Serial
//#define SERIAL_PORT_HARDWARE_OPEN  Serial

#endif /* Pins_Arduino_h  */
void setup() 
  {
  pinMode(D1, OUTPUT);  
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);  
  }

void loop() 
  {
  String dot = String("D");  
  String dit = String();  
  
  int a = 100*random(1,10);
  int b = 100*random(1,10);
  
  dit =  dot + random(1,4);
 
  digitalWrite(dit, LOW);
  delay(a);                    
  digitalWrite(dit, HIGH);  
  delay(b);                      
  }

the error said :

error: cannot convert ‘String’ to ‘uint8_t {aka unsigned char}’ for argument ‘1’ to ‘void digitalWrite(uint8_t, uint8_t)’.

have a good day.

Your pin selection is seriously misguided. There are no comments in the code to explain what you thought you were trying to do, so it's hard to guess. But I am 100% sure you can not write to pin number (String("D")+random(1,4)). You are going to have to explain that one.

Forget about casts right now. You have a mess of incorrect assumptions to clean up first.

I'm using arduino for ESP8266 nodemcu thats why the pins are wired.
I added the headers ,is it good now?

In future, please don't go back and edit code in posts. It makes the replies that follow it appear incorrect or confusing. Just repost the new code.

You have not really addressed my comments in your reply.

Just for starters, and this is just scratching the surface, which integer begins with a "D"?

Mr aarg!
Do you know the solution or not ?

Jetset:
Mr aarg!
Do you know the solution or not ?

The solution to what? If anybody can guess what you are trying to do, please speak up now!

Do you know the solution or not ?

There is no solution for what you are trying to do. Pins have numbers. "D1" is NOT a pin number.

#ifndef Pins_Arduino_h
#define Pins_Arduino_h

#define EXTERNAL_NUM_INTERRUPTS 16
#define NUM_DIGITAL_PINS        17
#define NUM_ANALOG_INPUTS       1

#define analogInputToDigitalPin(p)  ((p > 0)?NOT_A_PIN:0)
#define digitalPinToInterrupt(p)    (((p) < EXTERNAL_NUM_INTERRUPTS)?p:NOT_A_PIN)
#define digitalPinHasPWM(p)         (((p) < NUM_DIGITAL_PINS)?p:NOT_A_PIN)

static const uint8_t SDA = 4;
static const uint8_t SCL = 5;

static const uint8_t SS    = 15;
static const uint8_t MOSI  = 13;
static const uint8_t MISO  = 12;
static const uint8_t SCK   = 14;

static const uint8_t BUILTIN_LED = 16;

static const uint8_t A0 = 17;

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use.  For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR        Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL     Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE    Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN  Hardware serial ports which are open for use.  Their RX & TX
//                            pins are NOT connected to anything by default.
//#define SERIAL_PORT_MONITOR        Serial
//#define SERIAL_PORT_USBVIRTUAL     Serial
//#define SERIAL_PORT_HARDWARE       Serial
//#define SERIAL_PORT_HARDWARE_OPEN  Serial

#endif /* Pins_Arduino_h    Serial.begin(9600);
  while (!Serial) 
    {
      ; 
    }*/



void setup() 
  {
  pinMode(D1, OUTPUT);  
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);  
  }

void loop() 
  {
//  String dot = String("D");  
//  String dit = String();  
//  
  int a = 100*random(1,10);
  int b = 100*random(1,10);
//  
//  dit =  dot + random(1,4);
 
  digitalWrite(D2, LOW);
  delay(a);                    
  digitalWrite(D2, HIGH);  
  delay(b);                      
  }

this code runs ok
digitalWrite(D2, LOW);

I want to randomly send value to different pins (from D1 to D3).
is it possible to covert a string to unit_8t and use it in digitalWrite ?

is it possible to covert a string to unit_8t and use it in digitalWrite ?

Yes. But it is NOT possible to randomly choose the NAME of a variable that holds a pin number.

There are no names at run time.

Put the pin numbers in an array. Randomly choose a position in the array. Use that index to access the pin number.

Sample:digitalWrite(D2+random(1,4), HIGH);If the pins are consecutive. You should improve that by indexing an array as mentioned above.

digitalWrite(pinArray[random(0,4)], HIGH);

Thanks both of you.
I'm going to do that.

Jetset:
I added the headers ,is it good now?

Copy-and-paste is not how you’re supposed to use header files. There is an include statement that you can use. I’m not sure which header file you copied but below will include headerfile.h so whatever is defined in there is known to the compiler.

#include <headerfile.h>
void setup()
{
}

void loop()
{
}

If you ever create your own header file for something and it’s located in your project directory, you can include it using

#include "headerfile.h"