Using Serial1.read() to work with serial inputs [SOLVED]

I am attempting to start and stop an internal timer that calls an ADC sample in the Particle Photon using serial commands to control this functionality. I am using the RX/TX pins connected to a standard use bluetooth unit (JY-MCU H6), and the Serial1.read() function does not seem to comprehend any of the inputs I send while using my serial terminal (Termite). My code is below:

int analogPin = A0;             // EMG connected to analog pin A0
int val = 0;                    // variable to store the read value
int count = 0;

void Hertz()
{
    count++;
    val = analogRead(A0);  // read the input pin
    Serial1.println(count);
    Serial1.println(val);	
}

Timer timer(2, Hertz);  // 500 Hz

void setup()
{
   Serial1.begin(38400);
   pinMode(D7, OUTPUT);
   setADCSampleTime(ADC_SampleTime_480Cycles);
}

void serial1Event()
{
    if (Serial1.available())
    {
        char inByte = Serial1.read();
        if (inByte == '1'){
            digitalWrite(D7, HIGH);
            Serial1.println(Time.now());        //unix time stamp
            timer.start();
        }
        else if(inByte =='2'){
            digitalWrite(D7, LOW);
            timer.stop();
            Serial1.println(Time.now());        //unix time stamp
            count = 0;
        }  
    }  
}
void loop(){
    serial1Event();
}

It should be noted, that when I replace every “Serial1.xxx” with “Serial.xxx” (ie, change the serial port FROM the RX/TX pins TO the usb port), the code operates flawlessly. I have done a fair amount of research on serial port operations in the Photon (both on this site, and elsewhere) and have seem to hit a wall. Any advice on how to successfully navigate this Serial1.read() operation would be greatly appreciated.

Thanks.

Hi @aStringer4

I just have to ask because it has happened before: You have TX of the Photon connected to RX of the bluetooth module and vice-versa, right? That bluetooth module is capable of 3.3V operation, so you won’t need any resistors as in the Arduino circuits. You just need 3.3V and GND and then TX and RX connected to their opposite.

1 Like

Correct, RX/TX are inversely connect between the two chips. I should also mention that Serial1.print() functions correctly show up in the terminal, but the Serial1.read() functions are not functioning properly.

1 Like

Could you add two else cases to catch and Serial.print() all possible outcomes of your tests?
Maybe your data gets corrupted and hence your branches don’t get executed, but knowing if and how would be required to understand the cause.

1 Like

UPDATE

I discovered there was an issue with the Bluetooth unit I was using. I followed this guide

in order to change the baud rate to 38400, and it was having additional side effects on the module. After restoring the bluetooth unit to its original functionality, Serial1.read() is now functioning properly (but I am limited to 9600 baud for the time being). However, I have now discovered some heavy delay/overlap timing issues with my serialEvent() calling my timer operations, so I am now looking into this new problem. Any direction steering to useful information would be greatly appreciated. For now, I am making my rounds through the Software Timer section of the Particle References: https://docs.particle.io/reference/firmware/core/#software-timers

The more info you provide the more likely you get some useful info back.

SOLVED

After looking more closely at the Bluetooth unit I was using, I discovered the JY-MCU HC-06’s firmware has some issues interacting with the Particle Photon. Although an exact solution could not be determined, when using the HC-05 bluetooth unit (still following the arduino guide to change its baud rate to 38400 https://github.com/rwaldron/johnny-five/wiki/Getting-Started-with-Johnny-Five-and-JY-MCU-Bluetooth-Serial-Port-Module), my original posted code worked fine. There was an issue with the timer priority causing the timer.stop() function call to not be executing, but I was able to fix this using pinned interrupts.

Sorry I’m not able to provide more specifics than this, but long story short, replacing the HC-06 with the HC-05 solved my problem.