CASE command working with manually typed character, but not BTserial character.

Hello everyone. Thank you for creating such an informative environment for new learners like myself! I have searched the forums, and Google, but I wasn't able to find an answer. Hopefully one of you will find a simple fix for this problem.

Please be gentle, I've been doing this only for one week :wink:

Summary: I can see my desired value (the letter A) on the Arduino serial monitor, but the "case 'A':" only responds when I manually type the letter A into Arduino's IDE serial monitor and click Send. Nothing happens when the letter A comes across via the BTserial I have connected.

Detail: I have two Nanos, each with its own HC-05. The HC-05s are hardware programmed to automatically find and pair with each other on power up. Nano #1 has 24 push-button switches, the values of which I would like to utilize wirelessly on Nano #2.

Nano #1 is running this code:
(EDIT) I should make clear that Nano #1 is only connected to power, no physical data connection to my PC.

#define NUMBER_OF_SHIFT_CHIPS   3  //How many shift register chips are daisy-chained.
#define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8   //Width of data (how many ext lines)
#define PULSE_WIDTH_USEC   5  //Width of pulse to trigger the shift register to read and latch.
#define POLL_DELAY_MSEC   1   //Optional delay between shift register reads.
#define BYTES_VAL_T unsigned long   //You will need to change the "int" to "long" If the NUMBER_OF_SHIFT_CHIPS is higher than 2

long ploadPin        = 8;  // Connects to Parallel load pin the 165
long clockEnablePin  = 9;  // Connects to Clock Enable pin the 165
long dataPin         = 11; // Connects to the Q7 pin the 165
long clockPin        = 12; // Connects to the Clock pin the 165

BYTES_VAL_T pinValues;
BYTES_VAL_T oldPinValues;
BYTES_VAL_T read_shift_regs()
{
    long bitVal;
    BYTES_VAL_T bytesVal = 0;

    /* Trigger a parallel Load to latch the state of the data lines,
    */
    digitalWrite(clockEnablePin, HIGH);
    digitalWrite(ploadPin, LOW);
    delayMicroseconds(PULSE_WIDTH_USEC);
    digitalWrite(ploadPin, HIGH);
    digitalWrite(clockEnablePin, LOW);

    /* Loop to read each bit value from the serial out line
     * of the SN74HC165N.
    */
    for(int i = 0; i < DATA_WIDTH; i++)
    {
        bitVal = digitalRead(dataPin);

        /* Set the corresponding bit in bytesVal.
        */
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

        /* Pulse the Clock (rising edge shifts the next bit).
        */
        digitalWrite(clockPin, HIGH);
        delayMicroseconds(PULSE_WIDTH_USEC);
        digitalWrite(clockPin, LOW);
    }

    return(bytesVal);
}

/* Dump the list of zones along with their current status.
*/
void display_pin_values()
{
    Serial.print("Pin States:\r\n");

    for(int i = 0; i < DATA_WIDTH; i++)
    {
      // Adjust log wording here********************************
        Serial.print("P-");
        Serial.print(i);
        Serial.print(":");

   if((pinValues >> i) & 1)
            Serial.print("A");
        else
            Serial.print("L");

        Serial.print("\t");
    }

    Serial.print("\r\n");
}

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

    /* Initialize our digital pins...
    */
    pinMode(ploadPin, OUTPUT);
    pinMode(clockEnablePin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, INPUT);

    digitalWrite(clockPin, LOW);
    digitalWrite(ploadPin, HIGH);

    /* Read in and display the pin states at startup.
    */
    pinValues = read_shift_regs();
    display_pin_values();
    oldPinValues = pinValues;
}

void loop()
{
    /* Read the state of all zones.
    */
    pinValues = read_shift_regs();

    /* If there was a chage in state, display which ones changed.
    */
    if(pinValues != oldPinValues)
    {
        Serial.print("*Pin value change detected*\r\n");
        display_pin_values();
        oldPinValues = pinValues;
    }

    delay(POLL_DELAY_MSEC);
}

I've even tried adding spaces before and after the A - ie. " A " - with no luck.

Nano #2 is running this code:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX    **THESE ARE BACKWARDS FROM THE HARDWARE PINS**

void setup() {
  // initialize serial communication:
  Serial.begin(9600); 
  BTSerial.begin(9600); 
  
}

void loop() {
  if (BTSerial.available())
    Serial.write(BTSerial.read());

  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    
    // do something different depending on the character received.  
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For 
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
    case 'A':    
      digitalWrite(13, HIGH);
      break;
    
    default:
      // turn all the LEDs off:
      for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    } 
  }
}

Any help or suggestions would be more than appreciated.

Thanks in advance.
-Patrick

(Attachment: Pic of serial monitor showing Pin 5 = A)

5A.jpg

What if you receive an 'A' and turn the LED on then 1mS later your received a '\r' and turn it off. Would you see that pulse?


Rob

Graynomad:
What if you receive an 'A' and turn the LED on then 1mS later your received a '\r' and turn it off. Would you see that pulse?


Rob

Rob, Thanks for the reply!

I'm sorry, but I'm not sure I follow your meaning..... Does /r act as a way to turn pin 13 back low? I have tested typing /r into the serial monitor and I don't get any change in the LED. It remains on. Can you please elaborate?

Thanks in advance.
-Patrick

From what I can see your transmitting code does this

   if((pinValues >> i) & 1)
            Serial.print("A");
 ...
    Serial.print("\r\n");

That's 3 bytes being sent, each about 1mS after the other. Then the receiving code does

   switch (inByte) {
    case 'A':    
      digitalWrite(13, HIGH);
      break;
    
    default:
      // turn all the LEDs off:

So the 'A" turns the LED on and 1mS later the '\r' turns it back off again because that's what every character except 'A' does due to the default case.

Or have I got the wrong end of the stick?


Rob

I haven't noticed that to be what's happening. No matter what comes across the serial monitor, nothing happens. If I type A and press send in the IDE, the LED turns on. After resetting the Arduino (LED is back to off), typing A/r or Ar or Aanything here and pressing send keeps the LED lit.

I'm not sure if this information helps, but it seems that no matter what comes across the serial monitor, nothing affects the LED.

I changed the code to test a theory, same results. No LED on receiving Arduino when I boot up sending Arduino.

  {
 
    Serial.print("A");

  }

After booting, I see the "A" pop up on the serial monitor, but no response from the LED. When I type the letter A and press send, LED turns on. I'm stumped. :-\

Thanks again for any suggestions or thoughts.

--Patrick