Favqulodda vosita - Ikkita LEDni tugmachalar bilan yoping va buzuq ovozni o'zgartiring

Otam va men yangi BMTni qanday ishlatishni o'rganyapman.

Biz tugmani tegib turadigan chiroqlarni qanday qilib o'chirishni o'rgandik. Bundan tashqari, bir tugmani tegib, passiv buzzer ovozi qilishni o'rganib chiqdik va miloddan avvalgi kabi polga o'xshash ovozni o'zgartiramiz.

Biz faqat bilib olganimizdek, biz bir nechta loopni ishlay olmaymiz, shuning uchun biz buzzer kodini va LED kodini ikki marta (ko'k va qizil LEDlar uchun) birlashtirganmiz va u ishlaydi ... lekin LEDlardagi kechikish buzish ovozini buzmoqda. Biz qila oladigan biror narsa bormi?

Kod quyida.

Rahmat!

Kod:

int buzzerPin = 9;   //the number of the buzzer pin
int BlueLEDpin = 5;  //the number of the blue LED pin
int RedLEDpin = 7;   //the number of the Red LED pin  
float sinVal;        //Define a variable to save sine value for buzzer
int toneVal;         //Define a variable to save sound frequency for buzzer

void setup() {
  pinMode(buzzerPin, OUTPUT);//Set Buzzer pin to output mode
  pinMode(BlueLEDpin, OUTPUT);//Set Blue LED pin to output mode
  pinMode(RedLEDpin, OUTPUT);//Set Red LED pin to output mode
}

void loop() { 
    Buzzer(); 
    BlueLED();
    RedLED();

}

void Buzzer() {
  for (int x = 0; x < 360; x++) {      //X from 0 degree->360 degree
    sinVal = sin(x * (PI/180));      //Calculate the sine of x
    toneVal = 2000 + sinVal * 500;     //Calculate sound frequency according to the sine of x
    tone(buzzerPin, toneVal);          //Output sound frequency to buzzerPin
    delay(1);
  }

 }

  void BlueLED() {
  digitalWrite(5, HIGH);  //turn the LED on (HIGH is the voltage level)
  delay(1);             //wait for a second
  digitalWrite(5, LOW);   //turn the LED off by making the voltage LOW
  delay(1);             //wait for a second
}

void RedLED() {
  digitalWrite(7, HIGH);  //turn the LED on (HIGH is the voltage level)
  delay(1);             //wait for a second
  digitalWrite(7, LOW);   //turn the LED off by making the voltage LOW
  delay(1);             //wait for a second
}
3
Vau, rahmat. Hozir biz juda g'ururlanyapmiz.
qo'shib qo'ydi muallif Steve S, manba
Yaxshi formatlangan kod uchun +1 ... agar sizga imkon bo'lsa, izohlar va kodlarni alohida saqlash uchun yana bir narsa beraman ...
qo'shib qo'ydi muallif jsotola, manba

5 javoblar

Muammoingiz BlueLED() va RedLED() chaqiruvlaridagi kechikishlar (). "Ko'proq" bir vaqtning o'zida faoliyatni amalga oshirish uchun ushbu kechikishlarni amalga oshirish uchun taymer to'xtash usullaridan foydalanish kerak bo'ladi. Bu kontseptual jihatdan ancha murakkab, ammo buni amalga oshirishning to'g'ri yo'li.

2
qo'shib qo'ydi
Tez ish bo'ldi! Buni xursand qildingiz.
qo'shib qo'ydi muallif George Bailey, manba
Harperga juda minnatdorman; Bu juda foydali bo'ldi. Biz sizning maslahatingizga amal qildik va uni hal qildik! Rahmat!
qo'shib qo'ydi muallif Steve S, manba

Biz buni qildik. Harper Shelby yordami uchun rahmat.

Blink Without Delay modelini bizning original kodimiz bilan birlashtirdik:

int buzzerPin = 9;   //the number of the buzzer pin
const int BlueLEDpin = 5;
const int RedLEDpin = 7;    
float sinVal;        //Define a variable to save sine value
int toneVal;         //Define a variable to save sound frequency
int BlueLEDState = LOW; 
int RedLEDState = HIGH; 
unsigned long previousMillisBlue= 0;
unsigned long previousMillisRed= 0;
const long interval = 200; 

void setup() {
  pinMode(buzzerPin, OUTPUT);//Set Buzzer pin to output mode
  pinMode(BlueLEDpin, OUTPUT);
  pinMode(RedLEDpin, OUTPUT);
}

void loop() {
    Buzzer(); 
    BlueLED();
    RedLED();

}

void Buzzer() {
  for (int x = 0; x < 360; x++) {      //X from 0 degree->360 degree
    sinVal = sin(x * (PI/180));      //Calculate the sine of x
    toneVal = 2000 + sinVal * 500;     //Calculate sound frequency according to the sine of x
    tone(buzzerPin, toneVal);          //Output sound frequency to buzzerPin
    delay(1);
  }

 }

  void BlueLED() {
  unsigned long currentMillisBlue = millis();

  if (currentMillisBlue - previousMillisBlue >= interval) {
    previousMillisBlue = currentMillisBlue;
    if (BlueLEDState == LOW) {
      BlueLEDState = HIGH;
    } else {
      BlueLEDState = LOW;
    }

    digitalWrite(BlueLEDpin, BlueLEDState);
  }

}

void RedLED() {
  unsigned long currentMillisRed = millis();

  if (currentMillisRed - previousMillisRed >= interval) {
    previousMillisRed = currentMillisRed;
    if (RedLEDState == LOW) {
      RedLEDState = HIGH;
    } else {
      RedLEDState = LOW;
    }

    digitalWrite(RedLEDpin, RedLEDState);
  }

}
2
qo'shib qo'ydi

Birinchidan, tabriklar! siz birinchi qadamni mikrokontrolörlarning juda baxtli dunyosiga aylantirdingiz. Men mahalliy universitetda robotlarni o'rgandim, shuning uchun chiroqni qachon boshdan kechirganini va odamlarning tushunishni boshlashini har doim yaxshi ko'raman.

Yuqorida aytib o'tilganidek, kechikish kodisiz blink bu yaxshi boshlanishdir. Keyinchalik siz yana bir yorug'lik yoki 10 ni qo'shishni istasangiz, yaxshi kod kengaytirilgan bo'lishi kerak va bu har bir satrni kodlash juda katta bo'lishi mumkin. Ushbu turdagi vazifalarni bajarish uchun ko'plab boshqa usullar mavjud, ammo siz ushbu darsliklarni ko'rib chiqishingiz mumkin. Arduino bilan ko'p marta ishlash.

Ushbu misolda ishlatiladigan davlat mashinasi yondashuviga foyda, keyinchalik kodni o'zgartirish uchun juda oson bo'ladi; agar ketma-ket yonib-o'chadigan chiroqlar (masalan, ritsar-chavandoz chiroqlari) yoki undan ko'p sirenlar bo'lishni istasangiz, uni kiritish osonroq bo'ladi. Ikkinchi kodingizda redLED() va void blueLED() kodlari asosan bir xil qatorlarni o'z ichiga oladi; siz "LED" deb nomlangan ob'ektni yaratishingiz mumkin va keyinchalik bularning barchasi turli xil tezlik va turli xil pinlarda ishlaydigan (masalan, veb-saytdagi misolni keltirishi kerak) kodi). Misol uchun, bir kalit sifatida 4 xil sirenni dasturlashingiz mumkin: holatda va o'tgan vaqtdan keyin ular o'rtasida almashin.

