Advertisement
kilya

First project - Arduino uno

Apr 10th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pitches.h"
  2.  
  3. const int push_button_on = 13;
  4. const int push_button_off = 7;
  5. bool condition = false;
  6. int delay_time = 500;
  7. // notes in the melody:
  8. int melody[] = {
  9.   NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
  10. };
  11. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  12. int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
  13.  
  14. int led_blue = 9;
  15.  
  16. void setup() {
  17.   // Pin of Led
  18.   for(int i=2; i<=4; i++) { pinMode(i, OUTPUT); }
  19.   //pinMode(led_blue, OUTPUT);
  20.   // Pin of button push
  21.   pinMode(push_button_on, INPUT);
  22.   pinMode(push_button_off, INPUT);
  23. }
  24.  
  25. void loop() {
  26.   // -----ON-----
  27.   if(digitalRead(push_button_on) == HIGH && !condition)
  28.   {
  29.     for(int j=4; j>1; j--)
  30.     {
  31.       tone(8, NOTE_CS8, 500);  
  32.       digitalWrite(j+1, LOW);
  33.       delay(delay_time);
  34.       digitalWrite(j, HIGH);
  35.       delay(delay_time);
  36.     }
  37.    
  38.     for(int i=3; i<5; i++)
  39.     {
  40.       delay(delay_time);
  41.       digitalWrite(i, HIGH);
  42.     }
  43.  
  44.     delay(1000);
  45.     for(int i=0; i<=2; i++)
  46.     {
  47.       for(int j=4; j>1; j--)
  48.       {
  49.         digitalWrite(j, LOW);
  50.       }
  51.       delay(delay_time);
  52.       for(int j=4; j>1; j--)
  53.       {
  54.         tone(8, NOTE_CS8, 500);
  55.         digitalWrite(j, HIGH);
  56.       }
  57.       delay(delay_time);
  58.     }
  59.     condition = true;
  60.   }
  61.  
  62.   // -----OFF-----
  63.   if (digitalRead(push_button_off) == HIGH && condition)
  64.   {
  65.     for(int i=2; i<5; i++)
  66.     {
  67.       delay(delay_time);
  68.       digitalWrite(i, LOW);
  69.     }
  70.     condition = false;
  71.    
  72.     for (int thisNote = 0; thisNote < 8; thisNote++)
  73.     {
  74.       int noteDuration = 1000 / noteDurations[thisNote];
  75.       tone(8, melody[thisNote], noteDuration);
  76.       int pauseBetweenNotes = noteDuration * 1.30;
  77.       delay(pauseBetweenNotes);
  78.       // stop the tone playing:
  79.       noTone(8);
  80.     }
  81.  
  82.     delay(1000);
  83.     tone(8, NOTE_DS8, 1000);
  84.     //--ON LED 2 volt
  85.     for(int i=0; i<= 255;i++)
  86.     {
  87.       analogWrite(led_blue , i);
  88.       delay(10);
  89.     }
  90.     for(int i=255; i>= 0;i--)
  91.     {
  92.       analogWrite(led_blue , i);
  93.       delay(10);
  94.     }
  95.     delay(1000);
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement