I'm running into a problem with a GikFun Arduino Nano. The symptom appears when I'm reading the receive buffer to clear it after an operation completes. The unit just appears to stop somewhere in the loop function and never continues.
I know that this unit has a CH340 UART on it which seems to have issues.
Any suggestions on what I can do?
OP Edits
This code is more or less based on the code from this Make: article. I originally had this assembled through an Arduino Uno R3 with an HC-06 (Kootek BT2S) module. Everything connected via solderless bread-board.
This entire circuit works properly with the UNO, which I had it running on SoftwareSerial using pins 2, 3. After putting this on a perf-board in a project box using the GikFun Nano, it was not behaving. I had a number of wiring issues, which I resolved, but still had issues communicating on the HC-06/SoftwareSerial. I re-did the wiring using pins 0, 1 for the Serial and removed the SoftwareSerial references. The circuit worked as I expected with the HC-06 working as it had before. But if I issued more than 1-2 commands via Bluetooth the unit would just get stuck. I also tried this without the HC-06 connected and just used the Serial Monitor through the IDE same behavior. It would randomly lock up after clearing the receive buffer.
Code - as requested:
const int statusLED = 13; //the pin you connect your status LED to
const int desiredBaud = 4;//index of your desired baud rate 4 is 9600
char* myPin = "1234";//change to the pin of your choice
const int memRed=0;
const int memGreen=1;
const int memBlue=2;
const int redPin= 9;
const int greenPin= 10;
const int bluePin= 11;
const int cmdLed= 12;
// Max Red - 255
const int max_red = 255;
// Max Green - 90
const int max_green = 255;
// Max Green - 100
const int max_blue = 255;
int red= 255;
int green= 255;
int blue= 255;
unsigned int baudLookup[] =
{
1200, //code 1
2400, //code 2
4800, //code 3
9600, //code 4
19200, //code 5
38400, //code 6
57600 //code 7
};
void setup()
{
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(cmdLed, OUTPUT);
pinMode(statusLED, OUTPUT);
digitalWrite(statusLED, HIGH);
Serial.begin(9600);
delay(1500);//wait for reset to complete
Serial.print("AT");
delay(1500);//wait for reset to complete
while(Serial.available()) {
Serial.read();
}
delay(1500);//wait for reset to complete
digitalWrite(statusLED, HIGH); //SUCCESS!
digitalWrite(redPin, map( red, 0, 255, 0, max_red) );
digitalWrite(greenPin, map( green, 0, 255, 0, max_green) );
digitalWrite(bluePin, map( blue, 0, 255, 0, max_blue) );
}
void loop()
{
digitalWrite(statusLED, LOW); //fast blink means success
delay(200);
digitalWrite(statusLED, HIGH);
delay(200);
String stat;
char buffer[13];
buffer[12]= 0;
if( Serial.available() >= 3 ) {
String stat = String("OK");
Serial.readBytes(buffer, 3);
buffer[3]= 0;
String command= String(buffer);
Serial.println(command);
if( command == "GET" ) {
char outBuffer[7];
sprintf(outBuffer, " %02x%02x%02x", red, green, blue);
stat += outBuffer;
}
else if( command == "SET" ) {
if( Serial.available() >= 6 ) {
Serial.readBytes(buffer, 6);
int redVal= (getVal(buffer[0])<<4) | (getVal(buffer[1]));
int greenVal= (getVal(buffer[2])<<4) | (getVal(buffer[3]));
int blueVal= (getVal(buffer[4])<<4) | (getVal(buffer[5]));
digitalWrite(redPin, map( redVal, 0, 255, 0, max_red) );
digitalWrite(greenPin, map( greenVal, 0, 255, 0, max_green) );
digitalWrite(bluePin, map( blueVal, 0, 255, 0, max_blue) );
red= redVal;
green= greenVal;
blue= blueVal;
char outBuffer[7];
sprintf(outBuffer, " %02x%02x%02x", redVal, greenVal, blueVal);
stat += outBuffer;
} else {
stat = "ERR";
}
}
else {
Serial.println("No Command");
}
Serial.println(stat);
Serial.println("Clearing receive buffer");
delay(1500);
while(Serial.available()) {
Serial.read();
delay(100);
}
delay(1500);
}
}
byte getVal(char c)
{
if(c >= '0' && c <= '9') {
return (byte)(c - '0');
} else {
return (byte)(c-'A'+10);
}
}