Recycling the Sketch

Hi Guys

Remember I'm a novice .. ..

I've got a sketch up and running for a game I've built for my Grandkids Christmas .. .. ..

I'm using a Mega 2560 and after some angst and a few more grey hairs I've got most things as I want them but I've one action I cannot fathom ..

Is there a command I can put into a sketch to invoke a reset, that is, as though I'd pressed the reset button on the Arduino .. .. .. i've looked everywhere I can think of and found nothing .. .. .

Thanks

S

If you have a spare I/O pin you can use that to reset the processor:

const int ForceResetPin = 2;  // I/O pin connected to Reset pin

void ForceReset() {
    digitalWrite(ForceResetPin, LOW);
    pinMode(ForceResetPin, OUTPUT);
}

Brilliant John

Thank you very much ...

S

Why not write a function that resets any relevant variables to initial values and call it when you want to restart the game ? Resetting the Arduino seems to be a very clumsy way to achieve what you want.

Hi Bob

The simple answer to that one is I've been coding about 3 weeks now and I wouldn't know where to start with that .. sounds complicated. Invoking a reset, gets me the desired effect and a methodology I can understand.

I still have a lot of other problems to solve with this yet and at the moment, it's still a prototype on a breadboard .. at some stage I've got to make a pretty case for it with hot moulded acrylic and then build it all into the case.

I've appended my sketch so that you can see what I'm doing.. .. the commented out section at the top is because at the moment, the wave files play when the button is pressed and I'm trying to get them to play coincident with each new LED .. I thought that would do it but it keeps playing the last wave file over and over .. I think because PIN 13 is also the I/O pin for the onboard LED and it's conflicting but I'm not sure.

I'm learning fast, but not that fast !! :slight_smile:

Thanks for your time .. ..

S

Edit: it won't allow me to post the code 'exceeds maximum allowed length' . wife used to say that !

Edit: it won't allow me to post the code 'exceeds maximum allowed length

Had you actually read the stickies at the top of the forum, crude jokes would not have been necessary.

Ok, Well.. I do most humbly apologise for offending you ..

Goodnight

S

Musicmanager:
Ok, Well.. I do most humbly apologise for offending you ..

Goodnight

S

If the code is too long to put in a post then attach the .ino file instead

@johnwasser,
I thought it had been discussed that using an IO pin, which reverts to an input when reset is applied, resulted in reset not being applied long enough for reset to be effective?

I've never tried it myself. A low for 2.5uS is needed per the datasheet section 29.5, Table 29-11.
Document Atmel-8271J-AVR- ATmega-Datasheet_11/2015

Here's another attempt at showing you my code .. ..

Also, just called for food, back in a while .. .. .

S

Button_Hero_6a.ino (9.39 KB)

Do these have external pullup or pulldown resistors so the pins are at a known state when the buttons are not pressed?

 // mode the pins

 pinMode(2, INPUT);

 pinMode(3, INPUT);

 pinMode(4, INPUT);

 pinMode(5, INPUT);

 pinMode(6, INPUT);

 pinMode(9, OUTPUT);

I would guess pulldowns with pins connected to +5 when pressed from this:

 if (digitalRead(2) == HIGH ||

 digitalRead(3) == HIGH ||

 digitalRead(4) == HIGH ||

 digitalRead(5) == HIGH ||

 digitalRead(6) == HIGH) {

 started = true; //start the game if any button is pressed

 }

I would suggest using pinMode (pinX, INPUT_PULLP);
wire the button to connect to Gnd, and then look for a LOW vs a HIGH.
Alleviate any chance to accidentally connect +5 to GND and short out the power supply.

Hi Crossroads

How's the drumming ?

Thanks for the advice, I see what you mean. There are external pulldown resistors on the buttons and also an MC14490P debounce chip .. .. .. .

To achieve that however, I'd have to dismantle and rebuild the hardware and then re-write the code and given that it took me 2 weeks to get the thing working, albeit with some 'creases' to iron out, I think that's a bridge too far for me now.

I will however, keep a note for a future investigation.

Thanks

S