chars and const chars and strings

what is the difference between,

char chars[7] = "123456";

and

int integer = 123456;
char intChar = integer;
char chars[1]={intChar};

it seems like the expression "somechars" and the datatype 'char' are much different?

in other words,

udp.write("123456");

is not interchangeable with,

int integer = 123456;
char intChar = integer;
char chars[1]={intChar};

as in udp.write(chars); //from intChar

what is the difference?

Basically i have these lines of code i want to shorten,

      if (menuPosition == 1) {
        _pwmLVal[0] = _pwmLVal[0] + 50;
        if (_pwmLVal[0] > 1001) {
          _pwmLVal[0] = 1;
        }
        j = 0;
        Serial.print("pwmvalue");
        Serial.println(_pwmLVal[0]);
        sendCommand(Client1, _pwmLVal[0], "NODEC1", "C1NODE");
  
  
        _menuSelect(0, 0, 2, array[0], "Light:");
      }
      if (menuPosition == 2) {
        _pwmLVal[1] = _pwmLVal[1] + 50;
        if (_pwmLVal[1] > 1001) {
          _pwmLVal[1] = 1;
        }
        j = 1;
        sendCommand(Client1, _pwmLVal[1], "NODEC2", "C2NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[1], "Light:");
      }
      if (menuPosition == 3) {
        _pwmLVal[2] = _pwmLVal[2] + 50;
        if (_pwmLVal[2] > 1001) {
          _pwmLVal[2] = 1;
        }
        j = 2;
        sendCommand(Client1, _pwmLVal[2], "NODEC3", "C3NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[2], "Light:");
      }
      if (menuPosition == 4) {
        _pwmLVal[3] = _pwmLVal[3] + 50;
        if (_pwmLVal[3] > 1001) {
          _pwmLVal[3] = 1;
        }
        j = 3;
        sendCommand(Client1, _pwmLVal[3], "NODEC4", "C4NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[3], "Light:");
      }
  
      if (menuPosition == 5) {
        _pwmLVal[4] = _pwmLVal[4] + 50;
        if (_pwmLVal[4] > 1001) {
          _pwmLVal[4] = 1;
        }
        j = 4;
        sendCommand(Client1, _pwmLVal[4], "NODEC5", "C5NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[4], "Light:");
      }
      if (menuPosition == 6) {
        _pwmLVal[5] = _pwmLVal[5] + 50;
        if (_pwmLVal[5] > 1001) {
          _pwmLVal[5] = 1;
        }
        j = 5;
        sendCommand(Client1, _pwmLVal[5], "NODEC6", "C6NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[5], "Light:");
      }
      if (menuPosition == 7) {
        _pwmLVal[6] = _pwmLVal[6] + 50;
        if (_pwmLVal[6] > 1001) {
          _pwmLVal[6] = 1;
        }
        j = 6;
        sendCommand(Client1, _pwmLVal[6], "NODEC7", "C7NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[6], "Light:");
      }
      if (menuPosition == 8) {
        _pwmLVal[7] = _pwmLVal[7] + 50;
        if (_pwmLVal[7] > 1001) {
          _pwmLVal[7] = 1;
        }
        j = 7;
        sendCommand(Client1, _pwmLVal[7], "NODEC8", "C8NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[7], "Light:");
      }
      if (menuPosition == 9) {
        _pwmLVal[8] = _pwmLVal[8] + 50;
        if (_pwmLVal[8] > 1001) {
          _pwmLVal[8] = 1;
        }
        j = 8;
        sendCommand(Client1, _pwmLVal[8], "NODEC9", "C9NODE");
        sent = true;
        _menuSelect(0, 0, 2, array[8], "Light:");
      }
      if (menuPosition == 10) {
        _pwmLVal[10] = _pwmLVal[10] + 50;
        if (_pwmLVal[10] > 1001) {
          _pwmLVal[10] = 1;
        }
        sendCommand(Client1, _pwmLVal[10], "NODECA", "CANODE");
        _menuSelect(0, 0, 2, array[10], "Light:All");
      }
    }
    if (_menuButtonState == HIGH) {
      menuPosition ++;
      if (menuPosition > 0 && menuPosition < 11) {
        i = menuPosition;
        _menuSelect(0, 0, 2, 1002, "Light:");
      } else {
        i = 0;
        menuPosition = 0;
        defualtLayout();
      }
   }

i can shorten them if i could use this,

//    if (menuPosition > 0) {
     int r = menuPosition;
   _pwmLVal[r - 1] = _pwmLVal[r - 1] + 50;
     if (_pwmLVal[r - 1] > 1001) {
       _pwmLVal[r - 1] = 1;
     }
     j = r - 1;
     sendCommand(Client1, _pwmLVal[r - 1], r);

and setup sendCommand like this,

void sendCommand(IPAddress to, int data, int who ) {
  char _to = who;
  Udp.beginPacket(to, clientPort);
  Udp.write("NODEC");
  Udp.write(_to);
  Udp.write((uint8_t*)&data, 4); //cast to bytes
  Udp.write("C");
  Udp.write(_to);
  Udp.write("NODE");
  Udp.endPacket();
}

but i didn't have any luck getting it to work

this is what ended up printing out the serial,

07:08:37.688 -> CNODE
07:08:37.688 -> NODEC
07:08:37.688 -> CNODE
07:08:37.688 -> NODEC

notsolowki:
what is the difference between,

char chars[7] = "123456";

and

int integer = 123456;
char intChar = integer;
char chars[1]={intChar};

char chars[7] = "123456";

creates a character array (also known as a cstring) with the characters 123456 in it. Think of this as a textual representation of a number.

int integer = 123456;

is an attempt to create a binary number with a value equal to decimal 123456. I say "attempt" because the maximum value that an int can hold is 32767. If you want a bigger number you need to use a long rather than an int.

These are meaningless - they won't do what you think, they may even give compiler errors.

char intChar = integer;
char chars[1]={intChar};

...R

Robin2:
I say "attempt" because the maximum value that an int can hold is 32767. If you want a bigger number you need to use a long rather than an int.

Except on 32-bit ARM and ESP processors where an 'int' is 4 bytes. I really don't understand why people don't simply use the standard 'int16_t', 'uint16_t', 'int32_t', etc designations. Then, you know exactly what type of "integer" you're dealing with regardless of processor type.

Well i'm using an esp8266. my attempt to convert the integer into a char array failed. I have been reading about 'itoa' and i can't exactly tell if this is what i need to use for this task. One thing that concerns me is 'itoa' will return a null terminated string. On my receiver i would expect that to arrive like this,

NODEC1/0C1/0NODE but it needs to it arrive like NODEC1/0C1NODE/0.

I suppose i could just ditch the NODECM/CMNODE thing but i just feel more comfortable have the extra bytes at the beginning and end of the message for a makeshift type of 'error' checking.

if i could use my 'int r' to modify my delimiters "C1NODE/C2NODE/C3NODE like this,

print"c"
print"r" //integer=1
print"node"

to get a char array that is "c1node"or"c2node" i could remove a hundred lines of code from my program

also when i Udp.print"1234567", What datatype and i sending when i do that? is this a char array?

You could use arrays:

const char *NodeCxNames[] = {"NODEC1", "NODEC2", "NODEC3", "NODEC4", "NODEC5",
                             "NODEC6", "NODEC7", "NODEC8", "NODEC9", "NODECA"
                            };


const char *CxNodeNames[] = {"C1NODE", "C2NODE", "C3NODE", "C4NODE", "C5NODE",
                             "C6NODE", "C7NODE", "C8NODE", "C9NODE", "CANODE"
                            };

{
  if (menuPosition >= 1 && menuPosition <= 10)
  {
    byte index = menuPosition - 1;
    
    _pwmLVal[index] = _pwmLVal[index] + 50;
    if (_pwmLVal[index] > 1001)
    {
      _pwmLVal[index] = 1;
    }
    j = index;
    Serial.print("pwmvalue");
    Serial.println(_pwmLVal[index]);
    sendCommand(Client1, _pwmLVal[index], NodeCxNames[index], CxNodeNames[index]);


    _menuSelect(0, 0, 2, array[index], (index==9) ? "Light:All" : "Light:");
  }
}

johnwasser:
You could use arrays:

const char *NodeCxNames[] = {"NODEC1", "NODEC2", "NODEC3", "NODEC4", "NODEC5",

"NODEC6", "NODEC7", "NODEC8", "NODEC9", "NODECA"
                            };

const char *CxNodeNames[] = {"C1NODE", "C2NODE", "C3NODE", "C4NODE", "C5NODE",
                            "C6NODE", "C7NODE", "C8NODE", "C9NODE", "CANODE"
                            };

{
  if (menuPosition >= 1 && menuPosition <= 10)
  {
    byte index = menuPosition - 1;
   
    _pwmLVal[index] = _pwmLVal[index] + 50;
    if (_pwmLVal[index] > 1001)
    {
      _pwmLVal[index] = 1;
    }
    j = index;
    Serial.print("pwmvalue");
    Serial.println(_pwmLVal[index]);
    sendCommand(Client1, _pwmLVal[index], NodeCxNames[index], CxNodeNames[index]);

_menuSelect(0, 0, 2, array[index], (index==9) ? "Light:All" : "Light:");
  }
}

Thanks i did not think of that initially. That did work.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_SSD1306.h>

IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 101);
int i = 0;
const char* ssid = "LCS";
const char* password = "a1b2c3d4";

WiFiUDP Udp;
unsigned int localUdpPort = 4210;  // local port to listen on
unsigned int clientPort = 4220;
char incomingPacket[255];  // buffer for incoming packets

const char *NodeCxNames[] = {"NODEC1", "NODEC2", "NODEC3", "NODEC4", "NODEC5",
                             "NODEC6", "NODEC7", "NODEC8", "NODEC9", "NODECA",
                             "C1NODE", "C2NODE", "C3NODE", "C4NODE", "C5NODE",
                             "C6NODE", "C7NODE", "C8NODE", "C9NODE", "CANODE"
                            };

int _pwmLVal[] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
int array[9];

const int _menuButton = 12;
int _menuButtonState = 0;
const int _upButton = 14;
int _upButtonState = 0;
const int _downButton = 13;
int _downButtonState = 0;
int lastUpButtonState = LOW;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;

int menuPosition = 0;

bool sent = false;
bool connected = false;
int j = 0;

unsigned long now = 0;
unsigned long now2 = 0;
unsigned long now3 = 0;
unsigned long now4 = 0;
#define OLED_RESET LED_BUILTIN  //
Adafruit_SSD1306 display(OLED_RESET);

void sendCommand(IPAddress to, int data,  int id  ) {
  Serial.println(id);

  Udp.beginPacket(to, clientPort);
  Udp.write(NodeCxNames[id - 1]);
  Udp.write((uint8_t*)&data, 4); //cast to bytes
  Udp.write(NodeCxNames[id + 9]);
  Udp.endPacket();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  pinMode(_menuButton, INPUT);
  pinMode(_upButton, INPUT);
  pinMode(_downButton, INPUT);
  WiFi.setOutputPower(20.5);
  WiFi.persistent(0);
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_AP);
  while (!WiFi.softAP(ssid, password, 9, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
  display.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
  display.display();
  Udp.begin(localUdpPort);
}

void defualtLayout() {
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.clearDisplay();
  for (int i = 0; i <= 8; i++) {
    display.print("L");
    display.print(i + 1);
    display.print(":");
    display.println(array[i]);
    if (i == 7) {
      display.setCursor(50, 0);
    }
  }
  if (connected) {
    display.setCursor(50, 50);
    display.println("Connected!");
  }
  display.display();
}

void _menuSelect(int x, int y, int _size, int Val, char item[]) {
  display.clearDisplay();
  display.setTextSize(_size);
  display.setCursor(x, y);
  if (i == 10) {
    display.println("Light:All");
  } else {
    display.print(item);
    display.println(i);
  }

  if (Val != 1002) {
    display.println(Val);
  }
  display.display();
}

void loop() {
  _menuButtonState = digitalRead(_menuButton);
  int _upButtonReading = digitalRead(_upButton);
  _downButtonState = digitalRead(_downButton);

  if (_upButtonReading != lastUpButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (_upButtonReading != _upButtonState) {
      _upButtonState = _upButtonReading;

      if (_upButtonState == HIGH) {
        if (menuPosition > 0) {
          int r = menuPosition;
          _pwmLVal[r - 1] = _pwmLVal[r - 1] + 50;
          if (_pwmLVal[r - 1] > 1001) {
            _pwmLVal[r - 1] = 1;
          }
          j = r - 1;
          Serial.println(_pwmLVal[r - 1]);
          sendCommand(Client1, _pwmLVal[r - 1], r);
        }
      }
    }
  }
  lastUpButtonState = _upButtonReading;

  if (_menuButtonState == HIGH) {
    menuPosition ++;
    if (menuPosition > 0 && menuPosition < 11) {
      i = menuPosition;
      _menuSelect(0, 0, 2, 1002, "Light:");
    } else {
      i = 0;
      menuPosition = 0;
      defualtLayout();
    }
  }

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char verifyStart[7];//NODEC1 plus null
    char verifyEnd[7];//C1NODE plus null
    int _data;
    int len = Udp.read(incomingPacket, 800);
    strncpy (verifyStart, (char*)incomingPacket, 6);//6 bytes
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 6 );//6 bytes
    verifyStart[6] = 0; //null terminate
    verifyEnd[6] = 0; //null terminate
    if (strcmp(verifyStart, "NODEUD") == 0) {
      if (strcmp(verifyEnd, "UDNODE") == 0) {
        memcpy(&array, incomingPacket + 6 , sizeof(array)); //copy 4 bytes to _data
        array[10] = 0; //null terminate
        now4 = millis();
        connected = true;
        if (menuPosition == 0) {
          defualtLayout();
        }
      }
    }
  }

  if (millis() - now4 >= 3000) {
    connected = false;
    if (menuPosition == 0) {
      display.clearDisplay();
      display.setTextSize(1);
      display.setCursor(0, 20);
      display.println("DISCONNECTED!!");
      display.display();
      now4 = millis();
    }

  }
  if (millis() - now2 >= 1000) {
    if (menuPosition != 0 && menuPosition != 10) {
      _menuSelect(0, 0, 2, array[i - 1], "Light:");
    }
    if (i == 10) {
      _menuSelect(0, 0, 2, array[0], "Light:");
    }
    now2 = millis();
  }

  if (millis() - now >= 2000) {
    if (_upButtonState == LOW) {
      if (_downButtonState == LOW) {
        if (menuPosition == 0) {
          if (connected) {
            defualtLayout();
          }
        }
      }
    }
    now = millis();
  }
}

i ended up being able to shorten the code over 200 lines. i actually had to use debounce because the code runs so much faster now

edit: i just removed the debounce without delay and just add a delay because i dont need non blocking

int array[9];  //indexes 0-8
memcpy(&array, incomingPacket + 6 , sizeof(array)); 
array[10] = 0; //null terminate

This looks wrong. The array indexes run from 0 to 8, and you do not "own" the memory space of array[10].

This looks like some sort of attempt to null terminate an array like you have done with the character arrays. It does not make sense with the array of integer values.

What exactly is the message coming back surrounded by NODEUD and UDNODE?

cattledog:

int array[9];  //indexes 0-8

memcpy(&array, incomingPacket + 6 , sizeof(array));
array[10] = 0; //null terminate




This looks wrong. The array indexes run from 0 to 8, and you do not "own" the memory space of array[10].

This looks like some sort of attempt to null terminate an array like you have done with the character arrays. It does not make sense with the array of integer values.

What exactly is the message coming back surrounded by NODEUD and UDNODE?

That makes sense about not owning the memory at array[10]. i have removed the line that attempts to null terminate a array and not a char string. i have since revised the code and continue shortening it. let me know if you see anything wrong.

I have array[9] = array[0]; because i have Light"all" and i need a value to populate the screen and it dont matter which value it chooses because they will all be the same,

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_SSD1306.h>

IPAddress local_IP(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 101);

const char* ssid = "LCS";
const char* password = "a1b2c3d4";

WiFiUDP Udp;
unsigned int localUdpPort = 4210;  // local port to listen on
unsigned int clientPort = 4220;
char incomingPacket[255];  // buffer for incoming packets
const char *NodeCxNames[] = {"NODEC1", "NODEC2", "NODEC3", "NODEC4", "NODEC5",
                             "NODEC6", "NODEC7", "NODEC8", "NODEC9", "NODECA",
                             "C1NODE", "C2NODE", "C3NODE", "C4NODE", "C5NODE",
                             "C6NODE", "C7NODE", "C8NODE", "C9NODE", "CANODE"
                            };

int _pwmLVal[] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
int array[9];

const int _menuButton = 12;
int _menuButtonState = 0;
const int _upButton = 14;
int _upButtonState = 0;
const int _downButton = 13;
int _downButtonState = 0;
int menuPosition = 0;

bool sent = false;
bool connected = false;

unsigned long now = 0;
unsigned long now2 = 0;
unsigned long now3 = 0;
#define OLED_RESET LED_BUILTIN  //
Adafruit_SSD1306 display(OLED_RESET);

void sendCommand(IPAddress to, int data,  int id  ) {
  Serial.println(id);

  Udp.beginPacket(to, clientPort);
  Udp.write(NodeCxNames[id - 1]);
  Udp.write((uint8_t*)&data, 4); //cast to bytes
  Udp.write(NodeCxNames[id + 9]);
  Udp.endPacket();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  pinMode(_menuButton, INPUT);
  pinMode(_upButton, INPUT);
  pinMode(_downButton, INPUT);
  WiFi.persistent(0);
  WiFi.mode(WIFI_AP);
  while (!WiFi.softAP(ssid, password, 9, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, local_IP, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
  display.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
  display.display();
  Udp.begin(localUdpPort);
}

void defualtLayout() {
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.clearDisplay();
  for (int i = 0; i <= 8; i++) {
    display.print("L");
    display.print(i + 1);
    display.print(":");
    display.println(array[i]);
    if (i == 7) {
      display.setCursor(50, 0);
    }
  }
  if (connected) {
    display.setCursor(50, 50);
    display.println("Connected!");
  } else {
    display.println("DISCONNECTED!!");
  }
  display.display();
}

void _menuSelect(int x, int y, int _size, int Val, char item[]) {
  display.clearDisplay();
  display.setTextSize(_size);
  display.setCursor(x, y);
  if (menuPosition == 10) {
    display.println("Light:All");
  } else {
    display.print(item);
    display.println(menuPosition);
  }

  if (Val != 1002) {
    display.println(Val);
  }
  display.display();
}

void loop() {
  _menuButtonState = digitalRead(_menuButton);
  int _upButtonReading = digitalRead(_upButton);
  _downButtonState = digitalRead(_downButton);

  if (_upButtonReading == HIGH) {
    if (menuPosition > 0) {
      int r = menuPosition;
      _pwmLVal[r - 1] = _pwmLVal[r - 1] + 50;
      if (_pwmLVal[r - 1] > 1001) {
        _pwmLVal[r - 1] = 1;
      }
      sendCommand(Client1, _pwmLVal[r - 1], r);
      delay(200);
    }
  }


  if (_menuButtonState == HIGH) {
    menuPosition ++;
    if (menuPosition > 0 && menuPosition < 11) {
      _menuSelect(0, 0, 2, 1002, "Light:");
    } else {
      menuPosition = 0;
      defualtLayout();
    }
  }

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char verifyStart[7]; //NODEC1 plus null
    char verifyEnd[7]; //C1NODE plus null
    int len = Udp.read(incomingPacket, 800);
    strncpy (verifyStart, (char*)incomingPacket, 6);//6 bytes
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 6 );//6 bytes
    verifyStart[6] = 0; //null terminate
    verifyEnd[6] = 0; //null terminate
    if (strcmp(verifyStart, "NODEUD") == 0) {
      if (strcmp(verifyEnd, "UDNODE") == 0) {
        memcpy(&array, incomingPacket + 6 , sizeof(array)); //copy 4 bytes to _data
        now3 = millis();
        connected = true;
      }
    }
  }

  if (millis() - now3 >= 3000) {
    connected = false;
    now3 = millis();
  }

  if (millis() - now2 >= 1000 && menuPosition) {
    array[9] = array[0];
    _menuSelect(0, 0, 2, array[menuPosition - 1], "Light:");
    now2 = millis();
  }

  if (millis() - now >= 2000 && !menuPosition) {
    if (connected) {
      defualtLayout();
    }
    now = millis();
  }
}

The nodeud message is coming back as an array of integers i use for populating the lcd screen with the pwm value that the client is actually running

int array[9];
array[9] = array[0];

You still seem to be missing the point that the array indexes are zero based.

An array of 9 elements is array[0] ... array[8].

You don't "own" array[9] any more than you did array[10]. :frowning:

How many integers are you sending?

cattledog:

int array[9];

array[9] = array[0];




You still seem to be missing the point that the array indexes are zero based.

An array of 9 elements is array[0] ... array[8].

You don't "own" array[9] any more than you did array[10]. :( 

How many integers are you sending?

Oh my, your right. it went right over my head.

i use array[menuPosition - 1]
need to initialize the array as array[10]?

im sending 9 integers light 1-9 and a tenth entry i manually specify for light 'all"/10

menuPosition 1 = light 1 and menu position 10 = light all aka light 10. so i use array[menuPosition - 1] to get 'array[0]'

i need to initialize the array as array[10]?

Yes.

If the menuPosition is a Light, and they are Light 1 .... Light 9
Then when you use array[menuPosition -1] you have array indexes array[0] ... array[8].

If you want to add a menuPosition 10 for light all, then it will be array[9].

cattledog:
Yes.

If the menuPosition is a Light, and they are Light 1 .... Light 9
Then when you use array[menuPosition -1] you have array indexes array[0] ... array[8].

If you want to add a menuPosition 10 for light all, then it will be array[9].

so if i initialize an array as [9] im not allowed to use position 9 or the '10th' entry? i do understand that it starts at 0 that why when menuPosition=1 i use [menuPosition-1]. so when menuPosition=10 i use [menuPosition -1] which would be [9]?

so if i initialize an array as [9] im not allowed to use position 9 or the '10th' entry? i do understand that it starts at 0 that why when menuPosition=1 i use [menuPosition-1]. so when menuPosition=10 i use [menuPosition -1] which would be 9?

You seem to be confusing the declaration or an array, which specifies a number of elements in the array and the specific indexes used to access the elements in the array.

int array[9]; declares an array of 9 integers, but there is no individual element array[9]. The elements in a 9 integer array have indexes which run from 0 to 8. The indexes are zero based.

What i dont understand is why it work in the first place if i cant use

int array[9]

array[9]="1";
Serial.println(array[9]);

output: 1

because the above seems to work.,

Where can i read this because i have alaways used array[10] for an 11 position array

int array[9] creates:
array[0]
array[1]
array[2]
array[3]
array[4]
array[5]
array[6]
array[7]
array[8]

If your sketch then Reads array[9] or array[10] it is reading 2 bytes of data that some variable may have written to.
If your sketch Writes to array[9] or array[10] it is overwriting someone else's data, and all bets are off.

But it does state,

Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.

so i must have just got lucky all those times

thanks i learned something new

but now i think i get it

Good. I think the problem is that when we learn to count on our 10 fingers we go
1-2-3....8-9-10.

For zero based literacy we need to learn how to count our 10 fingers with
0-1-2...7-8-9.

What i don't understand is why it work in the first place

Sometimes you can be lucky. You had a whole other thread about your program crashing when you addressed memory you didn't own.

Where can i read this because i have always used array[10] for an 11 position array

Do you mean for the declaration, or the access?

This does not declare/create an 11 position array.

int array[10];

Sorry for all the threads edits, but now i wont forget that

So im trying to run an esp in ap and sta mode, so far it working fine it seems but im a little concerned with _data[] and null termination. from a python script i send the data as a single bytestring b"NODEUT1023UTNODE" sometimes the value in between is not the same length its up to '4'.

Im using atoi to convert the chars into an int,

int value = atoi(_data);

this seems to mostly work except im concerned with the output of the serial console when i print _data i choose to use terminate _data at position [4] but this only is valid for a 4 char string. either way it still works but should i be worried about anything. is there another way to do this?

serial output,

23:38:35.012 -> confirmed750⸮
23:38:35.052 -> confirmed850⸮
23:38:35.092 -> confirmed950⸮
23:38:35.172 -> confirmed1050
23:38:35.210 -> confirmed50H⸮
23:38:35.290 -> confirmed150⸮

receiving code,

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    int len = Udp.read(incomingPacket, 255);
    char verifyStart[7]; //NODEC1 plus null
    char verifyEnd[7]; //C1NODE plus null
    char _data[5];
    strncpy (verifyStart, (char*)incomingPacket, 6);//6 bytes
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 6 );//6 bytes
    verifyStart[6] = 0; //null terminate
    verifyEnd[6] = 0; //null terminate
    if (strcmp(verifyStart, "NODEUD") == 0) {
      if (strcmp(verifyEnd, "UDNODE") == 0) {
        memcpy(&array, incomingPacket + 6 , sizeof(array)); //copy 4 bytes to _data
        now3 = millis();
        connected = true;
      }
    }
    if (strcmp(verifyStart, "NODEUT") == 0) {
      if (strcmp(verifyEnd, "UTNODE") == 0) {
        strncpy (_data, (char*)incomingPacket + 6, len - 12);
        _data[4]=0;
        Serial.print("confirmed");
        Serial.println(_data);
        now3 = millis();
        int value = atoi(_data);
        sendCommand(Client1, value, 1);
        connected = true;
      }
    }
  }