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);
}