Go Down

Topic: One Line Breaks Code (Read 121 times) previous topic - next topic

matt_hackerman

Hi all,

Without said line of code (in comments at bottom) my adafruit code runs smoothly and displays the round with round number on the lcd screen. However, when I uncomment the code, the screen flickers rapidly like it is constantly printing the start message and clearing or something.

Any help is greatly appreicated. Don't grill me ;)

Code: [Select]


#include <Wire.h>
#include <Adafruit_RGBLCDShield.h> //using lcd screen mathods
#include <utility/Adafruit_MCP23017.h>
#include <stdlib.h>
#include <string.h>
#define RED 0x1 //colour red
#define GREEN 0x2 //colour green
#define WHITE 0x7 //colour white
#define For(x) for(int i=x;i>0;i--) //used to clear length 'x' of screen
using namespace std;
Adafruit_RGBLCDShield screen = Adafruit_RGBLCDShield(); //var screen represents the lcd
int pinPress = 6;
int inputCount, lives, seqLength;
bool correct, win, complete; //checks if user input is correct, checks if round is won.
char arrDirs[4]; //holds each choosable direction for random sequence generator
unsigned long mil; //holds time passed of program for random sequence generator
struct buttons {
  char dir;
  char* button;
} up, down, left, right;

void setup() {
  screen.begin(16,2);
  up.dir = 'U';
  up.button = "up";
  down.dir = 'D';
  down.button = "down";
  left.dir = 'L';
  left.button = "left";
  right.dir = 'R';
  right.button = "right";
  arrDirs[0] = up.dir;
  arrDirs[1] = down.dir;
  arrDirs[2] = left.dir;
  arrDirs[3] = right.dir;
  screen.print("PRESS SELECT");
  screen.setCursor(0, 1);
  screen.print("TO START");
}

void loop() {
  byte input = screen.readButtons();
  if (input) {
    if (input & BUTTON_SELECT) {
      lives = 3;
      seqLength = 1;
      win = true;
      correct = true;
      complete = false;
      clearScreen();
      while(lives > 0) {
        screen.print("ROUND ");
        screen.print(seqLength);
        delay(2000);
        clearScreen();
        delay(3000);
        char sequence[1000];
        char currentChar;
        for (int i=0;i<seqLength;i++) {
          mil = millis();
          if (i%3 == 1) {
            currentChar = arrDirs[random(static_cast<int>((mil-i/1000)%4))+1];
          }
          else {
            currentChar = arrDirs[random(static_cast<int>((mil-i/1000)%4))];
          }
          screen.print(currentChar);
          sequence[i] = currentChar;
          delay(500);
          clearScreen();
        }
        lives = -1;
        //screen.print(userInput(sequence)); <-- BROKEN LINE
      }
    }
  }
}

char* userInput(char sequence[]) { //DODGY FUNCTION
  bool ans = true;
  byte input = screen.readButtons();
  char uInput[1000];
  inputCount = 0;
  while(inputCount < seqLength) {
    if (input) {
      if (input & BUTTON_LEFT) {
        uInput[inputCount] = 'L';
        ++inputCount;
      }
      else if (input & BUTTON_RIGHT) {
        uInput[inputCount] = 'R';
        ++inputCount;
      }
      else if (input & BUTTON_DOWN) {
        uInput[inputCount] = 'D';
        ++inputCount;
      }
      else if (input & BUTTON_UP) {
        uInput[inputCount] = 'U';
        ++inputCount;
      }
    }
  }
  For(seqLength) {
    if (uInput[i] != sequence[i]) {
      ans = false;
    }
  }
  if (ans) {
    return "CORRECT";
  }
  else {
    return "INCORRECT";
  }
}
         
void clearScreen() {
  screen.setCursor(0,0);
  For(16) {screen.print(" ");}
  screen.setCursor(0, 1);
  For(16) {screen.print(" ");}
  screen.setCursor(0,0);
}


jremington

#1
Mar 26, 2020, 02:14 am Last Edit: Mar 26, 2020, 03:24 am by jremington
There appear to be several problems with that code.

But to start, what Arduino are you using?

Why is "char sequence[1000]" declared that size, and locally?

Did you forget that character arrays used as C-strings must be zero terminated?

Robin2

#2
Mar 26, 2020, 09:42 am Last Edit: Mar 26, 2020, 09:42 am by Robin2
Without said line of code (in comments at bottom) my adafruit code runs smoothly and displays the round with round number on the lcd screen. However, when I uncomment the code, the screen flickers rapidly like it is constantly printing the start message and clearing or something.
Is this a program you wrote yourself? If so, at what point in the development did this problem emerge?

What is the program intended to do? Can you write a description of how the different parts are intended to work?


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TheMemberFormerlyKnownAsAWOL

#3
Mar 26, 2020, 10:36 am Last Edit: Mar 26, 2020, 10:42 am by TheMemberFormerlyKnownAsAWOL
Yup a thousand chars on the stack is going to break stuff, particularly when there's another one just behind it.

Quote
like it is constantly printing the start message and clearing
Because it is?
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Luckyfish13

also:

Code: [Select]

      lives = 3;

        ...

      while(lives > 0) {

        ...

        lives = -1;

      }


is probably why the screen's flickering...

1000 character array is ambitious! lol...

TheMemberFormerlyKnownAsAWOL

... Two 1000 character arrays even more so.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Go Up