What does buf [x] mean ?

I’ve tried to modify some existing code to do what i want (make a 2 key keyboard) but i have no idea what the buf [2] actually means, Also the void releasekey at the end, what does that mean/do, Thanks very much all :slight_smile:

uint8_t buf[8] = { 
  0 };

#define PIN_Z 5
#define PIN_X 6

int state = 1;

void setup() 
{
  Serial.begin(9600);
  pinMode(PIN_Z, INPUT);
  pinMode(PIN_X, INPUT);
  digitalWrite(PIN_Z, 1); 
  digitalWrite(PIN_X, 1);
  delay(200);
}

void loop() 
{
  state = digitalRead(PIN_Z);
  if (state != 1) {
    buf[2] = 29; 
    Serial.write(buf, 8);
    releaseKey();
  } 

  state = digitalRead(PIN_X);
  if (state != 1) {
    buf[2] = 27; 
    Serial.write(buf, 8);
    releaseKey();
  } 
}

void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); 
  delay(100);
}
uint8_t buf[8] = { 0 };

This declares a variable array. instead of 7 variables buf1, buf2 etc...

buf[2] = 29;

This assigns the value 29 to the 3rd field in the array.
See more information.

Perehama:

uint8_t buf[8] = { 0 };

This declares a variable array. instead of 7 variables buf1, buf2 etc...

buf[2] = 29;

This assigns the value 29 to the 3rd field in the array.
See more information.

"instead of 8 variables buf0, buf1 etc"

Oh so would buf [3] just be the 4th variable ?

vegemalation:
Oh so would buf [3] just be the 4th variable ?

exactly.

Even an aged and non-experienced programmer like I encounters this problem of counting -- should start counting from 0 or from 1.

Say, we have an LCD with 4 rows (4 lines). How should we refer to the Topmost line? Is it Line-1 or Line-0? If we use Line-1, we are using decimal counting style where the very first thing is addressed as number 1. It is fine as a natural way of counting and referring in human world. When we use Line-0, we are using indexing method where the very first thing is addressed as number 0. It is fine as a natural way of counting and referring in binary/programming world.

In programming, we remain strict to the style of counting. buf[3] refers to the element with index 3, and of course it is the 4th item of the array? Now the meanings are --

uint8_t buf[8]; stands for an array which has 8 elements (members); each member holds 8-bit unsigned quantity.

buf[0] refers to the member with index 0 (the very first item).
.......
buf[7] refers to the member with index 7 (the very last item).

The answer that you actually need is "go do a C++ tutorial". Google "C++ tutorial", and work your way through the first half - ignoring things that are specific to programs running on full machines (stdin/stdout, the String class, dynamic memory, using main() ). It will cover functions, arrays, variable scope and lifetime, basic stuff that you need to know.

TolpuddleSartre:
"instead of 8 variables buf0, buf1 etc"

You would only have 7 in a buf[8]. You need to have one more space for a null terminator.

You would only have 7 in a buf[8]. You need to have one more space for a null terminator.

The concept of the insertion (manual/auto) of null-byte comes when we are talking about char type array containing 8-bit values for which there are ASCII charcaters . Here, we have byte (uint8_t) type array containing byte (8-bit) values for which there may or may not corresponding ASCII characters.

void setup() 
{
Serial.begin(9600);
char myArray[2] = {'A', 'B'}; //{'A', 'B', '\0'}  //auto/manual insertion of null-byte
Serial.println(myArray);                                //allowed because of char keyword

byte xArray[2] = {0x41, 0x23};  //no auto insertion of null-byte and even not needed
//Serial.println(xArray);               //not allowed because of uint8_t (byte) keyword.
}

void loop() 
{

}

The concept of the [null terminator] comes when we are talking about char type array

Even then, you only need the null terminator if you're dealing with the char array collectively as a "C String"; it's the libc string functions that need the null terminator.

Perehama:
You would only have 7 in a buf[8]. You need to have one more space for a null terminator.

We're not talking about strings here.