-
-
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(); | |
} |
Hi sobreira,
I didn't post the code yet.
It's quite a long time ago :)
I remember I referred microbit scratch extensions.
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
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?
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.
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? :(
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.
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 : [email protected]
Thank you :)
Hi Doojin Kang, hope everything is doing well.
Do you have posted the Scratch code for such connection?
Thanks!
Edson