0

For some context, I’m working on what is basically “Simon” for an Arduino project. I’m using an ATMEGA2560 and editing the code with Atmel Studio 6.1. The project uses the random number generator to create the sequence for the button input, which is where I’m running into trouble.

My current method of adding some variance is to use a timer interrupt to seed the random number, by incrementing a number to be processed through an equation. However, the timer simply doesn’t work – and the number doesn’t increment. Am I doing anything wrong with initialization, or is something else at fault?

Code:

#define F_CPU 1000000UL  // 1 MHz, for timer

#include <avr/io.h> // normal stuff
#include <avr/interrupt.h> // timer interrupt
#include <util/delay.h> // easy delay functions
#include <stdlib.h> // random function

// global var that timer uses
volatile uint8_t count;

// function prototypes
int generateSeq(); // generates random number
int getRandomNumber(); // also generates random number (?)



int main(void)
{

  // variables
  int sequence[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // actual sequence
  int terms = 0; // terms in sequence
  int gameState = 0;
  int ifWrong = 0; // if sequence is wrong


  // timer interrupt (WHAT AM I DOING WRONG)
  TCCR0A |= (1<<CS02)|(1<<CS00);
  TIMSK0 |= (1<<TOIE0);
  TCNT0 = 0;
  count = 1;
  sei();

  while(1)
  {
    // actual "game" part
    while(gameState == 1)
    {

      // generate term in sequence
      // 1 = up, 2 = right, 3 = down, 4 = left

      if(ifWrong == 0)
      {
        sequence[terms] = generateSeq(); // call sequence function
        terms++;
      }
    }
  }
}

// random seed for sequence generator (something wrong here?)
ISR(TIMER0_OVF_vect)
{
  count++;

  if(count >= 255)
  {
    count = 1;
  }
}

int generateSeq() // function to generate sequence
{
  // equation is currently a placeholder
  int r2 = (int)rand() * (int)count;
  int num = r2 * count;
  return (num % 4) + 1;
}
  • This does not look like typical Arduino code. So the term Arduino in the headline is a bit missleading, as this typically refers to both, the hardware and the software/toolchain. – too honest for this site Mar 26 '16 at 02:24
  • `int generateSeq();` is not a valid function prototype! – too honest for this site Mar 26 '16 at 02:30
  • Noticed that your timer initialization is wrong. Clock select flags for ATmega2560's Timer0 is located in TCCR0B not TCCR0A. Other than that, your code should work fine. As an aside, TIMER0_OVF_vect is already used for Arduino's internal function. Not that it matters to you, since you don't seem to be using Arduino's SDK. I think you might get better response tagging your question with ATmega instead of Arduino. Good luck. – bot1131357 Mar 26 '16 at 04:08
  • @bot1131357 Your suggestion managed to fix my problem with the timer. Thanks a bunch! – user6116189 Mar 27 '16 at 19:54

1 Answers1

0

gameState is 0, and never changed, so none of the generateSeq() code is called.

You also have a serious problem with sequence[terms] in that you don't check if terms is between 0 and 7. You will run off the end of the array.

uncleO
  • 7,997
  • 2
  • 20
  • 37