Tag: MCU(Arduino)

  • 超音波センサー(HC-SR04) 割り込み コード, Ultrasonic sensor(HC-SR04) intterupt Source code

    int Trig[5] = {100, 28, 36, 44, 52};
    int Echo[5] = {100, 21, 20, 19, 18};

    unsigned long Distance[5] = {0};
    unsigned long Duration[5] = {0};

    volatile unsigned long LastPulseTime1;
    volatile unsigned long LastPulseTime2;
    volatile unsigned long LastPulseTime3;
    volatile unsigned long LastPulseTime4;

    int iTemperature = 20;
    int UltraVelocity = 330 + (0.6 * iTemperature);
    int K = UltraVelocity * 0.0005;

    void setup() {

    Serial.begin(9600); // Starts the serial communication

    for (int i = 1; i <= 4; i ++)
    {
    pinMode(Trig[i],OUTPUT);
    }

    for (int i = 1; i <= 4; i ++)
    {
    pinMode(Echo[i],INPUT);
    }

    attachInterrupt(2, EchoPin1_ISR, CHANGE);
    attachInterrupt(3, EchoPin2_ISR, CHANGE);
    attachInterrupt(4, EchoPin3_ISR, CHANGE);
    attachInterrupt(5, EchoPin4_ISR, CHANGE);
    }

    void loop() {

    for ( int i = 1; i <= 4; i ++)
    {
      delay(20);
      digitalWrite(Trig[i], HIGH);
      delayMicroseconds(10);
      digitalWrite(Trig[i], LOW);

    }

    int sensorValue0 = analogRead(A0);
    int sensorValue1 = analogRead(A1);
    int sensorValue2 = analogRead(A2);

    for (int i = 1; i <= 4; i ++)
    {
      Distance[i] = Duration[i] * 330 * 0.0005;
      Serial.print(Distance[i]);
      Serial.print(“. “);
    }

      Serial.print(sensorValue0);
      Serial.print(“. “);
      Serial.print(sensorValue1);
      Serial.print(“. “);
      Serial.print(sensorValue2);
      Serial.println(” “);
    }

    void EchoPin1_ISR() {
        static unsigned long startTimeA;

        if (digitalRead(21))
            startTimeA = micros();
        else
            Duration[1] = micros() – startTimeA;
    }

    void EchoPin2_ISR() {
        static unsigned long startTimeB;

        if (digitalRead(20))
            startTimeB = micros();
        else
            Duration[2] = micros() – startTimeB;
    }

    void EchoPin3_ISR() {
        static unsigned long startTimeC;

        if (digitalRead(19))
            startTimeC = micros();
        else
            Duration[3] = micros() – startTimeC;
    }

    void EchoPin4_ISR() {
        static unsigned long startTimeD;

        if (digitalRead(18))
            startTimeD = micros();
        else
            Duration[4] = micros() – startTimeD;
    }

  • 아두이노 인터럽트 핀 번호

    Board                                                    INT.0     INT.1     INT.2     INT.3     INT.4     INT.5

    우노, 나노 미니, 기타 328-기반             2           3

    메가, 메가2560, 메가  ADK                   2           3            18         19          20          21


    Micro, Lonardo, other 32u4-based        0, 1, 2, 3, 7


    Zero(제로)                                             all digital pins, except 4


    MKR Family boards                              0, 1, 4, 5, 6, 7, 8, 9, A1, A2


    Due(듀에)                                              all digital pins


    101                                                        all digital pins

                                                                  (Only pins 2, 5, 7, 8, 10, 11, 12, 13 work with
                                                                  CHANGE)




    Arduino 인터럽트 핀 번호, 배선이나 설정이 틀린 경우도 많으니 인터럽트를 사용 했는데


    신호를 잘 받지 못한다면 한번 더 확인 해보기