currently creating a game where a light will show in a pattern and you have to press the corresponding button in the same pattern, the pattern grows after every round. Im currently debugging and came across an issue and was hoping someone knows a solution.
the issue is when the code gets to the 'while loop', my serial monitor just seems to spit out "while loop", meaning it is disregarding the 'for' loop.
For nested for loops, there's a whole alphabet to use - no need to re-use i, because you're messing with everyone's mind when you do that, even if you get away with it now and then.
"Which i do they think they're incrementing..."
I believe he left that one, as it happens in the setup, so it only happens once. Meaning it will automatically be initialised as 0 on set up and won’t be repeated meaning the variable doesn’t need to be changed back to a 0.
Here's a test I just did. Many times it printed 0, but then...
i is 22595
...Program finished with exit code 0
Press ENTER to exit console.
Here' the code.
# include <stdio.h>
int main()
{
for (int i; ; ) {
printf("i is %d\n", i);
break;
}
return 0;
}
and @DaveX was right to point out that whatever you do get depends on what might have come before.
I don't think anything in the startup code explicitly zeros out non-used memory, so you are indeed grabbing whatever the preceding function calls left on the stack.
Curiously I was unable to get the compiler to issue a warning. IMO this deserves one.
Hey guys, im relatively new to coding and only just started learning Arduino. I made this simple code that shows a sequence of lights, then makes the user click buttons in the same order of the lights showing.
I have finally got the code to work, but i feel like it is really chunky and kinda hard to read, do you have any suggestions how to make the code simpler and easier to read.
First, pleas ask a moderator to attach this to the thread you were working.
It helps helpers, avoids repeating stuff and will save you lotsa questions.
Plus we see more of you, and knowing what you are up to and how you got into the mess you are in now would be helpful.
So… this mess.
First, watch
then lose the goto. There is zero need for you to rely on the goto statement, and working to eliminate it will force you to think about the algorithm and will very probably result, along with de-nesting, in a better sketch.
You seem to know enough to be dangerous, an exciting time, but learning and reading other ppls' code is essential, more so at this point in your travels perhaps than at any other.
As I may have said in the other thread (see? make me go look?) creativity in coding is def a thing, but so are the very well established practices and patterns that you may not yet know enough about.
Lastly, if you aren't, make use of the IDE Autoformat tool.
@exotikdoggo I must curse and complain as you have peovoked an old interest in Simon and I have been wasting spending time playing the game.
But back to the sketch.
I will show here in pseudocode, a way for programmers to talk about code without getting into any pesky detaills, how your sketch might be structured.
start with a sequence one unit long
loop forever
SHOW:
loop over the sequence, lighting LEDs and making tones
until you've done the entire thing
PLAY:
loop
get user input and
see if it is correct
until the user finiches or makes an error
PUNISH:
if the user messed up do <whatever> and reset/start a new game
REWARD: if the user finched the current length sequence do <whatever>
and GROW: if she did the entire sequence, add to the sequence
HTH
Right now my version is busted... something I did too late or too something is making it show very odd behaviour, an error that is not quite anything I can even see how it is doing. So for playing, I am using an older version.
Later: will I go into the storage area and hope that my Simon is somewhere it should be, the batteries haven't leaked if I left them in and the thing lights up like it's 1979?
@exotikdoggo, you may well be at a point where having solutions handed yuo to "look over" or "consider" would be worth the time.
If you want to see the magic rabbit and read, play and learn, I time spent on this project would be well spent:
as it seems to match your goals for the functionality, is along the lines I have suggested and looks like you were heading (I haven't looked closely at your code and uses plain coding plainly.
@gcjr's code is dense and hard to read, even if you know what you are looking for (or at) only more so if you don't. Let's call it an acquired taste, one which I have failed to develop after seeing solutions like this for years. There may be ppl who are in the perfect place to gets stretched by it