How to Merge 2 arduino codes

Hi
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
{
    POWER_OFF,
    RUNNING
};
enum Btn_Status
{
    BTN_UP,
    BTN_DOWN,
    BTN_IGNORE
};

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_enable();
    sleep_bod_disable(); // Disable brown-out detector
    sei();               // Enable interrupts
    sleep_cpu();
    sleep_disable();
}

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

int main()
{
    setup();
    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;
                else
                {
                    status = RUNNING;
                    // setup of the device here if needed;
                }
                btn_status = BTN_IGNORE; // If status already changed don't swap it again
            }
        }
        else
        {
            if (status) // Is status RUNNING?
            {
                /* main code here */

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

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

ISR(PCINT0_vect)
{
    if (!((PINB >> BTN) & 0x01)) // Check if button is down
    {
        btn_status = BTN_DOWN;
        timer_init();
        timer = 0;
    }
    else
        btn_status = BTN_UP;
}

ISR(TIM0_COMPA_vect)
{
    timer++;
}

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
  delay(500);
  digitalWrite(1, HIGH); 
  exit(0);
}

see site:forum.arduino.cc 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?

Steve

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()
{
  setup1();
  setup2();
  // setup3();
  // setup4();
}
void loop()
{
  loop1();
  loop2();
  // 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.