Skip to main content
Rollback to Revision 6
Source Link
Heslacher
  • 50.4k
  • 5
  • 81
  • 175

UPDATE

I looked at Satwik Pasani's answer and used his tips. So I have a new code which I think works better and I wanted to show it to you.

#define RedTone 165
#define GreenTone 330
#define YelloTone 659
#define BlueTone 1319
#define FailTone 100
#define Success1 440
#define Success2 600

#define BuzzerPin 8


/* The led and button pins are set this way because I use their position as a hexadecimal.
    LED PINS:
      red:0
      green:1
      yello:2
      blue:3
    Button Pins:
      red:4
      green:5
      yello:6
      blue:7
*/


byte mem[20];
byte order[4] = {0x01, 0x02, 0x04, 0x08};
int sound[9] = {0, RedTone, GreenTone, 0, YelloTone, 0, 0, 0, BlueTone};  //the tones are placed in the arrey in the hex spot for turning on the leds.


void setup() {
  DDRD = 0x0F;
  randomSeed(analogRead(0));
  pinMode(8, 1);
}


void blink(int i) { //Blinking and making sound
  PORTD = i;
  tone(BuzzerPin, sound[i]);
  delay(500);
  PORTD = 0x00;
  noTone(BuzzerPin);
  delay(500);
}

void fs(bool s) { //Sucsseded of Failed
  if (s == 0) {
    PORTD = 0x15;
    tone(BuzzerPin, FailTone);
    delay(1000);
    PORTD = 0x00;
    noTone(BuzzerPin);
    delay(500);
  }
  else {
    tone(BuzzerPin, Success1);
    delay(250);
    tone(BuzzerPin, Success2);
    delay(350);
    noTone(BuzzerPin);
    delay(500);
  }
}

void set_mem() {  //sets the random order of the leds and transforms into the led's pin in hexadecimal
  int b;
  for (int i = 0; i < 20; i++)
    mem[i] = order[random(4)];
}


bool check(int i) { //checks if the pressed button is correct
  byte look;
  for (int x = 0; x <= i; x++) {
    look = 0;
    do
    {
      look = PIND >> 4;  //makes the button press pin to the pin of led with the same color
    }
    while (look == 0);  //waits until a button is pressed
    blink(look); //lights up the button's led
    if (look != mem[x]) //checks if the wrong button was pressed
      return 0; //tells program to reset game
  }
  return 1;
}


void loop() {
  set_mem();
  for (int i = 0; i < 20; i++) {
    fs(1); //shows if the player succeded
    for (int x = 0; x <= i; x++)
      blink(mem[x]);
    if (check(i) == 0) {
      fs(0); //shows if the player failed
      break; //couses a restart of the game
    }
  }
}

New place to test out code: Here


UPDATE

I looked at Satwik Pasani's answer and used his tips. So I have a new code which I think works better and I wanted to show it to you.

#define RedTone 165
#define GreenTone 330
#define YelloTone 659
#define BlueTone 1319
#define FailTone 100
#define Success1 440
#define Success2 600

#define BuzzerPin 8


/* The led and button pins are set this way because I use their position as a hexadecimal.
    LED PINS:
      red:0
      green:1
      yello:2
      blue:3
    Button Pins:
      red:4
      green:5
      yello:6
      blue:7
*/


byte mem[20];
byte order[4] = {0x01, 0x02, 0x04, 0x08};
int sound[9] = {0, RedTone, GreenTone, 0, YelloTone, 0, 0, 0, BlueTone};  //the tones are placed in the arrey in the hex spot for turning on the leds.


void setup() {
  DDRD = 0x0F;
  randomSeed(analogRead(0));
  pinMode(8, 1);
}


void blink(int i) { //Blinking and making sound
  PORTD = i;
  tone(BuzzerPin, sound[i]);
  delay(500);
  PORTD = 0x00;
  noTone(BuzzerPin);
  delay(500);
}

void fs(bool s) { //Sucsseded of Failed
  if (s == 0) {
    PORTD = 0x15;
    tone(BuzzerPin, FailTone);
    delay(1000);
    PORTD = 0x00;
    noTone(BuzzerPin);
    delay(500);
  }
  else {
    tone(BuzzerPin, Success1);
    delay(250);
    tone(BuzzerPin, Success2);
    delay(350);
    noTone(BuzzerPin);
    delay(500);
  }
}

void set_mem() {  //sets the random order of the leds and transforms into the led's pin in hexadecimal
  int b;
  for (int i = 0; i < 20; i++)
    mem[i] = order[random(4)];
}


bool check(int i) { //checks if the pressed button is correct
  byte look;
  for (int x = 0; x <= i; x++) {
    look = 0;
    do
    {
      look = PIND >> 4;  //makes the button press pin to the pin of led with the same color
    }
    while (look == 0);  //waits until a button is pressed
    blink(look); //lights up the button's led
    if (look != mem[x]) //checks if the wrong button was pressed
      return 0; //tells program to reset game
  }
  return 1;
}


void loop() {
  set_mem();
  for (int i = 0; i < 20; i++) {
    fs(1); //shows if the player succeded
    for (int x = 0; x <= i; x++)
      blink(mem[x]);
    if (check(i) == 0) {
      fs(0); //shows if the player failed
      break; //couses a restart of the game
    }
  }
}

New place to test out code: Here

Added updated code
Source Link
Sela12
  • 161
  • 1
  • 12

UPDATE

I looked at Satwik Pasani's answer and used his tips. So I have a new code which I think works better and I wanted to show it to you.

#define RedTone 165
#define GreenTone 330
#define YelloTone 659
#define BlueTone 1319
#define FailTone 100
#define Success1 440
#define Success2 600

#define BuzzerPin 8


/* The led and button pins are set this way because I use their position as a hexadecimal.
    LED PINS:
      red:0
      green:1
      yello:2
      blue:3
    Button Pins:
      red:4
      green:5
      yello:6
      blue:7
*/


byte mem[20];
byte order[4] = {0x01, 0x02, 0x04, 0x08};
int sound[9] = {0, RedTone, GreenTone, 0, YelloTone, 0, 0, 0, BlueTone};  //the tones are placed in the arrey in the hex spot for turning on the leds.


void setup() {
  DDRD = 0x0F;
  randomSeed(analogRead(0));
  pinMode(8, 1);
}


void blink(int i) { //Blinking and making sound
  PORTD = i;
  tone(BuzzerPin, sound[i]);
  delay(500);
  PORTD = 0x00;
  noTone(BuzzerPin);
  delay(500);
}

void fs(bool s) { //Sucsseded of Failed
  if (s == 0) {
    PORTD = 0x15;
    tone(BuzzerPin, FailTone);
    delay(1000);
    PORTD = 0x00;
    noTone(BuzzerPin);
    delay(500);
  }
  else {
    tone(BuzzerPin, Success1);
    delay(250);
    tone(BuzzerPin, Success2);
    delay(350);
    noTone(BuzzerPin);
    delay(500);
  }
}

void set_mem() {  //sets the random order of the leds and transforms into the led's pin in hexadecimal
  int b;
  for (int i = 0; i < 20; i++)
    mem[i] = order[random(4)];
}


bool check(int i) { //checks if the pressed button is correct
  byte look;
  for (int x = 0; x <= i; x++) {
    look = 0;
    do
    {
      look = PIND >> 4;  //makes the button press pin to the pin of led with the same color
    }
    while (look == 0);  //waits until a button is pressed
    blink(look); //lights up the button's led
    if (look != mem[x]) //checks if the wrong button was pressed
      return 0; //tells program to reset game
  }
  return 1;
}


void loop() {
  set_mem();
  for (int i = 0; i < 20; i++) {
    fs(1); //shows if the player succeded
    for (int x = 0; x <= i; x++)
      blink(mem[x]);
    if (check(i) == 0) {
      fs(0); //shows if the player failed
      break; //couses a restart of the game
    }
  }
}

New place to test out code: Here


UPDATE

