רובוטיקס בלוקס – קורס RB10 – שיעור 3
מטרת השיעור :
1.לימוד שפת C – המשך
2. הוספת אמצעי קלט למיקרו בקר – כפתור .
3.קריאת ערך קלט דיגיטאלי (1 או 0) לתוך משתנה
4.בדיקת ערך משתנה באמצעות משפט IF
שלב ראשון ניגש לאתר הסימולציה https://wokwi.com/
ושם נבחר את המיקרובקר עליו נעבוד : מיקרובקר ארדואינו נאנו
דוגמא 1 : משפט אמת , משפט שקר , שימוש ב IF
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 |
// robotics blocks - Learn button int temp; void setup() { Serial.begin(9600); temp=31; } void loop() { // read the state of the pushbutton value: if ( temp>24 ) { Serial.println("hot"); } if ( temp==24 ) { Serial.println("Just the way i like it 24 "); } if ( temp<24) { Serial.println(" cold "); } delay(1000); // wait 1 sec (1 sec = 1000 ms) } |
ערכים אנלוגים וערכים דיגיטלים (קלט)
ערך אנלוגי
ערך דיגיטלי
דוגמא לקלט אנלוגי – פוטנציומר – מייצר ערכים מ 0 עד 1023.
ערכים אנלוגים למשל יכול להיות כל מספר למשל 1023 ,23, 32- , 0, 55,623, 1, 90 , 1-
ערכים דיגיטלים יכולים להיות רק 0 או 1 בלבד .
באלקטורניקה יש רכיבים אנלוגים ויש רכיבים דיגיטלים אותם נילמד לחבר למיקרובקר , לבדוק את ערכם ולהציג כפלט למסך
קישור לתוכנית https://wokwi.com/projects/343256843804475986 נפעיל את התוכנית על ידי לחציה על הכפתור הירוק
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 |
// robotics blocks - Learn button int temp; float v; void setup() { Serial.begin(9600); temp=31; } void loop() { // read the state of the pushbutton value: v = analogRead(A2); if (v>500 ) { Serial.print("bigger then 2.5v "); } if ( v==0 ) { Serial.print(" 0 volt "); } if ( v<500) { Serial.print(" lower then 2.5 V "); } Serial.println(v/1023*5); // convert to volt delay(1000); // wait 1 sec (1 sec = 1000 ms) |
שים לב :
- במשפט IF אין נקודה ; לאחר הסוגריים.
- שווה לוגי הינו פעמיים : ==
דוגמא 2 דוגמא לערך דגיטלי (נבנה את הסכמה של המעגל)
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 |
// robotics blocks - Learn button int buttonPin = 4; // the number of the pushbutton pin int ledPin = 13; // the number of the LED pin // variables will change: void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input: } void loop() { // read the state of the pushbutton value: if ( digitalRead(buttonPin) == LOW) { // turn LED on: digitalWrite(ledPin, HIGH); Serial.println("BUTTON PRESS"); } else { // turn LED off: digitalWrite(ledPin, LOW); } delay(100); // Added the delay so that we can see the output of button } |
דגשים ללימוד :
:משפט IF – מתקיים רק במצב אמת
קלט מסוג : INPUT_PULLUP
דוגמא 3 הוספת למקירובקר זמזם BUZZER ו LED
שימוש הפקודה tone
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 |
// robotics blocks - Learn button int buttonPin = 4; // the number of the pushbutton pin int ledPin = 13; // the number of the LED pin int Buz = 7; // variables will change: void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: pinMode(Buz, OUTPUT); // initialize the LED pin as an output: pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input: tone(Buz, 1000, 50); // Plays 262Hz tone for 0.250 seconds delay(250); tone(Buz, 1000, 50); // Plays 262Hz tone for 0.250 seconds } void loop() { // read the state of the pushbutton value: if ( digitalRead(buttonPin) == LOW) { // turn LED on: digitalWrite(ledPin, HIGH); tone(Buz, 1000, 50); // Plays 262Hz tone for 0.050 seconds digitalWrite(Buz, HIGH); Serial.println("BUTTON PRESS"); } else { // turn LED off: digitalWrite(ledPin, LOW); } delay(10); // Added the delay so that we can see the output of button } |
תיקון שגיאת קימפול בתוכנה הבאה :
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 |
// robotics blocks - Learn button int temp; float v void setup() { Serial.begin(9600); temp=31; } void loop() { // read the state of the pushbutton value: v = analogRead(A2); if (v>500 ) { Serial.print("bigger then 2.5v "); } if ( v==0 ) { Serial.print(" 0 volt "); } if ( v<500) { Serial.print(" lower then 2.5 V "); } Serial.println(v/1023*5); // convert to volt delay(1000); // wait 1 sec (1 sec = 1000 ms) } |
רמז בשורה 5 חסר משהו אחרי האות V……………………………………………………………חסר נקודה פסיק
תרגיל בית :
בנה תוכנה המחורבת לפוטנציומטר
אם הערך גדול מ 500 הדלק אור אדום עם לד , אם הערך קטן מ 500 הדלק או ירוק עם לד .