Programming for OLED Screen - Trying to Combine 2 Programs into ONE

I have code for my E-11 Blaster with OLED Screen which works really well, I also have a adapted some code as an Ammo Counter which I would like to incorporate into the E-11 Blaster Code.

I have spent 2 Day's trying to figure it out but alas I have failed (I'm not a programmer but a dabbler)

The Blaster Cost has several different weapons available selected for a button, there is also a Fire Button and Reload Button. The Ammo Counter Code uses the same Reload and Fire Button but when I try to incorporate the Ammo Code with the Weapon Code it just doesn't work like it does when run separately.

If anyone is willing to have a look I'm happy to send the code.

Many thanks


This could help if your main problem is combining 2 working sketches into one:

Hi 6v6gt,

Yep done that not getting any errors and is uploading OK, the code runs but the Ammo Counter Park just doesn’t function as it does when run separately. The Reload displays totally different writing on the Screen as it did when ran seperatly.

Is there anyway of running a separate code inside another?

Many thanks for the help

Regards Phil

If you post your code a lot of forum members will probably jump in to help you out.

This is the Code for the Ammo Counter. I want to add this code to the Blaster code so the Ammo Counter displays for each selected weapon in the Blaster Code

Ammo Code Attached

Ammo_Counter_Master.ino (5.44 KB)

This is the Blaster Code I want to add the Ammo Code into: Any Help would be appreciated as I am no Programmer so don’t understand all the Terminology, I’m a copy & Paste Guy…

Code too Long so See Attached

E-11_Blaster_Master.ino (96.6 KB)

Sorry, but this code is really a bit hard to follow and highly inefficient. And I don’t have this device built to test any of this code. But in general since you are an admitted Cut and Paste guy, see if this will help.

Paste this subroutine (built from one section of your at the end of your Blaster Master code… (hint: subroutines allow you to run code inside of other code)

void shotUpdate(int shots, int maxAmmo) {  

  display.setCursor(0, 0);
  display.print("Shots Fired");

  display.setCursor(100, 0);

  display.setCursor(0, 12);
  display.print("E-11 Blaster");

  display.setCursor(0, 24);
  display.print("Weapon Ammo");

  display.setCursor(96, 15);

  if (shots < maxAmmo) {
     display.print(maxAmmo-shots);         //display number of shots remaining
     display.drawRect(90, 12, 34, 20, 1);
  } else {
     display.print("EMPTY");               //show Empty


You are checking buttonPushCounter to see which weapon is selected. Why not rename this something like weaponSelected instead so code is easier to read? Nobody cares that it is counting pushes the important bit is that it defines which weapon is selected. The same with buttonPushCounter2, etc. These are how many shots have been fired. Why not call them that instead like shotsFiredWeapon1, shotsFiredWeapon2, etc?

Anyway, when this is zero for example, weapon 1 is selected and inside that If statement you eventually play an mp3 sound effect and then do the fire LED lighting effects, and then eventually wait for the sound to finish with a delay(250). After this delay you need to add a call to the new shotUpdate subroutine like this (shown for weapon 1)…

shotUpdate(buttonPushCounter2, 21); //<- first number is the current shot count, second number is the maximum shots for this weapon

The if (buttonPushCounter2 <= 21) code block for weapon 1 ammo available would look like this…

     if (buttonPushCounter2 <= 21) {                          // AMMO AVAILABLE 
        // play file 007.mp3 in folder advert01

        mp3_play (7);

        delay(30); // pause to wait mp3 start
        digitalWrite(ledPin, HIGH); //red
        digitalWrite(ledPin10, HIGH);
        digitalWrite(ledPin9, HIGH);
        digitalWrite(ledPin10, LOW);
        digitalWrite(ledPin9, LOW);
        delay(80);  // partial lenght of sound
        digitalWrite(ledPin, LOW);
        delay(250); // pause to wait the sound finish

        shotUpdate(buttonPushCounter2, 21); //<-- Add this line for each weapon


You will need to add this subroutine call for each weapon after the delay statement that pauses to wait for the sound effect to finish. Using the right buttonPushCounter# and maxAmmo number for each weapon.

Like I said. No way for me to ever test or debug this so all I can hope is that this gets you closer to what you want to do. You are sort of on your own from here. Hopefully you will eventually consider learning what each of the code statements is actually doing and improving the code some. Maybe start with naming variables with something more descriptive of their actual function. Then defining/storing weapon parameters in a structure or array and use some subroutines to reuse rather than repeat so much of the same code for each weapon. This looks like a really fun project and a great chance to learn more programming. Best of luck.

Thanks for the reply's, with your help I have managed to sort it out.

Many thanks