Go Down

Topic: attachInterrupt() is not worked at DUE (Read 202 times) previous topic - next topic

Tan_Yuanqing

The dysfunctioned attachInterrupt() is at row15. When I add row15, the whole code doesn't work.
Did I use the attachInterrupt() function wrong?

Code: [Select]
1 // Digital value of sensor
2 #define SENSORD 26
3 int KEY_NUM = 0;
4 int val_pin26 = 0;
5 //int count = 0;
6 //int state = 0;
7 //analog value of sensor
8 #define SENSORA A1
9 int val_AD = 0;
10void setup()
11{
12  pinMode(13,OUTPUT);//led pin
13  pinMode(SENSORD,INPUT);
14  pinMode(SENSORA,INPUT);
15  attachInterrupt(SENSORD, ledsensor, CHANGE);
16  Serial.begin(9600); // setup serial
17}
18
19void loop()
20{
21  Serial.println("!");
22  val_pin26 = digitalRead(SENSORD);
23  Serial.println(val_pin26);
24  scanSensor();
25  if(KEY_NUM == 1)
26  {
27    KEY_NUM = 0;
28    Serial.println("press!");
29    Serial.print("count =");
30//    Serial.println(count);
31 //   count++;
32
33   
34    //if(state == 2)
35     //state = 0;
36  }
37   //analog value of sensor
38          val_AD = 1023 - analogRead(SENSORA);//detect analog number can be used to P control.
39        Serial.print("AD = ");////////////////////////////////////
40        Serial.println(val_AD); //////////////////////////////
41        delay(1000);
42       
43}
44
45void scanSensor()
46{
47  if(digitalRead(SENSORD) == HIGH)
48  {
49    delay(10);
50    if(digitalRead(SENSORD) == HIGH)
51    {
52      while(digitalRead(SENSORD) == HIGH);
53               Serial.println("threhold of sensor");
54      KEY_NUM = 1;
55    }
56  }
57}
58void ledsensor() 
59{
60   digitalWrite(13,HIGH);
61   delay(250);
62      digitalWrite(13,LOW);
63   delay(250);
64   Serial.println("LED!");///////////////////////////////
65   //motorBrake();
66   //motorreverse1();
67}

gfvalvo

Quote
58void ledsensor()  
59{
60   digitalWrite(13,HIGH);
61   delay(250);
62      digitalWrite(13,LOW);
63   delay(250);
64   Serial.println("LED!");///////////////////////////////
65   //motorBrake();
66   //motorreverse1();
67}
Two things you should absolutely not be doing in an ISR.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Tan_Yuanqing

Thank you. But what if I want to check out the interrupt work or not without the module. Since I can't use serial. println(), I can't use the monitor to check it.

gfvalvo

Set a global (and volatile) flag variable in the ISR. Check that flag in your non-ISR loop() code. When it's set, do your print then reset it.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Romonaga

ISR should be short fast and do very little.  It is not very clear to me what you are trying to do.  You want to use an interrupt, yet the same pin you set as an interrupt pin you are constantly polling.


So let us take a step back for a moment.

Why do you feel you need to use an interrupt?  What does the interrupt provide you that checking in your loop will not provide?

Now to answer your question, yes you are using it incorrectly, If you wish to have an ISR, have the ISR set a state flag of some sort.  Have your loop look for that state flag, code accordingly.


Go Up