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
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)