Trouble with Functions and delays

I wanted to do a really quick project for my room called Albert where I display a little derpy face that blinks at random intervals and yawn occasionally every 1-2 minutes. It's pretty straight forward but I managed to trip up. Everything is working except that the yawning happens too frequently. The "random()" for rndYawn, or random yawn, returns values in the negatives and values outside of the maximum and minimum integer limit. On top of that, the increase in delay value doesn't seem to actually come out as 60 seconds but some shorter amount of time. I'm assuming that the delays and the functions overlapping might be a cause of the problem, but I'm not sure. If you didn't understand what I just said then let me know.

#include <IRremote.h>
#include <LedControl.h>

int DIN = 10;
int CS = 11;
int CLK = 12;

int irPn = 3;

IRrecv IR(irPn);
decode_results cmd;

LedControl lc(DIN, CLK, CS, 0);

int dt = 100;

int rndBlink;
int rndYawn;

byte CLEAR[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte nEutral[] {0x00, 0x66, 0x66, 0x66, 0x00, 0x3C, 0x00, 0x00};
byte bLink[] {0x00, 0x00, 0x66, 0x00, 0x00, 0x3C, 0x00, 0x00};
byte yAwn[] {0x00, 0x00, 0x66, 0x00, 0x3C, 0x3C, 0x3C, 0x00};

bool alB = false;

void setup() {
  Serial.begin(9600);

  IR.enableIRIn();

  lc.shutdown(0, false);
  lc.setIntensity(0, 5);
  lc.clearDisplay(0);

  pinMode(irPn, INPUT);
  pinMode(DIN, OUTPUT);
  pinMode(CS, OUTPUT);
  pinMode(CLK, OUTPUT);
}

void loop() {
  if (IR.decode(&cmd) == 1) {
    delay(dt);
    IR.resume();

    if (cmd.value == 0xFFA25D) {
      printByte(CLEAR);
      alB = false;
    }

    if (cmd.value == 0xFF02FD) {
      alB = true;
    }
  }

  if (alB == true) {
    playAlbertBlink();
    playAlbertYawn();
  }
}

void printByte(byte byteName[]) {
  int j;

  for (j = 0; j <= 8; j = j + 1) {
    lc.setRow(0, j, byteName[j]);
  }
}

void playAlbertBlink() {
  printByte(nEutral);

  rndBlink = random(2000, 10001);

  delay(rndBlink);
  printByte(bLink);
  delay(200);
  printByte(nEutral);
  delay(rndBlink / 4);
}

void playAlbertYawn() {
  int j;

  rndYawn = random(60000, 120001);
  Serial.println(rndYawn);

  for (j = 0; j <= rndYawn; j = j + 1) {
    if (j == rndYawn) {
      printByte(yAwn);
      delay(1800);
      printByte(nEutral);
    }
  }
  j = 0;
}

What is the maximum value that can be stored in an int?

1 Like

good hint @Coding_Badly

heres another

Ooohhh. That makes sense. Got any ideas on what I should do?

Think about it!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.