0

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();
}

2 Answers 2

2
int m = 0;
...
    drawNo[m] = (char)keys;
...
    ++m;

No other references to m in your code. Therefore m keeps increasing and you are writing outside the bounds of the array drawNo, corrupting memory.

0
0

I doubt it is the count. Did you try moving it to above the serial print so that you can be sure of the report?

if (count == 16) {
        count = -1; /* <<<<<<<<<<<<<<<<<<<<<<<<<<<<Here is the problem*/       
        Serial.println("Counter equals 16 NOW");
        printScreen();
        delay(200);


        for (int i = 0; i < sizeof(drawNo);  i++) {
            drawNo[i] = (char)0;
        }
5
  • No problem still exists.. any other idea? Commented Jul 20, 2015 at 15:46
  • Before you must see "Counter equals 16 NOW" now with the changed code do you see "Counter equals 16 NOW"? Commented Jul 20, 2015 at 15:57
  • @ Visual Micro Ya it prints "Counter equals 16 NOW" on serial Monitor,and also I have printed the value of count and it also proved that count is -1 now. ! but why is that board restart?? Commented Jul 20, 2015 at 17:27
  • What Nick has said is right. It's because ++m is never reset Commented Jul 20, 2015 at 22:52
  • Thank you very much for your contribution. problem solved. Commented Jul 21, 2015 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.