Serial Communication PC/ArduinoUNO Problem

Hello to everybody outthere … I’m at my first sketch and post on the forum, so forgive me if I entred the post in the wrong forum section.

I just received an Arduino UNO as a gift for X-mas, and I thought it could be fine to use it to establish communication the PC and the model train pike, a friend of mine is setting up.

I figure out the amount of I/O for the model train will be in the range on some 150 I/O (mostly digital outputs to turnouts, semaphores, track blocks, and few digital inputs for presence detectors (photocells).
The interlocking will be performed by the PC with a VB2019 code, interfacing with Arduino.

So, right now I’m just playing with 3 LED to be turned on and Off from the PC (pls see the attched pic 3LED.png)

As for the Serial communication I derived the sketch from this topic (neither changed the name to the variables) … here the code ( that I understand is named “sketch”)

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

//============

void setup() {
  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT); 
    Serial.begin(9600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        digitalWrite(integerFromPC, messageFromPC);
        newData = false;
        
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atof(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);
}

What is making me crazy is that the LED do turn ON … but they DON’T turn OFF

Using the Serial Monitor I obtain the same result !! (see attched pic)

Thanks for any hint

What do you see as the output in the showParsedData() function ?

Images from Original Post so we don't have to download them. See this Simple Image Posting Guide

...R

Thanks for the hint :smiley:

From the program in the Original Post …

digitalWrite(integerFromPC, messageFromPC);

The text “high” (or “HIGH”) is not the same as

digitalWrite(integerFromPC, HIGH);

In the correct code HIGH is a constant with the value 1 and LOW has the value 0

Your program needs to be modified slightly so that you can send (for example) <6,1> where 6 is the number of the I/O pin and 1 is the value to turn the LED on. In other words, just send two integers.

…R

Tanks a lot for the prompt answer ... now it works perfectly !!!
:slight_smile: