{"id":86,"date":"2022-12-05T07:19:26","date_gmt":"2022-12-05T07:19:26","guid":{"rendered":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/?p=86"},"modified":"2022-12-09T04:54:34","modified_gmt":"2022-12-09T04:54:34","slug":"test","status":"publish","type":"post","link":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/2022\/12\/05\/test\/","title":{"rendered":"Arduino: Simon Says Game"},"content":{"rendered":"\n<p>Check out this Simon Says Game, created as a final project! <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\" \/>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"626\" data-id=\"125\" src=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino2.jpg\" alt=\"\" class=\"wp-image-125\" srcset=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino2.jpg 834w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino2-300x225.jpg 300w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino2-768x576.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"626\" data-id=\"127\" src=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino3.jpg\" alt=\"\" class=\"wp-image-127\" srcset=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino3.jpg 834w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino3-300x225.jpg 300w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino3-768x576.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"626\" data-id=\"126\" src=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino4.jpg\" alt=\"\" class=\"wp-image-126\" srcset=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino4.jpg 834w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino4-300x225.jpg 300w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino4-768x576.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"626\" data-id=\"124\" src=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino1.jpg\" alt=\"\" class=\"wp-image-124\" srcset=\"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino1.jpg 834w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino1-300x225.jpg 300w, https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-content\/uploads\/sites\/15085\/2022\/12\/Arduino1-768x576.jpg 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n<\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\" \/>\n\n\n\n<p class=\"has-text-align-center\">Check out the code I and my teammates wrote!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n Simon Says Game \n \n The arduino displays a set of lights that the user has to copy. Multiple sounds are used to indicate the state of the game\n A sound is played at the start, a correct move, a move indicator sound, and a game is lost.\n\n Author: Vince Osorio, Sukh Singh, David Soberano\n  \n Date Modified: 12\/04\/2022\n *\/\n\nconst int speaker = 2;\nconst int LED_PIN_R = 13;\nconst int LED_PIN_G = 12;\nconst int BUTTON_PIN_R = 9;\nconst int BUTTON_PIN_G = 8;\nconst int ledR = 1;\nconst int ledG = 2;\n\n                                        \/\/ the setup routine runs once when you press reset:\n\n#include \"tunes.h\"                      \/\/ A seperate library containing all the notes are used to create a more concise code\n\nvoid setup(){             \n  pinMode(speaker, OUTPUT);       \/\/ initialize the speaker with a pin for output.      \n  pinMode(LED_PIN_R, OUTPUT);    \/\/ initialize the digital pin as an output for led.\n  pinMode(LED_PIN_G, OUTPUT);    \/\/ initialize the digital pin as an output for led.\n  pinMode(BUTTON_PIN_R, INPUT);  \/\/ initialize the digital pin as an input for the button.\n  pinMode(BUTTON_PIN_G, INPUT);  \/\/ initialize the digital pin as an input for the button.\n  Serial.begin(9600);           \/\/intialize serial communication.\n  randomSeed(analogRead(A0) );  \/\/seeds the random number generator with different input each time.\n}\n\nvoid loop(){\n  start_tune();\n  int i = 0;\n  int j = 0;\n  int buttonStateR, buttonStateG, userInp;\n  static int round ;                              \/\/starting from zero, records the number of rounds.\n  static int Pattern_Array&#091;20];                 \/\/static array for recoding the pattern to display.\n  seedPattern(Pattern_Array, round);            \/\/function call to seed the array with a new pattern in each iteration of void loop.\n\n  while(Pattern_Array&#091;i] != -1){                \/\/while statement that turns leds on based on the pattern array until it reaches the sentinel value (-1).\n    digitalWrite(LED_PIN_R, LOW);\n    digitalWrite(LED_PIN_G, LOW);\n    if(Pattern_Array&#091;i] == ledR)                 \/\/Turns the red led using the red light function if the array has value coressponding to it.\n      Red_Light();\n    if(Pattern_Array&#091;i] == ledG)                 \/\/Turns the red led using the red light function if the array has value coressponding to it.\n      Green_Light();\n      i++;\n  }\n\n    for(j = 0; j &lt;= round; j++){\n     userInp = getUserInp();                      \/\/ uses a function to get the button input from the user\n     \n      \n     if(userInp != Pattern_Array&#091;j]){            \/\/ if the user input is not right, the if-statement ends the game while playing a sound\n       \n        delay(500);\n        end_round();\n        exit(0);\n      }\n\n      correct_guess();                           \/\/ if the user input is right, a different tone is played sound\n\n     \/*Serial.print(\"iteration:\");               \/\/Error Checking\n        Serial.println(j);\n        delay(500);*\/\n     }\n  round++;                                      \/\/increments the round variable at the end of each round\n  delay(500);                                 \/\/delay of 3 seconds between each round.\n\n}\n\n\/*\n************************\n\n*Turns the red light on*\n\n************************ *\/\n\nvoid Red_Light(){\n  digitalWrite(LED_PIN_R, HIGH);  \/\/ turn the LED on (HIGH is the voltage level)\n  delay(500);                    \/\/ wait for a second\n  digitalWrite(LED_PIN_R, LOW);   \/\/ turn the LED off by making the voltage LOW\n  delay(500);\n}\n\n\/*\n***************************\n\n*Turns the green light on*\n\n*************************** *\/\n\nvoid Green_Light(){\n  digitalWrite(LED_PIN_G, HIGH);\n  delay(500);\n  digitalWrite(LED_PIN_G, LOW);\n  delay(500);\n}\n\n\/*\n*************************************\n\n*Seed the array with random pattern.*\n\n************************************* *\/\n\nvoid seedPattern(int pattern&#091;],int round){\n  int i;\n  int randomNumber;\n  for(i = 0; i &lt;= round; i++){                    \/\/Seeds each index of array with a random ledpattern.\n    randomNumber = random(1, 3);                  \/\/ generates a random integer between 1 and 2\n    Serial.println(randomNumber);\n    pattern&#091;i] = randomNumber;                     \/\/array is intialized with this pattern\n  }\n\n  pattern&#091;round+1] = -1;                            \/\/-1 is used as an null character to specify the end of array.\n}\n\n\/*\n\n*******************************************************************\n\n*Get user input and return the ID of the button that was pressed*\n\n******************************************************************* *\/\n\n \nint getUserInp(){\n   long int gameTime1 = millis();                       \/\/time at start of the function is noted\n   long int gameTime2;\n   int buttonStateR, buttonStateG;\n   \/\/Serial.println(\"Check\");\n   Serial.println(gameTime1);\n   \n   while(buttonStateR == HIGH || buttonStateG == HIGH){            \/\/ a loop to make the program wait for the user unpress the button\n     buttonStateR = digitalRead(BUTTON_PIN_R);\n     buttonStateG = digitalRead(BUTTON_PIN_G);\n   }\n\n     buttonStateR = digitalRead(BUTTON_PIN_R);\n     buttonStateG = digitalRead(BUTTON_PIN_G);   \n\n     while(buttonStateR == LOW &amp;&amp; buttonStateG == LOW){       \/\/ loop designed to keep checking the button state until an input from the user is obtained            \n                          \n        input_indicator();\n        \n        buttonStateR = digitalRead(BUTTON_PIN_R);\n                                    \n        buttonStateG = digitalRead(BUTTON_PIN_G);\n        \n        gameTime2 = millis();                                 \/\/game at this point in the function is noted, while being updated in each iteration of the loop until a user input is received\n        \n\n        if( gameTime2 - gameTime1  &gt;= 3000){                 \/\/the if-statement triggers when the loop has been going for more then 3 seconds and no input has been received\n\n          return 0;                                          \/\/zero is returned, indicating that no input was received\n\n        }\n\n     }\n     \n  if(buttonStateR == HIGH){                                  \/\/turns the red light if the button corresponding to it is pressed\n        Red_Light();\n        return 1;                                             \/\/the id for the red button is returned if it is pressed\n  }\n\n \n\n  if(buttonStateG == HIGH){                                 \/\/turns the green light if the button corresponding to it is pressed\n        Green_Light();\n        return 2;                                               \/\/the id for the green button is returned if it is pressed\n     } \n\n       Serial.println(\"Check2\");\n}\n\n \n\nvoid start_tune(){          \/\/Start of a round Tune\n\n  tone(speaker,NOTE_A3); \n\n  delay(150);\n\n  tone(speaker,NOTE_D4);\n\n  delay(150);\n\n  tone(speaker, NOTE_A3);\n\n  delay(150);\n\n  tone(speaker, NOTE_A4);\n\n  delay(150);\n\n  noTone(speaker);\n\n    }\n\n \n\nvoid lose_tune(){         \/\/ Alternate End of a round tune\n\n  tone(speaker,NOTE_GS3);\n\n    delay(300);\n\n    tone(speaker, NOTE_FS3);\n\n    delay(300);\n\n    tone(speaker, NOTE_E3);\n\n    delay(300);\n\n    tone(speaker, NOTE_CS3);\n\n    delay(300);\n\n    noTone(speaker);\n\n    }\n\nvoid correct_guess(){           \/\/Correct Guess Tune\n\n  tone(speaker, NOTE_C4);\n\n    delay(100);\n\n    noTone(speaker);\n\n}\n\nvoid input_indicator(){           \/\/Input indicator Tune\n\n  tone(speaker,NOTE_DS3);\n\n  delay(100);\n\n  tone(speaker,NOTE_C3);\n\n  delay(100);\n\n  noTone(speaker);\n\n}\n\n \n\nvoid end_round(){             \/\/End game tune\n\n  tone(speaker, NOTE_A3);\n\n  delay(300);\n\n  tone(speaker, NOTE_E5);\n\n  delay(300);\n\n  tone(speaker, NOTE_A5);\n\n  delay(300);\n\n  noTone(speaker);\n\n                \n\n \n\n \n\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\" \/>\n","protected":false},"excerpt":{"rendered":"<p>Check out this Simon Says Game, created as a final project! Check out the code I and my teammates wrote!<\/p>\n","protected":false},"author":14910,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/users\/14910"}],"replies":[{"embeddable":true,"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":6,"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/posts\/86\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eportfolios.capilanou.ca\/vinceosorio\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}