קורס אמבדד – 002 – RB18 – תירגול שפת C , תוך לימוד אמבדד C
אתרים של רכיבים :
תרגיל : להיכנס לספאקפאן לחפש
- חיישן צבע COLOR SESNOR
-
AS7265
חזרה : לולאות , מחרוזות , לולאות .
נתחיל ממשהו כיפי…. חזרה על לולאת FOR
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": "Arvind Patil", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 146.33, "left": -51.88, "attrs": {} }, { "type": "wokwi-buzzer", "id": "bz1", "top": -19.75, "left": 74.81, "attrs": { "volume": "0.1" } }, { "type": "wokwi-led", "id": "led1", "top": -24.18, "left": 267.66, "attrs": { "color": "green" } } ], "connections": [ [ "bz1:2", "uno:9", "red", [ "v89.07", "h-7.33" ] ], [ "bz1:1", "uno:GND.1", "black", [ "v75.73", "h-51.33" ] ], [ "led1:A", "uno:9", "red", [ "v84.4", "h168" ] ], [ "led1:C", "uno:GND.3", "black ", [ "v187.06", "h208" ] ] ] } |
יצירת צללים פשוטים :
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 |
/* robotronix.co.il play tone with a loop */ const int speaker1 = 9; const int Led1 = 9; void setup() { pinMode(speaker1, OUTPUT); for(int i=0;i<5;i=i+1) // I=I+1 --> I++ { tone(speaker1, 1200); // Send 1200KHz sound signal, which is a G tone delay(300); tone(speaker1, 1400); // Send 1400KHz sound signal, which is a G tone delay(300); noTone(speaker1); // Stop sound delay(200); } } void loop() {} |
https://wokwi.com/projects/336059428787716692
גירוסקופ , אקסלומטר – גימבל – מערכות יצוב
יצוב מערכות מכניות
MPU6050
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 108 109 110 111 112 113 114 115 116 117 |
// Basic demo for accelerometer readings from Adafruit MPU6050 #include <Adafruit_MPU6050.h> #include <Adafruit_Sensor.h> #include <Wire.h> Adafruit_MPU6050 mpu; void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("Adafruit MPU6050 test!"); // Try to initialize! if (!mpu.begin()) { Serial.println("Failed to find MPU6050 chip"); while (1) { delay(10); } } Serial.println("MPU6050 Found!"); mpu.setAccelerometerRange(MPU6050_RANGE_8_G); Serial.print("Accelerometer range set to: "); switch (mpu.getAccelerometerRange()) { case MPU6050_RANGE_2_G: Serial.println("+-2G"); break; case MPU6050_RANGE_4_G: Serial.println("+-4G"); break; case MPU6050_RANGE_8_G: Serial.println("+-8G"); break; case MPU6050_RANGE_16_G: Serial.println("+-16G"); break; } mpu.setGyroRange(MPU6050_RANGE_2000_DEG); Serial.print("Gyro range set to: "); switch (mpu.getGyroRange()) { case MPU6050_RANGE_250_DEG: Serial.println("+- 250 deg/s"); break; case MPU6050_RANGE_500_DEG: Serial.println("+- 500 deg/s"); break; case MPU6050_RANGE_1000_DEG: Serial.println("+- 1000 deg/s"); break; case MPU6050_RANGE_2000_DEG: Serial.println("+- 2000 deg/s"); break; } mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); Serial.print("Filter bandwidth set to: "); switch (mpu.getFilterBandwidth()) { case MPU6050_BAND_260_HZ: Serial.println("260 Hz"); break; case MPU6050_BAND_184_HZ: Serial.println("184 Hz"); break; case MPU6050_BAND_94_HZ: Serial.println("94 Hz"); break; case MPU6050_BAND_44_HZ: Serial.println("44 Hz"); break; case MPU6050_BAND_21_HZ: Serial.println("21 Hz"); break; case MPU6050_BAND_10_HZ: Serial.println("10 Hz"); break; case MPU6050_BAND_5_HZ: Serial.println("5 Hz"); break; } Serial.println(""); delay(100); } void loop() { /* Get new sensor events with the readings */ sensors_event_t a, g, temp; mpu.getEvent(&a, &g, &temp); /* Print out the values */ Serial.print("Acceleration X: "); Serial.print(a.acceleration.x); Serial.print(", Y: "); Serial.print(a.acceleration.y); Serial.print(", Z: "); Serial.print(a.acceleration.z); Serial.println(" m/s^2"); Serial.print("Rotation X: "); Serial.print(g.gyro.x); Serial.print(", Y: "); Serial.print(g.gyro.y); Serial.print(", Z: "); Serial.print(g.gyro.z); Serial.println(" rad/s"); Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degC"); Serial.println(""); delay(500); } |
שוב חזרה קצרה מאוד
https://wokwi.com/projects/340367397829476948
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } |
נחזור לצלילים בקרוב ….
סוגי משתנים :
מערך חד מימדי
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %d\n", j, n[j] ); } return 0; } |
פקודה RANDOMIZE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> #include <conio.h> #include <stdlib.h> int main () { srand(time(0)); float n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] =rand()%100 ; /* set random numbers 0 - 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %.2f\n", j, n[j] ); } return 0; } |
שימוש במחרוזות
טבלת ASCII
Function | Syntax (or) Example | Description |
---|---|---|
strcpy() | strcpy(string1, string2) | Copies string2 value into string1 |
strncpy() | strncpy(string1, string2, 5) | Copies first 5 characters string2 into string1 |
strlen() | strlen(string1) | returns total number of characters in string1 |
strcat() | strcat(string1,string2) | Appends string2 to string1 |
strncat() | strncpy(string1, string2, 4) | Appends first 4 characters of string2 to string1 |
strcmp() | strcmp(string1, string2) | Returns 0 if string1 and string2 are the same; less than 0 if string1<string2; greater than 0 if string1>string2 |
strncmp() | strncmp(string1, string2, 4) | Compares first 4 characters of both string1 and string2 |
strcmpi() | strcmpi(string1,string2) | Compares two strings, string1 and string2 by ignoring case (upper or lower) |
stricmp() | stricmp(string1, string2) | Compares two strings, string1 and string2 by ignoring case (similar to strcmpi()) |
strlwr() | strlwr(string1) | Converts all the characters of string1 to lower case. |
strupr() | strupr(string1) | Converts all the characters of string1 to upper case. |
strdup() | string1 = strdup(string2) | Duplicated value of string2 is assigned to string1 |
strchr() | strchr(string1, ‘b’) | Returns a pointer to the first occurrence of character ‘b’ in string1 |
strrchr() | ‘strrchr(string1, ‘b’) | Returns a pointer to the last occurrence of character ‘b’ in string1 |
strstr() | strstr(string1, string2) | Returns a pointer to the first occurrence of string2 in string1 |
strset() | strset(string1, ‘B’) | Sets all the characters of string1 to given character ‘B’. |
strnset() | strnset(string1, ‘B’, 5) | Sets the first 5 characters of string1 to the given character ‘B’. |
strrev() | strrev(string1) | It reverses the value of string1 |
מערך רב מימדי
1 2 3 4 5 |
int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; /* output each array element's value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %d\n", i,j, a[i][j] ); } } return 0; } |
ניגוון יונתן הקטן
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() { } |
בניית מקליט "מנגינה" (פשוט) תרגיל כיתה
כפתור אדום מקליט את התוים (אותם שווי זמן – נזניח זמן לחיצת הכפתור בתרגיל זה) לתוך מערך
כפתור ירוק משמיע את התווים מתורך מערך
uart
raspberry pi uart
I2C
https://wokwi.com/projects/334566716332835412
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
//Kombinasi 4 (GYROSCOPE + OLED) //Menampilkan Logo Indobot Academy lalu Gerak Gyroscope //Tampilan di OLED //Link Wokwi : https://wokwi.com/projects/334566716332835412 #include <Wire.h> #include <Adafruit_SSD1306.h> #include <Adafruit_GFX.h> #include <Adafruit_NeoPixel.h> Adafruit_SSD1306 display( 128, 64); // 128 pixels width, 64 pixels height #define LED_PIN 6 #define LED_COUNT 1 Adafruit_NeoPixel neoPixel( LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); const int mpuAddress = 0x68; // I2C address of the MPU-6050 float xByGyro, yByGyro, zByGyro; // Global variables for the rotation by gyro // Set the origin in the middle of the display const int xOrigin = 64; const int yOrigin = 32; const float viewDistance = 150.0; // higher for less perspective, lower for more. // Vertices for a cube // A cube has 8 corners and each coordinate has x,y,z values. #define NUM_VERTICES 8 const int cube_vertex[NUM_VERTICES][3] = { { -20, -20, 20 }, // x, y, z { 20, -20, 20 }, { 20, 20, 20 }, { -20, 20, 20 }, { -20, -20, -20 }, { 20, -20, -20 }, { 20, 20, -20 }, { -20, 20, -20 } }; // The wirefram is to display the lines on the OLED display // It contains the corners of the shape in 2D coordinates int wireframe[NUM_VERTICES][2]; // 'logo-iot', 128x64pxA const unsigned char Logologo_iot [] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 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 0x63, 0x9f, 0xfe, 0x7f, 0xe7, 0xe7, 0xfc, 0x79, 0xfe, 0x7e, 0x7f, 0xc7, 0x0f, 0xfc, 0xff, 0xfe, 0x73, 0x9f, 0xfe, 0x7f, 0xcf, 0xe7, 0xfe, 0x03, 0xfe, 0x7e, 0x3f, 0xc0, 0x1f, 0xfc, 0x03, 0xfe, 0x7f, 0x9f, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xef, 0xff, 0xef, 0x6f, 0xf7, 0xfe, 0xdf, 0xfd, 0xff, 0xff, 0xdf, 0xb7, 0xef, 0x6f, 0xff, 0xfd, 0x7f, 0xff, 0xff, 0xef, 0xff, 0xf7, 0xff, 0xfd, 0xff, 0xfe, 0xdf, 0xf6, 0xff, 0xef, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xef, 0xff, 0xed, 0x8f, 0xfd, 0xff, 0xfe, 0xdf, 0xf6, 0x7f, 0xef, 0xff, 0xff, 0xe7, 0xff, 0xe7, 0xe7, 0x7f, 0xff, 0xff, 0xff, 0xcf, 0xf9, 0xdf, 0xff, 0xb7, 0xe7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin( 115200); Wire.begin(); // Initialize the OLED display and test if it is connected. if( !display.begin( SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F( "SSD1306 allocation failed")); for(;;); // halt the sketch if error encountered } // Initialize the MPU-6050 and test if it is connected. Wire.beginTransmission( mpuAddress); Wire.write( 0x6B); // PWR_MGMT_1 register Wire.write( 0); // set to zero (wakes up the MPU-6050) auto error = Wire.endTransmission(); if( error != 0) { Serial.println(F( "Error, MPU-6050 not found")); for(;;); // halt the sketch if error encountered } // Initialize the NeoPixel neoPixel.begin(); display.clearDisplay(); //clear sebelum tampilan baru display.drawBitmap(0, 0, Logologo_iot, 128, 64, WHITE); display.display(); //tampilkan data delay(1000); } void loop() { Wire.beginTransmission( mpuAddress); Wire.write( 0x3B); // Starting with register 0x3B (ACCEL_XOUT_H) Wire.endTransmission( false); // No stop condition for a repeated start // The MPU-6050 has the values as signed 16-bit integers. // There are 7 values in 14 registers. int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ; Wire.requestFrom( mpuAddress, 14); // request a total of 14 bytes AcX = Wire.read()<<8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) AcY = Wire.read()<<8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) AcZ = Wire.read()<<8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) Tmp = Wire.read()<<8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) GyX = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) GyY = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) GyZ = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) // The acceleration is directly mapped into the angles. // That is rather artificial. // The combined gravity could be used for an angle, while ignoring the strength. // // The gyro sets the rotation speed. // The angle created by the rotation speed is added to angle by the accelerometer. // // The conversion from the sensor values to the rotation is just a value // that makes it look good on the display. float xByAccel = (float) AcX * 0.0001; // static angle by accelerometer float yByAccel = (float) AcY * 0.0001; float zByAccel = (float) AcZ * 0.0001; xByGyro += (float) GyX * 0.00001; // moving angle by gyro yByGyro += (float) GyY * 0.00001; zByGyro += (float) GyZ * 0.00001; float x = xByAccel + xByGyro; // combine both angles float y = yByAccel + yByGyro; float z = zByAccel + zByGyro; // Keep the radians in range (although the cos/sin functions accept every value) if( x < 0.0) x += 2.0 * M_PI; else if( x > 2.0 * M_PI) x -= 2.0 * M_PI; if( y < 0.0) y += 2.0 * M_PI; else if( y > 2.0 * M_PI) y -= 2.0 * M_PI; if( z < 0.0) z += 2.0 * M_PI; else if( z > 2.0 * M_PI) z -= 2.0 * M_PI; // Draw 3D picture for (int i = 0; i < NUM_VERTICES; i++) { // Rotate Y float rotx = cube_vertex[i][2] * sin(y) + cube_vertex[i][0] * cos(y); float roty = cube_vertex[i][1]; float rotz = cube_vertex[i][2] * cos(y) - cube_vertex[i][0] * sin(y); // Rotate X float rotxx = rotx; float rotyy = roty * cos(x) - rotz * sin(x); float rotzz = roty * sin(x) + rotz * cos(x); // Rotate Z float rotxxx = rotxx * cos(z) - rotyy * sin(z); float rotyyy = rotxx * sin(z) + rotyy * cos(z); float rotzzz = rotzz; // Add depth perspective rotxxx *= viewDistance / (viewDistance + rotzzz); rotyyy *= viewDistance / (viewDistance + rotzzz); // Bring to middle of screen rotxxx += (float) xOrigin; rotyyy += (float) yOrigin; // Store new vertices values for wireframe drawing wireframe[i][0] = (int) rotxxx; wireframe[i][1] = (int) rotyyy; wireframe[i][2] = (int) rotzzz; } draw_wireframe(); // Set the color of the NeoPixel according to the temperature // Temperature by the MPU-6050 is -40 to 85. // According to the datasheet: // Temperature in Celsius = (raw_value / 340) + 36.53 // The Hue range for the NeoPixel is the full uint16_t range. float Celsius = ((float) Tmp / 340.00) + 36.53; float hue = (Celsius + 40.0) / 125.0 * 65535.0; uint32_t rgbcolor = neoPixel.ColorHSV( (uint16_t) hue); neoPixel.setPixelColor( 0, rgbcolor); neoPixel.show(); // update new values to NeoPixel } void draw_wireframe(void) { // Start with a empty buffer display.clearDisplay(); // A cube has 8 points and 12 sides. // The wireframe contains the 8 points, and the 12 lines are drawn here. display.drawLine( wireframe[0][0], wireframe[0][1], wireframe[1][0], wireframe[1][1], SSD1306_WHITE); display.drawLine( wireframe[1][0], wireframe[1][1], wireframe[2][0], wireframe[2][1], SSD1306_WHITE); display.drawLine( wireframe[2][0], wireframe[2][1], wireframe[3][0], wireframe[3][1], SSD1306_WHITE); display.drawLine( wireframe[3][0], wireframe[3][1], wireframe[0][0], wireframe[0][1], SSD1306_WHITE); display.drawLine( wireframe[4][0], wireframe[4][1], wireframe[5][0], wireframe[5][1], SSD1306_WHITE); display.drawLine( wireframe[5][0], wireframe[5][1], wireframe[6][0], wireframe[6][1], SSD1306_WHITE); display.drawLine( wireframe[6][0], wireframe[6][1], wireframe[7][0], wireframe[7][1], SSD1306_WHITE); display.drawLine( wireframe[7][0], wireframe[7][1], wireframe[4][0], wireframe[4][1], SSD1306_WHITE); display.drawLine( wireframe[0][0], wireframe[0][1], wireframe[4][0], wireframe[4][1], SSD1306_WHITE); display.drawLine( wireframe[1][0], wireframe[1][1], wireframe[5][0], wireframe[5][1], SSD1306_WHITE); display.drawLine( wireframe[2][0], wireframe[2][1], wireframe[6][0], wireframe[6][1], SSD1306_WHITE); display.drawLine( wireframe[3][0], wireframe[3][1], wireframe[7][0], wireframe[7][1], SSD1306_WHITE); // Extra cross face on one side display.drawLine( wireframe[1][0], wireframe[1][1], wireframe[3][0], wireframe[3][1], SSD1306_WHITE); display.drawLine( wireframe[0][0], wireframe[0][1], wireframe[2][0], wireframe[2][1], SSD1306_WHITE); // Write the new picture to the display display.display(); } |