Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arduino coding help : - Here, i have a code for interrupt when a button is press

ID: 3873767 • Letter: A

Question

Arduino coding help :

- Here, i have a code for interrupt when a button is pressed, led turns on and goes off

Code : ATMega329p with uno breadboard

#include   //provide set of default interrupt routines

//pointer direct to specific address

unsigned char* pEICRA = reinterpret_cast (0x69);
unsigned char* pEIMSK = reinterpret_cast (0x3D);
                  
void setup(void)
{
    pinMode(2, INPUT);  // Push Button as 2
    pinMode(13, OUTPUT); // LED as 13        
    *pEICRA = 0x03; //re
    *pEIMSK |= (1 << INT0);
    sei();   // Sets the bit and switches interrupts on  
}
                           
void loop(void) //need if function to tell true
{
Help !!!!
}

ISR(INT0_vect)
{
    digitalWrite(13, HIGH);   
    delay(3000);
    digitalWrite(13, LOW);
    cli();   // Clears the global interrupt flag
}

1 ) I need help in the loop function. ' if ' statement for true when push button is pushed, LED should stay for 3 sec.

Thank you.

Explanation / Answer

we use millis function to wait for few seconds after press the button

long offAt=0;


                           
void loop(void)

{

if( digitalRead(led)==LOW) && (digitalRead(pin)==LOW))

{

digitalWrite(led,HIGH);

offAt=milliss() + 3000;// store of +3 seconds

}

if(digitalRead(led)==HIGH) // if led is on

{

if(millis()>=offAt) // check if its time to turn off led

{

digitalWrite(led,LOW);

}

}

}

Thank you !! kindly rate my answer

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote