Offline
Full Member
Karma: 0
Posts: 207
|
 |
« on: January 22, 2013, 07:39:51 am » |
This is the whole coding for the simon game. I am curious about how the code flow since each function do not return anything. And I am curious about this part: void input() { for (int x=0; x <= turn;) { input1 = digitalRead(SWITCHROOT); input2 = digitalRead(SWITCHROOT+1); input3 = digitalRead(SWITCHROOT+2); input4 = digitalRead(SWITCHROOT+3);
counter++; if (counter > RESPONSETIME) { Serial.println("TIMEOUT!"); fail(randomArray[x]); counter = 0; x++; } Does that means that we need to respond faster as the counter increases ? Thanks
|
|
|
|
« Last Edit: January 22, 2013, 07:43:31 am by Vincent19 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35576
Seattle, WA USA
|
 |
« Reply #1 on: January 22, 2013, 08:20:54 am » |
This is the whole coding for the simon game. No, it isn't.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #2 on: January 22, 2013, 08:23:54 am » |
The whole code in the text file as it exceed the word limits.
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9438
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #3 on: January 22, 2013, 03:28:47 pm » |
you can attach as zip file
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #4 on: January 22, 2013, 08:41:46 pm » |
Cant be view now ?
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #5 on: January 22, 2013, 09:01:20 pm » |
This is the whole coding for the simon game. I am curious about how the code flow since each function do not return anything.
In certain terminologies, functions which don't return anything are called "procedures". Your example code is well-written, IMO. And I am curious about this part: void input() { for (int x=0; x <= turn;) { input1 = digitalRead(SWITCHROOT); input2 = digitalRead(SWITCHROOT+1); input3 = digitalRead(SWITCHROOT+2); input4 = digitalRead(SWITCHROOT+3);
counter++; if (counter > RESPONSETIME) { Serial.println("TIMEOUT!"); fail(randomArray[x]); counter = 0; x++; } Does that means that we need to respond faster as the counter increases ? No, it means if too much time has elapsed, you lose. Big clues are: "TIMEOUT!" and "fail"  HTH, John
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #6 on: January 22, 2013, 09:05:21 pm » |
But is it true that every turn I have 3s of timeout time ? But it seems to me not on hardware ><
Well, procedures so means once it done execute the output code, then it will automatically go to input code ? Flow according to the code ?
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #7 on: January 22, 2013, 09:12:47 pm » |
Does that means that we need to respond faster as the counter increases ?
/////////////////////////////////////////////////////////////////////////// // Slowly cranks up the pressure void increaseSpeed() { if (turn == band) { beepdelay = 170; pausedelay = 80; return; } if (turn == band * 2) { beepdelay = 150; pausedelay = 60; return; }
if (turn == band*3) { beepdelay = 120; pausedelay = 40; } }
/////////////////////////////////////////////////////////////////////////// void loop() { for (int y = 0; y < WINSTATE; y++) { output(); input(); increaseSpeed(); // <---------------------------------- THIS is the part that makes it go faster as the game proceeds } It calls the procedure "increaseSpeed" defined above win(); } in the "for" loop above, until the player either wins or loses, the output code is executed, then the input code is executed, then increasespeed, and then back to output, etc. John
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #8 on: January 22, 2013, 09:16:44 pm » |
Ok, the speeed of the output goes faster. However, based on the full coding, does it means that the time I need to respond also getting faster ? Cause I seems to feel it when playing it ><
Ok, so either the player win or lose, it will follow the sequence : output > input > increase speed. And the final win(); is for what ?
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #9 on: January 22, 2013, 09:33:09 pm » |
Ok, the speeed of the output goes faster. However, based on the full coding, does it means that the time I need to respond also getting faster ? Cause I seems to feel it when playing it ><
I don't think so, because RESPONSETIME is a defined constant and doesn't get changed. Ok, so either the player win or lose, it will follow the sequence : output > input > increase speed. And the final win(); is for what ?
Up above is the definition of the win() "procedure". Looks like it beeps a certain way and flashes all the lights. The loop() procedure at the bottom of the code is called over and over by the Arduino framework code. Therefore, win or lose, the player gets to keep going (but I think the score is reset once you lose). http://arduino.cc/en/Reference/LoopThe loop() procedure "high-level" code being at the bottom is typical of c and c++ coding style, with the definitions of the procedures/functions/subroutines above. Note I am answering these questions from quick, cursory examination of the code because it is well organized, factored, and very good names for the variables. (But I could have missed something  ) Have you programmed in any other languages? If so, what?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #10 on: January 22, 2013, 10:02:28 pm » |
No..just arduino languages. So , win() will only be call once the for loop finish executing in void loop() ?
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #11 on: January 22, 2013, 10:15:41 pm » |
No..just arduino languages. So , win() will only be call once the for loop finish executing in void loop() ?
Yes, it will be called after y<WINSTATE becomes false. WINSTATE is defined as 32 so it appears that the for-loop will loop 32 times.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #12 on: January 22, 2013, 10:38:51 pm » |
I am very curious and dont understand for the part if(counter> RESPONSETIME)
How is that ? the counter will increase in every loop, so in every loop it is getting closer to exceed RESPONSETIME isnt ?
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #13 on: January 22, 2013, 10:49:58 pm » |
I am very curious and dont understand for the part if(counter> RESPONSETIME)
How is that ? the counter will increase in every loop, so in every loop it is getting closer to exceed RESPONSETIME isnt ?
Yes. It looks like the player has something slightly over 3 seconds (RESPONSETIME=3000) to push the buttons in proper sequence, or else he fail().
|
|
|
|
« Last Edit: January 22, 2013, 10:52:23 pm by johncc »
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 207
|
 |
« Reply #14 on: January 22, 2013, 10:51:11 pm » |
slightly over 3s ? not less than 3s ?
|
|
|
|
|
Logged
|
|
|
|
|
|