I know this is a super basic question, and I did try debugging, but somehow I think I am missing the way variable stores value in Arduino.

int enablePin = 2; // ebanble pin for slave

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

bool tx_master = false;
void setup() {
  // initialize serial:
  // setup enable pin for slave (always low)
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
  // set digital pin 8 - 11 for output
  for (int i = 8; i < 12; i++) {
    pinMode(i, OUTPUT);

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    // make sure this message for slave "A"
      Serial.println("PRINT LOCAL VARIABLE");      
      String akshay = "2AKSHA";
    if (inputString.charAt(0) == '2') { // check if message for SLAVE 1
      Serial.println("MESSAGE FOR SLAVE 2");
      tx_master = true; 
      for (int i = 1; i < 5 ; i++) {
        if (inputString.charAt(i) == '1')
          digitalWrite(7 + i, HIGH);
          digitalWrite(7 + i, LOW);
    } else {
      Serial.println("MESSAGE NOT FOR SLAVE");
    // clear the string:
      inputString = "";
      tx_master = false;
    stringComplete = false;

  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
  • Now in this program i have these 2 lines String akshay = "2AKSHA"; Serial.println(akshay.charAt(0)); which give me output as "2AKSHA" and "2"
  • Now the next 2 lines Serial.println(inputString); Serial.println(inputString.charAt(0)); does same thing here inputString value is "2AKSHA" but here my output is "2AKSHA" and "S"

Now i am not sure why charAt(0) gives me "S" for global variable and not for local variable.

Other solution i tried -

  1. I tried to use substring(0,1) it returned same "S"
  2. Assigned inputString to local variable and tried to get charAt it returned "S"
  3. Thought there could be space so did trim but still same "S"
  4. Tried to get IndexOf but same result.
  • @jsotola yes i am dealing with different variable but it has same value as 1st 2 lines, if i print it clearly shows me same value. But when i try to get charAt(0) it gives me "S". Only difference is one Global and one local, i even tried to assign that to local variable but no luck.. – Akshay May 15 '19 at 0:43
  • 1
    what are the values of inputString.charAt(1) and inputString.charAt(2) ? – jsotola May 15 '19 at 0:57
  • @jsotola it helped me to resolve issue, i was sending 2 messages i used one message on master which seems to be transfer to slave first and then next one after that, so slave always received 2 values and but that variable always had 1st stored in it. Thanks your (1) and (2) trick helped me to debug – Akshay May 15 '19 at 1:19
  • @Akshay if you send message for other slave and then many for this slave, what happens? I suspect it gets jammed and eventually it'll crash as you're not clearing inputString if it's not starting by '2'. – KIIV May 15 '19 at 8:19
  • 1
    @KIIV means once i find that the message is for this slave in that same if condition i can clear it. Let me know if you think other way.. – Akshay May 15 '19 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.