Author : Ila MulyandiNo comments
3D Cube Rendering w/ LCD TFT 3,2" Arduino Mega2560 + Joystick + ADXL345 Accel Sensor
Langsung aja cekidot Sourch Codenya..
#include <Wire.h> // i2C Connection Library #include <Adafruit_Sensor.h> // Adafruit Sensor for ADXL345 #include <Adafruit_ADXL345_U.h> // ADXL345 Library Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); #define BLACK 0x0000 #define WHITE 0xFFFF #include <TFT_HX8357.h> // LCD UHD 3,2" Mega Shield Library TFT_HX8357 tft = TFT_HX8357(); // Invoke custom library int16_t h; int16_t w; int inc = -2; float xx, xy, xz; float yx, yy, yz; float zx, zy, zz; float fact; int Xan, Yan; int Xoff; int Yoff; int Zoff; struct Point3d { int x; int y; int z; }; struct Point2d { int x; int y; }; int LinestoRender; int OldLinestoRender; struct Line3d { Point3d p0; Point3d p1; }; struct Line2d { Point2d p0; Point2d p1; }; Line3d Lines[20]; Line2d Render[20]; Line2d ORender[20]; /***********************************************************************************************************************************/ char c; uint8_t mode = 0; void setup() { Serial.begin(9600); pinMode (A2,INPUT_PULLUP); digitalWrite (A2,HIGH); if(!accel.begin()) { /* There was a problem detecting the ADXL345 ... check your connections */ //Serial.println("Ooops, no ADXL345 detected ... Check your wiring!"); while(1); } accel.setRange(ADXL345_RANGE_16_G); tft.init(); sensors_event_t event; accel.getEvent(&event); h = tft.height(); w = tft.width(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); cube(); fact = 180 / 3.14159259; // conversion from degrees to radians. Xoff = 240; Yoff = 160; Zoff = 350; //Ukuran Kubus } /***********************************************************************************************************************************/ void loop() { if (Serial.available()){ c = Serial.read(); if (c=='1') mode=1; if (c=='0') mode=0; } // Rotate around x and y axes in 1 degree increments //Xan++; //Yan++; if (mode==0){ // Rotate around x and y axes with joystick Xan = map(analogRead(A0),0,1023,0,360); Yan = map(analogRead(A1),0,1023,0,360); } if (mode==1){ // Rotate around x and y axes with gyro sensor sensors_event_t event; accel.getEvent(&event); Xan = map((event.acceleration.x*100),-1000,1000,360,0); Yan = map((event.acceleration.y*100),-1000,1000,360,0); } Yan = Yan % 360; Xan = Xan % 360; // prevents overflow. SetVars(); //sets up the global vars to do the 3D conversion. // Zoom in and out on Z axis within limits // the cube intersects with the screen for values < 160 if (digitalRead(A2)== LOW ) {Zoff += inc; if (Zoff > 500) inc = -1; else if (Zoff < 160) inc = 1; } for (int i = 0; i < LinestoRender ; i++) { ORender[i] = Render[i]; ProcessLine(&Render[i], Lines[i]); } RenderImage(); delay(9); } /***********************************************************************************************************************************/ void RenderImage( void) { for (int i = 0; i < OldLinestoRender; i++ ) { tft.drawLine(ORender[i].p0.x, ORender[i].p0.y, ORender[i].p1.x, ORender[i].p1.y, BLACK); // erase the old lines. } for (int i = 0; i < LinestoRender; i++ ) { uint16_t color = TFT_BLUE; if (i < 4) color = TFT_RED; if (i > 7) color = TFT_GREEN; tft.drawLine(Render[i].p0.x, Render[i].p0.y, Render[i].p1.x, Render[i].p1.y, color); } OldLinestoRender = LinestoRender; } /***********************************************************************************************************************************/ void SetVars(void) { float Xan2, Yan2, Zan2; float s1, s2, s3, c1, c2, c3; Xan2 = Xan / fact; // convert degrees to radians. Yan2 = Yan / fact; s1 = sin(Yan2); s2 = sin(Xan2); c1 = cos(Yan2); c2 = cos(Xan2); xx = c1; xy = 0; xz = -s1; yx = (s1 * s2); yy = c2; yz = (c1 * s2); zx = (s1 * c2); zy = -s2; zz = (c1 * c2); } /***********************************************************************************************************************************/ void ProcessLine(struct Line2d *ret, struct Line3d vec) { float zvt1; int xv1, yv1, zv1; float zvt2; int xv2, yv2, zv2; int rx1, ry1; int rx2, ry2; int x1; int y1; int z1; int x2; int y2; int z2; int Ok; x1 = vec.p0.x; y1 = vec.p0.y; z1 = vec.p0.z; x2 = vec.p1.x; y2 = vec.p1.y; z2 = vec.p1.z; Ok = 0; // defaults to not OK xv1 = (x1 * xx) + (y1 * xy) + (z1 * xz); yv1 = (x1 * yx) + (y1 * yy) + (z1 * yz); zv1 = (x1 * zx) + (y1 * zy) + (z1 * zz); zvt1 = zv1 - Zoff; if ( zvt1 < -5) { rx1 = 256 * (xv1 / zvt1) + Xoff; ry1 = 256 * (yv1 / zvt1) + Yoff; Ok = 1; // ok we are alright for point 1. } xv2 = (x2 * xx) + (y2 * xy) + (z2 * xz); yv2 = (x2 * yx) + (y2 * yy) + (z2 * yz); zv2 = (x2 * zx) + (y2 * zy) + (z2 * zz); zvt2 = zv2 - Zoff; if ( zvt2 < -5) { rx2 = 256 * (xv2 / zvt2) + Xoff; ry2 = 256 * (yv2 / zvt2) + Yoff; } else { Ok = 0; } if (Ok == 1) { ret->p0.x = rx1; ret->p0.y = ry1; ret->p1.x = rx2; ret->p1.y = ry2; } } /***********************************************************************************************************************************/ void cube(void) { // Front Face. Lines[0].p0.x = -50; Lines[0].p0.y = -50; Lines[0].p0.z = 50; Lines[0].p1.x = 50; Lines[0].p1.y = -50; Lines[0].p1.z = 50; Lines[1].p0.x = 50; Lines[1].p0.y = -50; Lines[1].p0.z = 50; Lines[1].p1.x = 50; Lines[1].p1.y = 50; Lines[1].p1.z = 50; Lines[2].p0.x = 50; Lines[2].p0.y = 50; Lines[2].p0.z = 50; Lines[2].p1.x = -50; Lines[2].p1.y = 50; Lines[2].p1.z = 50; Lines[3].p0.x = -50; Lines[3].p0.y = 50; Lines[3].p0.z = 50; Lines[3].p1.x = -50; Lines[3].p1.y = -50; Lines[3].p1.z = 50; //back face. Lines[4].p0.x = -50; Lines[4].p0.y = -50; Lines[4].p0.z = -50; Lines[4].p1.x = 50; Lines[4].p1.y = -50; Lines[4].p1.z = -50; Lines[5].p0.x = 50; Lines[5].p0.y = -50; Lines[5].p0.z = -50; Lines[5].p1.x = 50; Lines[5].p1.y = 50; Lines[5].p1.z = -50; Lines[6].p0.x = 50; Lines[6].p0.y = 50; Lines[6].p0.z = -50; Lines[6].p1.x = -50; Lines[6].p1.y = 50; Lines[6].p1.z = -50; Lines[7].p0.x = -50; Lines[7].p0.y = 50; Lines[7].p0.z = -50; Lines[7].p1.x = -50; Lines[7].p1.y = -50; Lines[7].p1.z = -50; // now the 4 edge lines. Lines[8].p0.x = -50; Lines[8].p0.y = -50; Lines[8].p0.z = 50; Lines[8].p1.x = -50; Lines[8].p1.y = -50; Lines[8].p1.z = -50; Lines[9].p0.x = 50; Lines[9].p0.y = -50; Lines[9].p0.z = 50; Lines[9].p1.x = 50; Lines[9].p1.y = -50; Lines[9].p1.z = -50; Lines[10].p0.x = -50; Lines[10].p0.y = 50; Lines[10].p0.z = 50; Lines[10].p1.x = -50; Lines[10].p1.y = 50; Lines[10].p1.z = -50; Lines[11].p0.x = 50; Lines[11].p0.y = 50; Lines[11].p0.z = 50; Lines[11].p1.x = 50; Lines[11].p1.y = 50; Lines[11].p1.z = -50; LinestoRender = 12; OldLinestoRender = LinestoRender; }
Posted On : Sunday, February 5, 2017Time : 10:04 PM