How to Merge 2 arduino codes

im just new here & coding
i have a qustion about in How to combine 2 arduino codes
that im using in attiny85
first code to use the attiny85 as power push button when i hold pin 2 to ground for particular period of time pin 0 will register ON and same to turn it OFF

second code to give a pulse in pin 1 HIGH-LOW-HIGH and stay in HIGH
They work as required, all separately, but I did not know how to combine them in one code

Any help will be appreciate!

first code :

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

#define BTN 2
#define timer_init() (TIMSK |= (1 << OCIE0A))
#define BTN_HOLD_MS 20000    // Press button for 1 second

enum Device_Status
enum Btn_Status

void setup()
    sei();                  // Enable interrupts
    PORTB |= (1 << BTN);    // Enable PULL_UP resistor
    GIMSK |= (1 << PCIE);   // Enable Pin Change Interrupts
    PCMSK |= (1 << BTN);    // Use PCINTn as interrupt pin (Button I/O pin)
    TCCR0A |= (1 << WGM01); // Set CTC mode on Timer 1
    TIMSK |= (1 << OCIE0A); // Enable the Timer/Counter0 Compare Match A interrupt
    TCCR0B |= (1 << CS01);  // Set prescaler to 8
    OCR0A = 125;            // Set the output compare reg so tops at 1 ms

void power_off()
    cli();                               // Disable interrupts before next commands
    wdt_disable();                       // Disable watch dog timer to save power
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode power down
    sleep_bod_disable(); // Disable brown-out detector
    sei();               // Enable interrupts

volatile unsigned int timer;  // milliseconds counter 
Btn_Status btn_status;        // Status of the button

int main()
    Device_Status status = POWER_OFF; // Set start ON or OFF when power is connected
    btn_status = BTN_UP;
    DDRB |= (1 << DDB0); // Set pin 0 as output

    for (;;)

        if (btn_status == BTN_DOWN)
            if (timer > BTN_HOLD_MS) // Check if button has been pressed enough
                if (status == RUNNING)
                    status = POWER_OFF;
                    status = RUNNING;
                    // setup of the device here if needed;
                btn_status = BTN_IGNORE; // If status already changed don't swap it again
            if (status) // Is status RUNNING?
                /* main code here */

                PORTB |= (1 << PB0); // Pin 0 ON

                /* -------------- */
                PORTB &= ~(1 << PB0); // Pin 0 OFF

    if (!((PINB >> BTN) & 0x01)) // Check if button is down
        btn_status = BTN_DOWN;
        timer = 0;
        btn_status = BTN_UP;


seconde code :

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(1, OUTPUT); //LED on Model A  or Pro

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(1, HIGH)
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(1, HIGH); 

see merge code - Google Search

I put your subject "How to merge 2 Arduino codes" into Google and got lots of useful looking results. Have you tried any of them?


Put the snippet form "second code" into a function; put the function in the main code; call the function as needed.

The fast and dirty way to merge two (or more) working sketches into one (possibly working) sketch is create this main sketch:

void setup()
  // setup3();
  // setup4();
void loop()
  // loop3();
  // loop4{};

Then, put the two (or more) sketches into 'tabs'. Just put the other sketch files into the same directory as the new 'main' sketch. When you close and re-open the 'main' sketch, the other .ino files in the directory will show up in tabs. You can also add, remove, and rename tabs with the tab menu: the little triangle at the right end of the tab bar.

Rename the setup() and loop() in each tab to setup1()/loop1() in the first tab, setup2()/loop2() in the second, etc.

If you are lucky there will be no problems and both sketches will take turns so fast they will appear to be running 'at the same time'. If you are unlucky, you will have to resolve conflicts and rewrite each sketch that hogs the CPU.

Problems to look for:

Name Conflicts: Two or more sketches defining global variables or functions of the same name. You will have to change the names until there are no conflicts.

Pin conflicts: Two or more sketches using the same pin number for different purposes. You will have to change pin assignments to avoid conflicts.

Hardware Usage Conflicts: Two or more sketches that use the same hardware feature in different ways. For example, using the Serial port at different data rates. If you can get them all to agree on a data rate you can replace the several Serial.begin(rate) calls with one at the top of setup() in the 'main' sketch.

Library Conflicts: Some libraries don't work together because they use the the same hardware for different purposes. For example the Servo library uses Timer1 and so does the IRremote library. If you are lucky you will get an error message when they both try to use the same hardware interrupt. If you are not lucky, things just won't work.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.