Writing in two different char arrays[]

I am trying to write a code that lets you typ in the serial monitor.
you can only typ A to F or a to f and 0 to 9.
What i am trying to do here is to check the ASCII value and subtract the right value from it so that it will have Hex values.
I want to ignore the “space” if you for example typ in: “A0 B1”.
In array_buffer should be the values: 10,0,11,1.
But when i look at the serial monitor i get: 0,0,0,0.
any idea what i am doing wrong?

code arduino.PNG

you might be interested in this code that I use for debugging

// pcRead - debugging using serial monitor

const char version [] = "PcRead 200416a";

int debug = 0;

// ---------------------------------------------------------
// toggle output bit
void
pinToggle (
    int pin)
{
    static int  bits = 0;
    int     bit = 1 << pin;

    if (debug)  {
        Serial.print ("pinToggle: ");
        Serial.println (pin);
    }

    if (bits & bit)  {
        digitalWrite (pin, LOW);
        bits &= ~bit;
    }
    else {
        digitalWrite (pin, HIGH);
        bits |= bit;
    }
}

// ---------------------------------------------------------
// toggle output bit
int
readString (
    char *s,
    int   maxChar )
{
    int  n = 0;

    Serial.print ("> ");
    do {
        if (Serial.available()) {
            int c    = Serial.read ();

            if ('\n' == c)
                break;

            s [n++] = c;
            if (maxChar == n)
                break;
        }
    } while (true);

    return n;
}

// -----------------------------------------------------------------------------
// process single character commands from the PC
#define MAX_CHAR  10
char s [MAX_CHAR] = {};

void
pcRead (void)
{
    static int  analogPin = 0;
    static int  val = 13;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            break;

        case 'A':
            analogPin = val;
            Serial.print   ("analogPin = ");
            Serial.println (val);
            val = 0;
            break;

        case 'D':
            debug ^= 1;
            break;

        case 'I':
            pinMode (val, INPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT");
            val = 0;
            break;

        case 'O':
            pinMode (val, OUTPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" OUTPUT");
            val = 0;
            break;

        case 'P':
            pinMode (val, INPUT_PULLUP);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT_PULLUP");
            val = 0;
            break;


        case 'a':
            Serial.print   ("analogRead: ");
            Serial.println (analogRead (val));
            val = 0;
            break;

        case 'c':
            digitalWrite (val, LOW);
            Serial.print   ("digitalWrite: LOW  ");
            Serial.println (val);
            val = 0;
            break;

        case 'p':
            analogWrite (analogPin, val);
            Serial.print   ("analogWrite: pin ");
            Serial.print   (analogPin);
            Serial.print   (", ");
            Serial.println (val);
            val = 0;
            break;

        case 'r':
            Serial.print   ("digitalRead: pin ");
            Serial.print   (val);
            Serial.print   (", ");
            Serial.println (digitalRead (val));
            val = 0;
            break;

        case 's':
            digitalWrite (val, HIGH);
            Serial.print   ("digitalWrite: HIGH ");
            Serial.println (val);
            val = 0;
            break;

        case 't':
            Serial.print   ("pinToggle ");
            Serial.println (val);
            pinToggle (val);
            val = 0;
            break;

        case 'v':
            Serial.print ("\nversion: ");
            Serial.println (version);
            break;

        case '\n':          // ignore
            break;

        case '"':
            while ('\n' != Serial.read ())     // discard linefeed
                ;

            readString (s, MAX_CHAR-1);
            Serial.println (s);
            break;

        case '?':
            Serial.println ("\npcRead:\n");
            Serial.println ("    [0-9] append to #");
            Serial.println ("    A # - set analog pin #");
            Serial.println ("    D # - set debug to #");
            Serial.println ("    I # - set pin # to INPUT");
            Serial.println ("    O # - set pin # to OUTPUT");
            Serial.println ("    P # - set pin # to INPUT_PULLUP");
            Serial.println ("    a # - analogRead (pin #)");
            Serial.println ("    c # - digitalWrite (pin #, LOW)");
            Serial.println ("    p # -- analogWrite (analogPin, #)");
            Serial.println ("    r # - digitalRead (pin #)");
            Serial.println ("    s   - digitalWrite (pin #, HIGH)");
            Serial.println ("    t   -- toggle pin # output");
            Serial.println ("    v   - print version");
            Serial.println ("    \"   - read string");
            Serial.println ("    ?   - list of commands");
            break;

        default:
            Serial.print ("unknown char ");
            Serial.println (c,HEX);
            break;
        }
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    pcRead ();
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin(115200);

    Serial.println (version);
}

