So I'm brand new to the world of programming and have a small project where I'm reading the value of a push button, printing the result on the Serial Monitor, and turning on an LED. Within the program I also want to debounce the signal and have an interrupt that prevents the program from continuously checking the input pin. I've tried to do some searching/Googling/Youtubing but just can't seem to find an explanation that fullfills my understanding. My code is below and what I'm asking is what should I place in the function I'm calling (tagt) to make it so that my program only checks the pin when the button is depressed, preventing my Serial monitor from ALWAYS polling that pin?
/**********************************************************/
/* READ A PUSH BUTTON, PRINT TO SM, DEBOUNCE, INTERRUPT */
/**********************************************************/
/***********************************/
/* DECLARE VARIABLES */
/***********************************/
int push_pin = 2; // Push button on digital pin 2
int led = 8; // LED on digital pin 8
int val; // value to store the state of the push button
int val2; // value to store the state of the push button to cross reference for the debounce
int button_state; // variable need for the debouncing
/********************************************/
/* BEGIN SERIAL COMS, SET PINMODES, ADD VARS*/
/********************************************/
void setup()
{
Serial.begin(9600); //setup Serial coms @ 9600 baud
while (! Serial); // Needed for Leonardo
pinMode(push_pin, INPUT); //set the push pin as an input
pinMode(led, OUTPUT); //set the LED as an output
button_state = digitalRead(push_pin); //store the push pin state in the button_state variable
attachInterrupt(1, tagt, RISING);
}
/***************************************/
/* SETUP LOOP, SM PRINTING, DEBOUNCING */
/***************************************/
void loop()
{
Serial.print("The push pin is depressed:");
Serial.println(digitalRead(push_pin)); //print the state of the push button as either 1 or 0
val = digitalRead(push_pin); //store the state of the push button in the val variable
delay(10); //add a short delay for stability
val2 = digitalRead(push_pin);
if (val == val2) /************************************/
{ /* THIS SEGMENT USED FOR DEBOUNCING */
if (val != button_state) /************************************/
{
Serial.println(button_state);
button_state = val;
}
}
if (val == HIGH)
{
digitalWrite(led, HIGH);
}
if (val == LOW)
{
digitalWrite(led, LOW);
}
button_state = val;
}
/***************************/
/* INSERT INTERRUPT FN */
/***************************/
void tagt()
{
}