Необходима помощь, по добавлению звука в скетч.

Тема в разделе "Arduino & Shields", создана пользователем NIKKOLAY1, 12 июл 2021.

  1. NIKKOLAY1

    NIKKOLAY1 Нерд

    Заказал себе для магнитометра два датчика и плату шилд под ардуино уно. Скачал скетч,демо версия,без звука.К сожалению,программирование для меня,дремучий лес...поэтому и обращаюсь за помощью.
     
  2. NIKKOLAY1

    NIKKOLAY1 Нерд

    Код (C++):
    /*
    * FG sensor measurement sketch
    * Work with MAG SHIELD and FG-3 sensors. Avaible at www.fgsensors.com
    * Output is displayed in pulses per ms
    *
    * Displayed number is in [no. of pulses / neasureTime]
    * "updateRate" must have bigger value than "measureTime"
    *
    */


    /* Include necessary libraries */
    #include <LiquidCrystal.h>

    /* Define LCD pins */
    const int rs = 5,
              en = 4,
              data4 = A3,
              data5 = A2,
              data6 = A1,
              data7 = A0;
    /* Initialize LCD library */
    LiquidCrystal lcd(rs, en, data4, data5, data6, data7);

    /* Define names for used pins */
    int key1 = A4;
    int key2 = A5;
    int key3 = 6;
    int sensorLo = 3;
    int sensorUp = 2;

    /* Define sensor update rate in ms */
    unsigned int updateRate = 500;

    /* Define sensor measure time in ms */
    unsigned int measureTime = 100;

    /* Define sensor frequency prescaler number */
    unsigned int freqPrescaler = 4;

    /* Define variables */
    int state = 0;                            //Stores current state
    volatile unsigned int intEnable = 0;      //0-counter for sensors is not incremented; 1-counter for sensors is incremented
    volatile unsigned long sensorLoCnt = 0;   //Stores number of counted changes for Lo sensor
    volatile unsigned long sensorUpCnt = 0;   //Stores number of counted changes for Up sensor
    int long diffOffset = 0;                  //Stores difference on first button press
    int long difference = 0;                  //Stores calculated difference (sensorLoFreq-sensorUpFreq)
    unsigned long prevMillis = 0;             //Stores previous millis() for calculating refresh rate
    unsigned int keyPress = 0;                         //Stores if key was released

    /* Setup loop */
    void setup() {
      /* Setup LCD column and row number */
      lcd.begin(16, 2);
      /* Turn cursor off and disable blinking */
      lcd.noBlink();
      lcd.noCursor();
      /* Turn on LCD display and display initialization screen */
      lcd.display();
      lcd.print("MAG SHIELD V1.0");
      lcd.setCursor(0,1);
      lcd.print(" fgsensors.com");

      /* Setup pins */
      pinMode(key1, INPUT_PULLUP);
      pinMode(key2, INPUT_PULLUP);
      pinMode(key3, INPUT_PULLUP);
      pinMode(sensorLo, INPUT);
      pinMode(sensorUp, INPUT);

      /* Setup interrupts */
      attachInterrupt(digitalPinToInterrupt(sensorLo), sensorLoHandler, CHANGE);
      attachInterrupt(digitalPinToInterrupt(sensorUp), sensorUpHandler, CHANGE);

      /* Some delay to keep the initialization LCD text readable */
      delay(3000);
    }

    /* Main loop */
    void loop() {
      /* Measure current time */
      unsigned long currMillis = millis();

      /* State machine - user can add more states */
      switch(state){
        /* Calculate new sensor data and update LCD */
        case 1:    
          /* Calculate number of actual pulses per [ms]
             Divide by 2 since we measure change in signal and by measureTime to get number of pulses per [ms]*/

          sensorLoCnt = ((sensorLoCnt * freqPrescaler)/2);
          sensorUpCnt = ((sensorUpCnt * freqPrescaler)/2);

          /* Calculate difference */
          difference = diffOffset - (sensorUpCnt - sensorLoCnt);
         
          /* Update LCD */
          lcdUpdate(sensorLoCnt, sensorUpCnt, difference);
         
          /* Reset variables */
          state = 0;
        break;

        /* When key is pressed calculate and update offset between sensors */
        case 2:
          /* Calculate offset */
          diffOffset = sensorUpCnt - sensorLoCnt;

          /* Calculate difference */
          difference = diffOffset - (sensorUpCnt - sensorLoCnt);

          /* Update LCD */
          lcdUpdate(sensorLoCnt, sensorUpCnt, difference);
          /* Reset variables */
          state = 0;
         
        /* User can add more cases (states) */
      }

      /* If defined sensor update rate has passed measure sensors */
      if(currMillis - prevMillis >= updateRate) {
        /* Reset variables */
        sensorLoCnt = 0;
        sensorUpCnt = 0;
       
        /* Save current time */
        prevMillis = currMillis;

        /* Measure number of changes in signal for defined time */
        intEnable = 1;
        delay(measureTime);
        intEnable = 0;
       
        /* Enable state 1 */
        state = 1;
      }

      /* If key is pressed, measure sensors offset */
      if((digitalRead(key1) == LOW)&(state==0)&(keyPress==0)) {
        state = 2;
        keyPress = 1;
        delay(1);
      }

      /* For making sure the difference is updated only once even if key held pressed */
      if(digitalRead(key1) == HIGH){
        keyPress = 0;
      }
    }

    /* Lo sensor interrupt handler */
    void sensorLoHandler(){
      if (intEnable==1)
        sensorLoCnt++;
    }

    /* Up sensor interrupt handler */
    void sensorUpHandler(){
      if (intEnable==1)
        sensorUpCnt++;
    }

    /* Function to update the LCD */
    /* sensor1 - sensorLo */
    /* sensor2 - sensorUp */
    void lcdUpdate(unsigned long sensor1, unsigned long sensor2, int long diff){
      /* Clear LCD */
      lcd.clear();
      /*Write new data to LCD */
      lcd.print("U:");
      lcd.print(sensor2);
      lcd.setCursor(0,1);
      lcd.print("L:");
      lcd.print(sensor1);
      lcd.setCursor(10,0);
      lcd.print("Diff:");
      lcd.setCursor(10,1);
      lcd.print(diff);
    }
     
     
  3. NIKKOLAY1

    NIKKOLAY1 Нерд

    схема
     

    Вложения:

    • schematic.pdf
      Размер файла:
      22,9 КБ
      Просмотров:
      83
  4. NIKKOLAY1

    NIKKOLAY1 Нерд

    Идея такая,сделать звук с нарастанием ,изменением частоты при приближении к цели,вывести регулировку звука на две свободные кнопки.В принципе все.Буду благодарен за помощь.С уважением.
     
  5. NIKKOLAY1

    NIKKOLAY1 Нерд

    требуется
     
  6. NIKKOLAY1

    NIKKOLAY1 Нерд

    наверное
     
  7. parovoZZ

    parovoZZ Гуру

    требуется, наверное
     
  8. NIKKOLAY1

    NIKKOLAY1 Нерд

    точно
     
  9. parovoZZ

    parovoZZ Гуру

    Кернигана и Ричи почитать