Im building a code to a rfid reader, a rdm6300 and a arduino pro micro.
the sensor reads the cards and led continualy does a spectrum loop.
But the sensor output that is stored in a variable is bugging and loosing some letters/numbers.
const int redPin = 6;
const int greenPin = 5;
const int bluePin = 3;
String ledloop = "on";
String scanresult = "";
#define STX 0x02
#define ETX 0x03
const size_t LEN = 24; // length of the expected message (e.g. 12 hex characters = 12 * 2 = 24)
char buffer[LEN+1];
int index = 0;
bool receiving = false;
void setup() {
// Start off with the LED off.
setColourRgb(0,0,0);
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
do {
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
scan();
delay(5);
}
}
} while (ledloop == "on");
}
void scan() {
if (Serial1.available() > 0) {
char serialByte = Serial1.read();
if (serialByte == STX) {
receiving = true;
index = 0;
} else if (serialByte == ETX || index == LEN) {
buffer[index] = '\0';
parse(buffer, index);
index = 0;
receiving = false;
} else if (receiving) {
buffer[index++] = serialByte;
}
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void parse(char* input, size_t length) {
scanresult = input;
Serial.println(scanresult);
}