I want to assign -1 to count variable again when count == 16 but when I have assigned -1 inside the if condition I have marked below in the code, the Arduino restarts.
Can anyone guess what is wrong here?
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <SPI.h>
#define DISPLAY_WIDTH 16
// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard(&Usb);
int count = -1;
char drawNo[4];
int m = 0;
int turn;
class KbdRptParser : public KeyboardReportParser {
void PrintKey(uint8_t mod, uint8_t key);
protected:
virtual void OnKeyDown(uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) {
uint8_t c = OemToAscii(mod, key);
count++;
if (c)
{ OnKeyPressed(c); }
}
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key) {
static uint32_t next_time = 0; //watchdog
static uint8_t current_cursor = 0; //tracks current cursor position
if (millis() > next_time) {
lcd.clear();
current_cursor = 0;
delay(5); //LCD-specific
lcd.setCursor(0, 0);
}//if( millis() > next_time ...
next_time = millis() + 200; //reset watchdog
if (current_cursor++ == (DISPLAY_WIDTH + 1)) { //switch to second line if cursor outside the screen
lcd.setCursor(0, 1);
}
int keys = (int) key;
if (count == 4 | count == 5 | count == 6 | count == 7) {
turn++;
drawNo[m] = (char)keys;
//lcd.print(drawNo[m]);
Serial.print(drawNo[m]);
Serial.print(m);
++m;
}
if (count == 16) {
Serial.println("Counter equals 16 NOW");
printScreen();
delay(200);
count = -1; /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<Here is the problem*/
for (int i = 0; i < sizeof(drawNo); i++) {
drawNo[i] = (char)0;
}
}
}
void printScreen() {
int j;
lcd.print(" DRAW NO : ");
for (j = 0; j < 4; j++) {
lcd.print(drawNo[j]);
}
}
KbdRptParser Prs;
void setup() {
Serial.begin(115200);
Serial.println("Start");
if (Usb.Init() == -1) {
Serial.println("OSC did not start.");
}
delay(200);
Hid.SetReportParser(0, (HIDReportParser *)&Prs);
// set up the LCD's number of columns and rows:
lcd.begin(DISPLAY_WIDTH, 2);
lcd.clear();
lcd.noAutoscroll();
lcd.print("Scan Your Code");
delay(200);
}
void loop() {
Usb.Task();
}