i have a small problem on interrupt fuction, all i want is to print on monitor when the pin goes from low to high and from high to low,
so i was thinking to use twice this fuction, here is my simple code
void setup()
{
attachInterrupt(0, rising_CH1, RISING);
attachInterrupt(0, falling_CH1, FALLING);
Serial.begin(115200);
}
void loop()
{
}
void rising_CH1(){
Serial.println(" state on PIN_2 changed from 0 TO 1");
}
void falling_CH1(){
Serial.println(" state on PIN_2 changed from 1 TO 0");
}
the resulut is that woriking only the last void falling_CH1
Interrupts are disabled during this time, so serial.print won't work
Try using a variable and giving it a number on the interrupts. Then back in the normal program, if any of these numbers are found, print relative message.
if i understoond that, it doesnt work
here is the new code
int state;
void setup()
{
attachInterrupt(0, rising_CH1, RISING);
attachInterrupt(0, falling_CH1, FALLING);
Serial.begin(115200);
}
void loop()
{
if (state==1){
Serial.println(" state on PIN_2 changed from 0 TO 1");
state=3;
}
if (state==0){
Serial.println(" state on PIN_2 changed from 1 TO 0");
state=3;
}
}
void rising_CH1(){
state=1;
}
void falling_CH1(){
state=0;
}
int counter;
int input_1=2;
volatile int state = LOW;
int val;
void setup()
{
pinMode(input_1, INPUT);
attachInterrupt(0, rising_CH1, CHANGE);
Serial.begin(115200);
}
void loop()
{
if (state==1){ //if there is change on pin
counter=counter+1;
Serial.print("counter nr: ");
Serial.print(counter);
Serial.print(" state on PIN_2 changed to ");
Serial.println(val);
state=2; //reset the change
}
}
void rising_CH1(){
val=digitalRead(input_1);
state=1; //there is change on pin
}
when i am changing the wire of that pin quickly on vdd (i have pulldown resistor on gnd) i am geting these results:
counter nr: 73 state on PIN_2 changed to 0
counter nr: 74 state on PIN_2 changed to 1
counter nr: 75 state on PIN_2 changed to 1
counter nr: 76 state on PIN_2 changed to 0 counter nr: 77 state on PIN_2 changed to 1 counter nr: 78 state on PIN_2 changed to 1
counter nr: 79 state on PIN_2 changed to 1
counter nr: 80 state on PIN_2 changed to 0
counter nr: 81 state on PIN_2 changed to 1
off course a pin cant chaging from 1 to 1 state ot from 0 to 0,
Now you can have logic to do what you need to do with the transition. You can take your time in terms of generating serial output, and the interrupt handler will still count the transitions.
Your loop code can look at the rise and fall counts to decide what is going on, of if a transition might have been missed.
The if(val == 1) construct evaluates the statement inside the parentheses. If the statement is true the if block is executed. In the if(val == 1) case, the equality operator is applied to the 2 operands to determine true or false. In the if(val) case, there is no operator to apply, so val itself must represent true (non-zero) or false (zero).
Unless the single operand in the if statement is a boolean variable (that is explicitly set to values of true or false), I don't like the if(val) construct. Even after 25 years of coding in C/C++, I still need to look up whether true or false is 0.
That was psuedo code to give you an idea of the structure of the program. The idea was that you would replace the "something" with something meaningful, like the number of rising edges or falling edges.