I looked at Satwik Pasani's answer and used his tips. So I have a new code which I think works better and I wanted to show it to you.

#define RedTone 165
#define GreenTone 330
#define YelloTone 659
#define BlueTone 1319
#define FailTone 100
#define Success1 440
#define Success2 600

#define BuzzerPin 8


/* The led and button pins are set this way because I use their position as a hexadecimal.
    LED PINS:
      red:0
      green:1
      yello:2
      blue:3
    Button Pins:
      red:4
      green:5
      yello:6
      blue:7
*/


byte mem[20];
byte order[4] = {0x01, 0x02, 0x04, 0x08};
int sound[9] = {0, RedTone, GreenTone, 0, YelloTone, 0, 0, 0, BlueTone};  //the tones are placed in the arrey in the hex spot for turning on the leds.


void setup() {
  DDRD = 0x0F;
  randomSeed(analogRead(0));
  pinMode(8, 1);
}


void blink(int i) { //Blinking and making sound
  PORTD = i;
  tone(BuzzerPin, sound[i]);
  delay(500);
  PORTD = 0x00;
  noTone(BuzzerPin);
  delay(500);
}

void fs(bool s) { //Sucsseded of Failed
  if (s == 0) {
    PORTD = 0x15;
    tone(BuzzerPin, FailTone);
    delay(1000);
    PORTD = 0x00;
    noTone(BuzzerPin);
    delay(500);
  }
  else {
    tone(BuzzerPin, Success1);
    delay(250);
    tone(BuzzerPin, Success2);
    delay(350);
    noTone(BuzzerPin);
    delay(500);
  }
}

void set_mem() {  //sets the random order of the leds and transforms into the led's pin in hexadecimal
  int b;
  for (int i = 0; i < 20; i++)
    mem[i] = order[random(4)];
}


bool check(int i) { //checks if the pressed button is correct
  byte look;
  for (int x = 0; x <= i; x++) {
    look = 0;
    do
    {
      look = PIND >> 4;  //makes the button press pin to the pin of led with the same color
    }
    while (look == 0);  //waits until a button is pressed
    blink(look); //lights up the button's led
    if (look != mem[x]) //checks if the wrong button was pressed
      return 0; //tells program to reset game
  }
  return 1;
}


void loop() {
  set_mem();
  for (int i = 0; i < 20; i++) {
    fs(1); //shows if the player succeded
    for (int x = 0; x <= i; x++)
      blink(mem[x]);
    if (check(i) == 0) {
      fs(0); //shows if the player failed
      break; //couses a restart of the game
    }
  }
}

New place to test out code: Here

deleted 19 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I buitbuilt a Simon Says game using an Arduino UNO board. This This was my first "real" arduino project. I I would like to know how to make it better, game mecanicsmechanics and code efficencyefficiency.

Code:

Edit: I'm having problems making the snippet work, so here is the link to the site itself and here is a video of it working (sorry for the low quality).

(I'm having problems making the snippet work, so here is the link to the site itself and here is a video of it working (sorry for the low quality).)

I buit a Simon Says game using an Arduino UNO board. This was my first "real" arduino project. I would like to know how to make it better, game mecanics and code efficency.

Code:

Edit: I'm having problems making the snippet work, so here is the link to the site itself and here is a video of it working (sorry for the low quality).

I built a Simon Says game using an Arduino UNO board. This was my first "real" arduino project. I would like to know how to make it better, game mechanics and code efficiency.

(I'm having problems making the snippet work, so here is the link to the site itself and here is a video of it working (sorry for the low quality).)

Tweeted twitter.com/StackCodeReview/status/863124544307040256
Added ways to see it working
Source Link
Sela12
  • 161
  • 1
  • 12
Loading
deleted 6 characters in body
Source Link
Sela12
  • 161
  • 1
  • 12
Loading
added 8 characters in body
Source Link
Sela12
  • 161
  • 1
  • 12
Loading
deleted 12 characters in body
Source Link
Sela12
  • 161
  • 1
  • 12
Loading
Source Link
Sela12
  • 161
  • 1
  • 12
Loading