אלקטרוניקה לארדואינו : שיעור 4 – pulse tone קורס C510
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 |
// www.robotronix.co.il // אלקטרוניקה לארדואינו : שיעור 4 - pulse tone קורס C510 #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() { } |
סרבו ארדואינו – שימוש ב servo pwm
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 |
// WWW.ROBOYTONIX.CO.IL // SERVO PWM #include <Servo.h> Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
{ "version": 1, "author": "Uri Shaked", "editor": "wokwi", "parts": [ { "id": "uno", "type": "wokwi-arduino-uno", "top": 200, "left": 20 }, { "id": "servo", "type": "wokwi-servo", "left": 400, "top": 200, "attrs": { "hornColor": "black" }, "rotate": 0 } ], "connections": [ ["uno:9", "servo:PWM", "orange", ["v-20", "*", "h0", "h-52"]], ["uno:5V", "servo:V+", "red", ["v20", "h0", "*", "h-20"]], ["uno:GND.1", "servo:GND", "black", ["v-28","h240", "*", "h-20"]] ] } |
UART כתיבה וקריאה
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 |
void setup() { pinMode(8, INPUT_PULLUP); // set push button pin as input pinMode(8, OUTPUT); // set LED pin as output digitalWrite(8, LOW); // switch off LED pin Serial.begin(9600); // initialize UART with baud rate of 9600 bps Serial.println("start"); } void loop() { if(Serial.available()) { char data_rcvd = Serial.read(); // read one byte from serial buffer and save to data_rcvd if(data_rcvd == '1') { digitalWrite(8, HIGH); // switch LED On } if(data_rcvd == '0') { digitalWrite(8, LOW); // switch LED Off } Serial.print(data_rcvd); } } |
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 |
{ "version": 1, "author": "יניב מאור", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 245.89, "left": 139.36, "attrs": {} }, { "type": "wokwi-led", "id": "led1", "top": 133.41, "left": 285.84, "attrs": { "color": "red" } }, { "type": "wokwi-resistor", "id": "r1", "top": 200.21, "left": 282.02, "rotate": 90, "attrs": { "value": "470" } } ], "connections": [ [ "led1:C", "uno:GND.1", "green", [ "v14.61", "h-39.2" ] ], [ "uno:8", "r1:2", "green", [ "v0" ] ], [ "led1:A", "r1:1", "green", [ "v0" ] ] ] } |