I was wondering if it was possible and am trying it in the Wokwi. The laser cut arduino crack the code puzzle is fun and was fun to build, but I thought about upping the ante with a ten digit crack the code puzzle. I am able to get the led count up and added all the leds necessary, kept the oled screen, but moved the SDA and SCL to their respective locations. It generates a code randomly and , through the serial readout, shows the code. The encoder push button works to initiate the puzzle, but the rotary encoder does not register. I kept the same pins as what was on the Uno (which works perfectly) but changed the interrupts called out to 4 and 5 respectively. Still no joy. Here is the uno working the code perfectly.
Here is the same code with the mega added and the led pins moved not working
Here is the 10 digit code everything working except the rotary.
Anyone want to take a stab at this and help me out?
Yes, a stab. So far I have not been able to fix or verify the encoder function, which is involved with maintaining the encoderPos - I would prefer to get the encoder reporting detents or steps, and do anything like count to 10 on that basis elsewhere. Not in the ISR.
But never mind that. You can't just make up functionality. There is a reason the encoder code uses pins 2 and 3 on an UNO, those are where interrupts 0 and 1 are available.
So this at least
//... also renamed functions PinA and PinB to not confuse me...
attachInterrupt(digitalPinToInterrupt(pinA), pinA_ISR, RISING); //Set an interrupt on PinA
attachInterrupt(digitalPinToInterrupt(pinB), pinB_ISR, RISING); //Set an interrupt on PinB
and note tha change of the ISR functions' names. I old, pinA and PinA and pina get very confused up there in what's left of my brain.
Also, I placed this at the top of your (not free running) loop, just to see if the flags were being set by moving the encoder. So far, I don't have any report that makes sense, but this is what you have to look foir, more easily outside the contxt of running your whole game logic.
Which looks like it migth be fun when you get it working. I'll take another peek at the encoder thing, but you def must fix the attach statements and avoid improvisation.
Remembering at a far right now, but change the following from 0 and 1:
attachInterrupt(0,PinA,RISING);
attachInterrupt(1,PinB,RISING);
to 4 and 5:
attachInterrupt(4,PinA,RISING);
attachInterrupt(5,PinB,RISING);
0 and 1 are for pins 20 and 21 on the mega, right?
OIC. Sry, barking at the wrong tree. I will look again (and carefully) when I am in the lab.
But I do remain of the opinion that the encoder could be fixed without the complications.
Also, I saw a bunch of encoder/interrupt examples where the author fails to protect access to volatile variables outside the interrupt service routine.
Maybe not your problem, but something to remember when programming w/ interrupts.
Also I saw printing from within the ISR, generally a Bad Idea.
Please this loop() I have been using in your wokwi as I tweeze it:
void loop()
{
noInterrupts();
int myCount = encoderCount; // our safe copy! encoderPos in the OP's code?
interrupts();
Serial.print("loop "); //... running free? NO!
Serial.print(myCount); // print as a number, not a character
Serial.println();
return; // just to see if the encoder is working
}
Modify it however, but the idea is to just see the encoder able to increment and decrement a simple int.
I am enjoying your game and assume I will have the patience to play to the end one day.
I made only, I think, these changes:
Move to MEGA 18 and 19. symmetrical who cares
static int pinA = 19; //Hardware interrupt digital pin clk
static int pinB = 18; //Hardware interrupt digital pin DT
In setup(), rust digitalPinToInterrupt():
attachInterrupt(digitalPinToInterrupt(pinA), PinA, RISING); //Set an interrupt on PinA
attachInterrupt(digitalPinToInterrupt(pinB), PinB, RISING); //Set an interrupt on PinB
Srsly, is this a ten digit numerical wordle or cows and bulls kinda puzzle? I am out!
I could never solve it. It is not too hard to dial in a combination if you know it. I would like to see the number I guessed left, along with the LED "score" until I click to start a new guess.
In this post is some rotary encoder stuff for MEGA, info collected in responding to that thread. I think we took the original code for the scheme from the same place, or from places that took it from the same place.
You might think so by looking at the INTx numbers, but INTx and attachInterrupt() numbers don't match on Mega. This number shuffling makes pins 2 and 3 compatible with Uno. There was no digitalPinToInterrupt() macro at the time.
Thanks all, as usual Alto, you always come through.
This, most likely, isn't something that will be made....at least not in this form. My mind is moving along looking at how to make it better or more fun. Admittedly, a 10 digit password that you have no idea where you were in the code is a frustrating idea in itself, I probably would not be able to finish the game either.
As I said, moving towards a path of something better.
1 - a selector switch that can give the options of the amount of digits. This selector is a simple switch with 8 possibilities. This way a player can choose the level of difficulty from a three digit puzzle to a ten digit puzzle.
The next thing would be exactly what Alto eluded to, some way of feedback without making it too easy at the lower digits. My initial reaction is to do it "movie" style where a segment display will display the numbers and "lock in" the correct digits when they are guessed and do away with the red and green LEDS altogether.
For that matter, would I be better going to a membrane keypad for number entry?...and a screen relaying the past few guesses....I don't know
Again though, this was a project I built from someone back a number of years and I was able to use my laser cutter, 3d printer and arduino to make it. Turned out great really.