Here i think you are processing one charcater at the time and not saving them?
I want to typ one long string example: "01 04 D1 etc".
01 is the start bit.
04 is the packet length.
D1 is letting the code now if it will receive floats from the sensors.

Thats why i want to save it in a array.
But i dont want to save the spacebutton input (32).
because i will convert with the %2 function and multiple it with 16 so i will get hex value.
But i am struggling with writing it into another array.

i thought pcRead() would give you some ideas for how to deal with serial input. It has a mode to read a string (readString()) preceded by a quote (") and terminated by a '\n' and code to translate a string of chars into a decimal value.

so for case, if available() read a byte into an array and increment an index. I would just read a complete message and post process it rather than translate or skip values (e.g. space). The format you describe is similar to TLVs, so capture the length when the index is 0. If your messages are short, the first nibble can be the # of byte/chars.

not sure how you intend to handle a "start bit". A start bit is the first bit transmitted in a byte by the the UART.

seems somewhat inefficient to send binary data using ascii characters; requires twice as many bytes.

floats can always to scaled (x100) so that they can be transferred as integers

when I use pcRead(), i precede a command with a numeric value that can be processed by the command. if you can avoid the commands 'A' - F' you can accept hexadecimal values

dalawey:
I am trying to write a code that lets you typ in the serial monitor.
you can only typ A to F or a to f and 0 to 9.

This is the sketch that accomplishes the above tasks:

char myData[20];
int i = 0;
char x;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    x = Serial.read();
    if ((x >= 0x41) && (x <= 0x46)) //A to F
    {
      regData(x);
    }
    if ((x >= 0x61) && (x <= 0x66)) //a to f
    {
      regData(x);
    }
    if ((x >= 0x30) && (x <= 0x39)) // 0 to 9
    {
      regData(x);
    }
    if (x == 0x20)  //space
    {
      regData(x);
    }
  }
}

void regData(char t)
{
  myData[i] = t;
  Serial.print(t);
  i++;
}

Your original code doesn’t work because when you get a valid character you do...

b++;

Which adds one to the index b.

But then you try to do...

Serial.println(array_buffer[b]);

b is now “pointing” to the next (empty == 0) array element because you added 1 to “b” previously.

Try moving the Serial.println code inside every “valid” if statement, and before you increment b.
That’s what Golam has helpfully done for you, but also completely rewrote your code by using the regData function - and so it may not be obvious to you why your original code did not work....

I am trying to understand what you guys are saying but i can't get my head around it.
First it does the serial.read() so it will read a character and give it a ASCII value. Then it will put it in a array.
After that there are 3 if-statements. it will check each index and subtract a certain value if its true.
When it is in the if statement it writes it into another array and the b point to the index.
So it thought it would write the first character (if b=0) in array[0] and after that i wanted to increment it. So the next character will be writen in array[1] (b =1).
I wanted to print the array after each charachter.

Have you tested the sketch of Post#4? Is it working? If so, add codes with this foundation sketch of Post#4 to suit your needs. We have solved one prat of your problem -- how to accept and print only A - F, a - f, 0 - 9, and space.

dalawey:
I wanted to print the array after each charachter.

Serial.write(array, sizeof(array));