Skip to content

Instantly share code, notes, and snippets.

@doojinkang
Created March 2, 2020 08:32
Show Gist options
  • Save doojinkang/23828cc6017524676721efb1fccacd4d to your computer and use it in GitHub Desktop.
Save doojinkang/23828cc6017524676721efb1fccacd4d to your computer and use it in GitHub Desktop.
// #define HC_05
#ifdef HC_05
// Arduino + HC_05 altserial
#define SERIAL_BAUD 19200
#include "AltSoftSerial.h"
AltSoftSerial altSerial;
#define MySerial altSerial
#else
// BLE NANO
#define SERIAL_BAUD 19200
#define MySerial Serial
#endif
// Common Anode I have
int red_light_pin= 3;
int green_light_pin = 5;
int blue_light_pin = 6;
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}
void setup() {
MySerial.begin(SERIAL_BAUD);
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
enum STATE {
COMD,
RVAL,
GVAL,
BVAL
};
uint8_t currentLED[] = { 0, 0, 0 };
uint8_t RGB_CMD[3]; // Stored by SCRATCH
// command format (4byte) : 0x81, R, G, B
boolean parseCmd(int c) {
static int CURRENT_STATE = COMD;
switch (CURRENT_STATE) {
case COMD:
if ( c == 0x81 )
CURRENT_STATE = RVAL;
return false;
case RVAL:
RGB_CMD[0] = c;
CURRENT_STATE = GVAL;
return false;
case GVAL:
RGB_CMD[1] = c;
CURRENT_STATE = BVAL;
return false;
case BVAL:
RGB_CMD[2] = c;
CURRENT_STATE = COMD;
return true;
}
}
void serialEvent1() {
while (MySerial.available()) {
if ( parseCmd(MySerial.read()) ) {
currentLED[0] = RGB_CMD[0];
currentLED[1] = RGB_CMD[1];
currentLED[2] = RGB_CMD[2];
RGB_color(255-currentLED[0], 255-currentLED[1], 255-currentLED[2]);
}
}
}
void loop() {
delay(1000);
MySerial.write(currentLED, 3);
serialEvent1();
}
@sinanthottan
Copy link

Hi sobreira,
I didn't post the code yet.
It's quite a long time ago :)
I remember I referred microbit scratch extensions.

Hi Doojunkang,
I was trying to connect HM13 (BLE) to scratch3. Referred Microbit extension code, but the connection fails immediately after successful connection. Could you help me with some tips, what could be the issue? or where would I look for the solution? I am kind of new to this.

Thanks in advance

@Yejin715
Copy link

Yejin715 commented Jan 23, 2024

Hello. Currently, I am also experiencing the same phenomenon where the connection is disconnected immediately after connecting to BT. I can't figure out which part is the problem...
After checking, it was found that the data received through Bluetooth was not output and continued to accumulate in the buffer, and that Bluetooth was turned off after a lot of data accumulated.
Is there any way I can refer to it or ask for advice?

@doojinkang
Copy link
Author

Hi, Yejin,
I have some BT modules, some of them caused the phenomenon you wrote.
Those BT modules are HC-05 with firmware version 4.0, and they are fake.

  • BLE is not BT.

@Yejin715
Copy link

Hi, Yejin, I have some BT modules, some of them caused the phenomenon you wrote. Those BT modules are HC-05 with firmware version 4.0, and they are fake.

  • BLE is not BT.
    Hello. Thank you for your kind response.
    May I know how you solved it? :(

@doojinkang
Copy link
Author

Yejin, if it is going with HC-05 with V4 firmware, no solution. It is fake HW issue.
If you mail me abut the trouble you face, I can give you an advice.

@Yejin715
Copy link

Yejin, if it is going with HC-05 with V4 firmware, no solution. It is fake HW issue. If you mail me abut the trouble you face, I can give you an advice.

I send my problem. This email : doojin.kang@gmail.com
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment