לימוד ארדואינו : שיעור 16 – מוסיקה ארדואינו ,שימוש ב ()while – קורס c506
השיעור חולק לשני חלקים 16.1 ו 16.2 , בשיעור עושים שימוש באתר wokwi.com לכתיבת תוכנה
while()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// www.robotronix.co.il learn c languge c506 int i; void setup() { // put your setup code here, to run once: i=0; Serial.begin(9600); // bps = bit per second } void loop() { // put your main code here, to run repeatedly: while(i<=5) { Serial.print("i : "); Serial.println(i); delay(100); i++; } } |
https://wokwi.com/projects/331297760568410707
While עם ערך לוגי TRUE or false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// www.robotronix.co.il learn c languge c506 int temp; boolean IsHot; void setup() { // put your setup code here, to run once: temp=20; Serial.begin(9600); // bps = bit per second IsHot=false; } void loop() { // put your main code here, to run repeatedly: while(IsHot == false) { Serial.print("temp : "); Serial.println(temp); delay(100); temp++; if (temp>28) { Serial.println("Temp is to hot"); IsHot=true; delay(100); } } exit(0); } |
https://wokwi.com/projects/331298355443401299
שימוש בפונקציה tone מאפשר לנו ליצור צליל בתדר משתנה ,ובכך ליצור מנגנה במיקרובקר ארדואינו .
על מנת להפסיק את הצליי נשמתש בפקודה noTone
tone()
Description
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.
Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone()
will have no effect. If the tone is playing on the same pin, the call will set its frequency.
Use of the tone()
function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).
It is not possible to generate tones lower than 31Hz. For technical details, see Brett Hagman’s notes.
Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
דוגמא 1 : הפקת צליל
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// www.robotronix.co.il // רובוטרוניקס קורס C506 // שיעור 16 - צללים טונים #define speaker 5 const int Led3 = 2; void setup() { pinMode(speaker, OUTPUT); tone(speaker, 261,1000); // DO delay(300); noTone(speaker); // Stop sound delay(100); tone(speaker, 293); // RE delay(300); noTone(speaker); // Stop sound delay(100); tone(speaker, 329,1000); // ME delay(300); noTone(speaker); // Stop sound delay(100); tone(speaker, 349,1000); // FA delay(300); noTone(speaker); // Stop sound delay(100); tone(speaker, 392,1000); // SOL delay(300); noTone(speaker); // Stop sound delay(100); tone(speaker, 440,1000); // LA delay(300); noTone(speaker); // Stop sound delay(100); tone(speaker, 493,1000); // SI delay(300); noTone(speaker); // Stop sound } void loop() { } |
דוגמא 2 : ניגון יונתן הקטן ולימוד While
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
// www.robotronix.co.il // רובוטרוניקס קורס C506 // שיעור 16 - צללים טונים #define speaker 5 const int Led3 = 2; #define EndOfMusic=-1; #define DO 261 #define RE 293 #define ME 329 #define FA 349 #define SOL 392 #define LA 440 #define SI 493 int Song1[2][100]; int StackIndex=0; void play(int frequency, int duration) { digitalWrite(3, HIGH); tone(speaker, frequency); // RE delay(duration); noTone(speaker); // Stop sound digitalWrite(3, LOW); delay(250); } void push(int frequency, int duration) { Song1[0][StackIndex] =frequency; Song1[1][StackIndex] =duration; StackIndex++; } void setup() { int i=0; pinMode(speaker, OUTPUT); pinMode(3, OUTPUT); // load melody to array Song1[0][0] =SOL; Song1[1][0]=500; Song1[0][1] =ME; Song1[1][1]=500; Song1[0][2] =ME; Song1[1][2]= 1000; Song1[0][3] =FA; Song1[1][3]=500; Song1[0][4] =RE; Song1[1][4]=500; Song1[0][5] =RE; Song1[1][5]= 1000; Song1[0][6] =DO; Song1[1][6]= 500; Song1[0][7] =RE; Song1[1][7]= 500; Song1[0][8] =ME; Song1[1][8]= 500; Song1[0][9] =FA; Song1[1][9]= 500; Song1[0][10] =SOL; Song1[1][10]= 500; Song1[0][11] =SOL; Song1[1][11]= 500; Song1[0][12] =SOL; Song1[1][12]= 500; Song1[0][13] =-1; while (Song1[0][i]!=-1) { play( Song1[0][i] ,Song1[1][i]); i++; // i=i+1 } } void loop() { } |
דוגמא 3 : שימוש ב PUSH – מה זה STACK לניגון שיר
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
// www.robotronix.co.il // רובוטרוניקס קורס C506// שיעור 16 - צללים טונים #define speaker 5 const int Led1 = 3;#define EndOfMusic=-1; #define DO 261 #define RE 293 #define ME 329 #define FA 349 #define SOL 392 #define LA 440 #define SI 493 int Song1[2][100]; int StackIndex=0; void play(int frequency, int duration) { digitalWrite(Led1, HIGH); tone(speaker, frequency); // RE delay(duration); noTone(speaker); // Stop sound digitalWrite(Led1, LOW); delay(250); } void push(int frequency, int duration) { Song1[0][StackIndex] =frequency; Song1[1][StackIndex] =duration; StackIndex++; } void setup() { int i=0; pinMode(speaker, OUTPUT); pinMode(Led1, OUTPUT); // load melody to array push(SOL,500); push(ME,500); push(ME,1000); push(FA,500); push(RE,500); push(RE,1000); push(DO,500); push(RE,500); push(ME,500); push(FA,500); push(SOL,500); push(SOL,500); push(SOL,1000); push(EndOfMusic,0); // EndOfMusic = -1 while (Song1[0][i]!=EndOfMusic) // EndOfMusic =-1 { play( Song1[0][i] ,Song1[1][i]); i++; // i=i+1 } } void loop() { } |
סקובץ הסכמה האלקטורני diagram.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
{ "version": 1, "author": "Arvind Patil", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 122.67, "left": -36.67, "attrs": {} }, { "type": "wokwi-buzzer", "id": "bz1", "top": -5.86, "left": 127.94, "attrs": { "volume": "0.1" } }, { "type": "wokwi-led", "id": "led1", "top": -3.88, "left": 241.95, "attrs": { "color": "red" } }, { "type": "wokwi-resistor", "id": "r1", "top": 70.92, "left": 246.17, "rotate": 90, "attrs": { "value": "370" } } ], "connections": [ [ "uno:5", "bz1:2", "green", [ "v0" ] ], [ "uno:GND.1", "bz1:1", "black", [ "v-39.07", "h77.58" ] ], [ "led1:A", "r1:1", "green", [ "v0" ] ], [ "r1:2", "uno:3", "green", [ "h0" ] ], [ "uno:GND.1", "led1:C", "black", [ "v-30.16", "h180.33" ] ] ] } |