Yana bir zamonaviy usul - real vaqtda operatsion tizimni kiritish. RTOS bir nechta mustaqil vazifalarni bajarishga imkon beradi. Har bir yorug'lik o'z vazifasiga va uchinchi tomonda siyra bo'lishi mumkin va tizim har bir ishni bir vaqtning o'zida davom ettiradi. Bunday yondashuv, agar biror yorug'lik vaqtini o'zgartirishni xohlasangiz, afzalliklarga ega bo'lasiz; kodni o'zgartirishga muhtoj bo'lishidan qo'rqmasdan bir joyda o'zgaradi. Arduino'da FreeRTOS haqida ma'lumotni topish mumkin bu erda . Blink va analog o'qish misollarini o'z ichiga olgan sahifaning pastki qismida misol mavjud.

Bu juda ilg'or mavzulardir, lekin sizning va otangiz u erga boradi deb o'ylayman. Yana bir bor tabriklayman.

1
qo'shib qo'ydi
Nathanga katta rahmat! Biz bu haqda keyingi sahifani ko'rib chiqamiz va bergan veb-sayt havolasi ajoyib ko'rinadi.
qo'shib qo'ydi muallif Steve S, manba

Bu ehtimol biroz rivojlangan, ammo ...

Loop funktsiyasini pastadirga tushirish uchun boshlang'ich sinf va markerni foydalanasiz. Siz bu darajada ko'p yutuqlarga erisha olmaysiz, lekin yonib-o'chadigan chiroqlar va ko'zgu yoritgichlari kabi boshqa narsalar qo'shilayotgandek, hatto orqa tomonga qaragan xabarlar paneli ham sizning hayotingizni osonlashtiradi.

/*
Jacob & Dad’s Police Car light and sounds
*/

class BaseOutputObject
{
protected:
    int Pin;
public:
    BaseOutputObject(const int& pin)
        : Pin(pin)
    {
        pinMode(Pin, OUTPUT);
    }
    virtual void Update()
    {
    }
};

class FlashingLED : public BaseOutputObject
{
    long OnTime;
    long OffTime;
    int ledState;
    unsigned long previousMillis;
public:
    FlashingLED(const int& pin, const long& on, const long& off, const bool& startLow = true)
        : BaseOutputObject(pin)                    //Call base class constructor                      
        , OnTime(on)                           //Use initialisers rather than assignments 
        , OffTime(off)
        , ledState(startLow ? LOW : HIGH)
        , previousMillis(0)
    {
    }

    void Update()
    {
       //check to see if it's time to change the state of the LED
        const unsigned long currentMillis = millis();          //Make it const because it won't change within this call of the function.

        if (currentMillis - previousMillis >= OnTime)
        {
            ledState = (ledState == LOW ? HIGH : LOW); //Toggle the state.
            previousMillis = currentMillis; //Remember the time
            digitalWrite(Pin, ledState); //Update the actual LED
        }
    }
};

class Buzzer : public BaseOutputObject
{
    float SinValue;
    int ToneValue;
public:
    Buzzer(const int& pin)
        : BaseOutputObject(pin)
        , SinValue(0.0f)       //Always initialise variables, in case you change the code later.
        , ToneValue(0)
    {
    }

    void Update()
    {
        for (int x = 0; x < 360; x++)
        {
            SinValue = sin(x * (PI/180));
            ToneValue = 2000 + SinValue * 500;
            tone(Pin, ToneValue);
            delay(1);
        }
    }

};

// The objects could be declared dynamically, but thats not a great idea on embedded systems.
FlashingLED ledOne(5, 100, 400);
FlashingLED ledTwo(7, 100, 400);
Buzzer buzzer(9);
enum { LED_ONE, LED_TWO, BUZZER, MaxObjects };
// Have to have pointers to the objects to allow you to cast them down to the base objects.
BaseOutputObject* objects[MaxObjects] = {  &ledOne, &ledTwo, &buzzer};

void setup()
{
}

void loop()
{
    for (int index = 0; index < MaxObjects; ++index)
    {
        objects[index]->Update();
    }
}
0
qo'shib qo'ydi

Neytanning ko'rsatgichi bilan uni yaxshiroq qildik.

Sinf qilishni o'rgandik, ammo LEDlar bir-biriga bostirib kirdi, shuning uchun biz boshqa sinfni qanday qilishni o'rganib oldik (sinfdagi "Ommaviy va bo'sh kelmaydigan yangilanishlar" - bizni birinchi navbatda qo'lga kiritdi). Sinflar davlatning yuqori darajasidan boshlanadi, ikkinchisi esa davlatning quyi qismiga ega. Barcha uch tugmachani bosganimizda ikkita ohangni va muqobil yorug'likni olamiz.

Ushbu usuli yordamida biz ko'plab yangi LEDlarni qo'sha olamiz.

Biz yozgan kodimiz quyidagicha:

/*
  Jacob & Dad’s Police Car light and sounds
*/

int buzzerPin = 9;    
float sinVal;         
int toneVal;         

class FlashingLED_A
{
    int ledPin;     
    long OnTime;     
    long OffTime;    
    int ledState;                   
    unsigned long previousMillis;   
  public:
  FlashingLED_A(int pin, long on, long off)
  {
    ledPin = pin;
    pinMode(ledPin, OUTPUT);     
    OnTime = on;
    OffTime = off;
    ledState = LOW; 
    previousMillis = 0;
  }

 void Update()
  {
   //check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();

    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
        ledState = LOW; //Turn it off
      previousMillis = currentMillis; //Remember the time
      digitalWrite(ledPin, ledState); //Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH; //turn it on
      previousMillis = currentMillis;   
      digitalWrite(ledPin, ledState);     
    }
  }

};

class FlashingLED_B
{
    int ledPin;  
    long OnTime;    
    long OffTime;   
    int ledState;                   
    unsigned long previousMillis;   
  public:
  FlashingLED_B(int pin, long on, long off)
  {
    ledPin = pin;
    pinMode(ledPin, OUTPUT);     
    OnTime = on;
    OffTime = off;
    ledState = HIGH; 
    previousMillis = 0;
  }

 void Update()
  {
   //check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();

    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
        ledState = LOW; //Turn it off
      previousMillis = currentMillis; //Remember the time
      digitalWrite(ledPin, ledState); //Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH; //turn it on
      previousMillis = currentMillis;   
      digitalWrite(ledPin, ledState);    
    }
  }

};

FlashingLED_A led1(5, 100, 400);
FlashingLED_B led2(7, 100, 400);

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  Buzzer(); 
    led1.Update();
    led2.Update();
}

void Buzzer() {
  for (int x = 0; x < 360; x++) {      
    sinVal = sin(x * (PI/180));       
    toneVal = 2000 + sinVal * 500;    
    tone(buzzerPin, toneVal);          
    delay(1);
  }
 }
0
qo'shib qo'ydi
FlashingLED_A va FlashingLED_B o'rtasidagi yagona farq A ning quyi boshlanganligi va B yuksaklik boshlanishidirmi? Agar shunday bo'lsa, sinf ishlab chiqaruvchisiga qo'shimcha parametr qo'shib, sinflardan birini ishlatishim kerak.
qo'shib qo'ydi muallif Thomas Myron, manba