לימוד ארדואינו : שיעור 15 – הפקודה sprintf -קורס C506 – הדפסת משתנה של מספרים שלמים , מחרוזות , ומשתנה עשרוני
נילמד להשתמש בפקדוה dtostrf()
converts a float to a string
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 |
// www.robotronix.co.il // לימוד שפת סי , בסביבת ארדואינו קורס C506 // רובוטרוניקס מפתחת תוכנה ואלקטרוניקה - משדרים כרטיסים אלקטרונים // קורסים למהנדסים , בתי ספר , ונוער #include <stdio.h> #include <string.h> unsigned char i; char buf[80]; int Level; void setup() { Serial.begin(9600); // put your setup code here, to run once: Level=10; i=0; for (i=0;i<=255;i++) { sprintf(buf,"%d %c, ",i,i); Serial.println(buf); delay(150); // 150 ms -- ? 1/8 sec } } void loop() { } |
הדפסת מחזרות מערך רב מימדי
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 |
// www.robotronix.co.il // לימוד שפת סי , בסביבת ארדואינו קורס C506 // רובוטרוניקס מפתחת תוכנה ואלקטרוניקה - משדרים כרטיסים אלקטרונים // קורסים למהנדסים , בתי ספר , ונוער #include <stdio.h> #include <string.h> #define SizeTemp 6 float temp[SizeTemp]; void setup() { char buf[100]; char t[10][100]; int i; Serial.begin(9600); strcpy(t[0],"www.Robotronix.co.il"); strcpy(t[1],"www.stm.com"); strcpy(t[2],"www.mouser.com"); strcpy(t[3],"www.nxp.com"); Serial.println("searching sites ..."); for(i=0;i<=3;i++) { sprintf(buf," Serching site : %s",t[i]); Serial.println(buf); delay(1000); // 150 ms -- ? 1/8 sec } } void loop() { // put your main code here, to run repeatedly: } |
כיצד להציג FLOAT בתוך SPRINTF – בקומפיילר של ארדואינו זה לא עובד ולכן נעשה טריק – נמיר את המשתנה מטיפוס FLOAT ל למחרוזת – ואותו נדפיס ב SPRINTF
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 |
// www.robotronix.co.il // לימוד שפת סי , בסביבת ארדואינו קורס C506 // רובוטרוניקס מפתחת תוכנה ואלקטרוניקה - משדרים כרטיסים אלקטרונים // קורסים למהנדסים , בתי ספר , ונוער #include <stdio.h> #include <string.h> #include <float.h> unsigned char i; char buf[80]; char t[10]; int Level; float temp1, temp2; void setup() { Serial.begin(9600); // put your setup code here, to run once: Level=10; temp1 = random(24,31); temp1=27.5278; //Serial.println(temp1); // will not work in arduino // sprintf(buf," temp1 : %.2f",temp1); // dtostrf(float, StringSapceLeftOrRight , numVarsAfterDecimal, string); // StringSapceLeftOrRight == can be + or - number // exm1 dtostrf(temp1, 8, 2 ,t); //sprintf(buf," temp1 : %.2f",temp1); sprintf(buf," temp1 : [%s]",t); Serial.println(buf); // exm1 dtostrf(temp1, -8, 2 ,t); //sprintf(buf," temp1 : %.2f",temp1); sprintf(buf," temp1 : [%s]",t); Serial.println(buf); // exm3 dtostrf(temp1, 5 , 2, t); //sprintf(buf," temp1 : %.2f",temp1); sprintf(buf," temp1 : [%s]",t); Serial.println(buf); delay(150); // 150 ms -- ? 1/8 sec } void loop() { } |