#include <stdio.h>
#include "pico/stdlib.h"
#define led 6
#define button 14
int store[3]={ 6,3,2 }; // No of times blinking can be set here
static int count,res,three;
static volatile int var=1;
struct repeating_timer timer; //timer variable
void button_callback(uint gpio,uint32_t events){ //interrupt for push button
static int rand=0;
if((rand++) >= 100){
int hk=var;
var=!var; //toggle button state
if(hk != var){
rand=0; res=0; count=0;
if(var == 1){
three=0;
printf("Interrupt occured %d Normal pattern \n",var); //normal pattern
}
else if(var==0){
three=2;
printf("Interrupt occured %d Reverse pattern\n",var); //reverse pattern
}
}
}
}
bool repeating_timer_callback(struct repeating_timer *t) { //timer function
printf("Time in sec= %d\n",res);
count++,res++;
if(count == store[three]) { //condition to change blinking time
printf("Blink after every %d sec\n",store[three]);
gpio_put(led,1);
sleep_ms(200);
gpio_put(led,0);
add_repeating_timer_ms(900, repeating_timer_callback, NULL, &timer);
count=0;
}
if((var == 1) && (three<3) && (res==60)){ //normal pattern
three++; res=0; count=0;
if(three >= 3) {
three=0;
}
}
else if((var==0) &&(three>0) && (res==60)) { //reverse pattern
three--; res=0; count=0;
if(three <= 0){
three=3;
}
}
}
int main() {
stdio_init_all();
printf("HELLO FROM PICO\n");
gpio_init(led); gpio_init(button); //in it
gpio_set_dir(button,GPIO_IN); gpio_set_dir(led, GPIO_OUT); //set direction
gpio_pull_up(button); //pull up for button
gpio_set_irq_enabled_with_callback(button,0x04,1,button_callback); //interrupt attached
add_repeating_timer_ms(800, repeating_timer_callback, NULL, &timer); //timer attached
while (1) { /*do nothing */ }
}
//--------------------------------------------------------------------------------------
/* NOTE: when interrupt 0 occurs : led blinks every 2 sec then 3 and then 6 sec.
when interrupt 1 occurs : led blinks every 6 sec then 3 and then 2 sec.
By default interupt 1 is enabled */