Pages: [1]   Go Down
Author Topic: Sketch keeps going  (Read 357971 times)
0 Members and 2 Guests are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay so what i'm trying to do is make an LED dice based on the tutorial HERE http://www.instructables.com/id/Arduino-Led-Dice/ The "die" rolls and displays a number between 1&6I got the sketch he wrote up and it runs perfectly. But being the tinkerer i am i wasnt satisfied. Eventually i want to involve two die on one breadboard to play a "game" of sorts. When one player wins a sound plays (right now it is set to play a sound byte from SMB) using the tone library. Right now while im troubleshooting i have it to play the tone when a 4 is rolled. The problem im having is that it will randomly pick a number and then "print" it to the die (using the "disp" function) multiple times instead of just one. This makes no visible change but when a 4 is rolled the sound will play more then once. It seems almost random how many times is is sent into the disp function but its always between 1 and 3. I'm very new to arduino and would probably consider this my first real project and I've tried everything i can think of. Any help would be much appreciated

My code isn't commented at all because its nowhere near completed but if you have question about what any part of the code does please ask.

disp(int rand)  ---  Displays the random number on the die

blank() ---  Clears the die completly turning all LEDs off

play_rtttl(char *p) --- plays the sound when called

Code:
int pin1 = 4;
int pin2 = 6;
int pin3 = 7;
int pin4 = 5;
int buttonPin = 2;
int ran;

#include <Tone.h>
Tone tone1;
#define OCTAVE_OFFSET 0

int notes[] = {
  0,
  NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
  NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
  NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
  NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};
char *song = "smbdeath:d=4,o=5,b=90:8p,16b,16f6,16p,16f6,16f.6,16e.6,16d6,16c6,16p,16e,16p,16c";

void setup ()
{
  tone1.begin(3);
  pinMode (pin1, OUTPUT);
  pinMode (pin2, OUTPUT);
  pinMode (pin3, OUTPUT);
  pinMode (pin4, OUTPUT);
  pinMode (buttonPin, INPUT);
  randomSeed(analogRead(A1));
}
#define isdigit(n) (n >= '0' && n <= '9')
void loop()
{
  while(digitalRead(buttonPin)==HIGH)
  {
    ran = random(1,7);
    for(int x = 1;x<=3;x++){
      blank();
      if(x==1){
        digitalWrite(pin2,HIGH);
      }
      if(x==2){
        digitalWrite(pin3,HIGH);
        digitalWrite(pin1,HIGH);
      }
      if(x==3){
        digitalWrite(pin4,HIGH);
      }
      delay(45);
      if(digitalRead(buttonPin)==LOW)
      {
        blank();
        disp(ran);
      }
    }
  }
}



void disp(int rand)
{
  switch(rand)
  {
  case 1:
    digitalWrite (pin4, HIGH);
    break;
  case 2:
    digitalWrite (pin1, HIGH);
    break;
  case 3:
    digitalWrite (pin3, HIGH);
    digitalWrite (pin4, HIGH);
    break;
  case 4:
    digitalWrite (pin1, HIGH);
    digitalWrite (pin3, HIGH);
    break;
  case 5:
    digitalWrite (pin1, HIGH);
    digitalWrite (pin3, HIGH);
    digitalWrite (pin4, HIGH);
    break;
  case 6:
    digitalWrite (pin1, HIGH);
    digitalWrite (pin2, HIGH);
    digitalWrite (pin3, HIGH);
    break;
  }
  if(rand == 4)
  {
    play_rtttl(song);
  }
}




void blank()
{
  digitalWrite(pin1,LOW);
  digitalWrite(pin2,LOW);
  digitalWrite(pin3,LOW);
  digitalWrite(pin4,LOW);
}





void play_rtttl(char *p)
{
  byte default_dur = 4;
  byte default_oct = 6;
  int bpm = 63;
  int num;
  long wholenote;
  long duration;
  byte note;
  byte scale;

  while(*p != ':') p++;
  p++;

  if(*p == 'd')
  {
    p++;
    p++;              // skip "d="
    num = 0;
    while(isdigit(*p))
    {
      num = (num * 10) + (*p++ - '0');
    }
    if(num > 0) default_dur = num;
    p++;                   // skip comma
  }
  if(*p == 'o')
  {
    p++;
    p++;              // skip "o="
    num = *p++ - '0';
    if(num >= 3 && num <=7) default_oct = num;
    p++;                   // skip comma
  }
  if(*p == 'b')
  {
    p++;
    p++;              // skip "b="
    num = 0;
    while(isdigit(*p))
    {
      num = (num * 10) + (*p++ - '0');
    }
    bpm = num;
    p++;                   // skip colon
  }
  wholenote = (60 * 1000L / bpm) * 4;
  while(*p)
  {
    num = 0;
    while(isdigit(*p))
    {
      num = (num * 10) + (*p++ - '0');
    }

    if(num) duration = wholenote / num;
    else duration = wholenote / default_dur;  // we will need to check if we are a dotted note after

      // now get the note
    note = 0;

    switch(*p)
    {
    case 'c':
      note = 1;
      break;
    case 'd':
      note = 3;
      break;
    case 'e':
      note = 5;
      break;
    case 'f':
      note = 6;
      break;
    case 'g':
      note = 8;
      break;
    case 'a':
      note = 10;
      break;
    case 'b':
      note = 12;
      break;
    case 'p':
    default:
      note = 0;
    }
    p++;

    // now, get optional '#' sharp
    if(*p == '#')
    {
      note++;
      p++;
    }

    // now, get optional '.' dotted note
    if(*p == '.')
    {
      duration += duration/2;
      p++;
    }

    // now, get scale
    if(isdigit(*p))
    {
      scale = *p - '0';
      p++;
    }
    else
    {
      scale = default_oct;
    }

    scale += OCTAVE_OFFSET;

    if(*p == ',')
      p++;       // skip comma for next note (or we may be at the end)

    // now play the note

    if(note)
    {
      tone1.play(notes[(scale - 4) * 12 + note]);
      delay(duration);
      tone1.stop();
    }
    else
    {
      delay(duration);
    }
  }
}



Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3290
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd say this has something to do with the way you read your keypad. Have you tried with longer presses on the input button and shorter ones?

Debouncing the key is another thing... I don't see any debounce code and that may well be what's happening to you.

Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Pages: [1]   Go Up
Jump to: