Components and supplies
sharp distance sensor
Arduino UNO
Stepper Motor, Mini Step
power supply 12V AC/DC
Stepper motor driver board A4988
Apps and platforms
Arduino IDE
Processing
Project description
Code
pfa2.pde
processing
first , all distance measures will be saved in an array list then processing will use it to draw many points that will form our objects 3d model
1/* 2i have plenty of ideas to make this code better and easier to use but unfortunately i can't find enough time for that because of study but it's functional so i will be glad to see what is the changes you made 3*/ 4import processing.serial.*; 5boolean startScanButtonPressed ; 6Serial myPort; 7PShape button ; 8float y ; 9float f ; 10float maxVirtualHigh = -40 ; 11float stepHigh = 5 ; 12float distance ; 13int secPass ; 14ArrayList <PVector> twod = new ArrayList<PVector>() ; 15ArrayList <PVector> threed = new ArrayList<PVector>() ; 16int pointIndex ; 17float angle ; 18float oneStepDegree = 10 ; 19boolean startDrawing ; 20boolean viewModeOn ; 21boolean changeBg ; 22void setup() 23{ 24 size(800,700,P3D); 25 myPort = new Serial(this, "COM4", 9600); 26 angle = 0 ; 27 startDrawing = false ; 28 myPort.bufferUntil('\ 29'); 30 startScanButtonPressed = false ; 31 viewModeOn = false ; 32 changeBg = false ; 33button() ; 34} 35 36void draw() { 37 if (startScanButtonPressed == true) { //if we clicked in the window 38 myPort.write('1'); //send a 1 39 println("receiving data from arduino ..."); 40 delay(200) ; 41 changeBg = true ; 42 startScanButtonPressed = false ; 43 } 44 else { 45if (changeBg){ 46background(0) ; 47} 48 println("press button to start ...") ; 49 if ((y<=-20)&&(startDrawing == true)) 50viewModeOn = true ; 51 pushMatrix() ; 52translate(250,400,f) ; 53 if (startDrawing == true){ 54myPort.stop(); // seeME PLEASE 55mouseDragged() ; 56drawMe() ; 57 } 58popMatrix() ; 59programControl(secPass) ; 60save("Object.OBG") ; 61 } 62} 63 64//---------------------------------------------------------------------- 65void mousePressed(){ 66if ((150 <= mouseX)&&(mouseX <= 350)&&(220 <= mouseY) 67&&(mouseY <= 260)){ 68startScanButtonPressed = true ; 69} 70else startScanButtonPressed = false ; 71} 72 73void button(){ 74button = createShape() ; 75button.beginShape() ; 76fill(0,0,250) ; 77rect(150,220,200,40) ; 78fill(255) ; 79textSize(18) ; 80text("Start Scanning",190,245) ; 81button.endShape() ; 82} 83 84float serialEvent(Serial myPort){ 85 distance = float(myPort.readStringUntil('\ 86')) ; 87 secPass++ ; 88 threed.add(new PVector()) ; 89 twod.add(new PVector()) ; 90 twod.get(pointIndex).y = distance ; 91 twod.get(pointIndex).rotate(angle) ; 92 threed.get(pointIndex).z = twod.get(pointIndex).y ; 93 threed.get(pointIndex).x = twod.get(pointIndex).x ; 94 threed.get(pointIndex).y = y ; 95angle = oneStepDegree+angle ; 96 if (angle >= 360) 97 { 98 y-=stepHigh ; 99 angle = 0 ; 100 if (y <= maxVirtualHigh){ 101 startDrawing = true ; 102 } 103} 104pointIndex++ ; 105return distance ; 106 } 107 108void drawMe(){ 109 stroke(255) ; 110beginShape(POINTS) ; // QUADS LINES 111 for(int j = 0 ; j<threed.size() ; j++){ 112vertex(threed.get(j).x,threed.get(j).y,threed.get(j).z) ; 113} 114endShape(CLOSE) ; // CLOSE 115} 116 117// WATCH DATA 118void programControl(int onePercent){ 119 String msg ; 120println(" angle = ",angle) ; 121println(" startDrawing =",startDrawing) ; 122text("pixel number = " + pointIndex,20,20) ; 123println("viewModeOn =",viewModeOn) ; 124println("distance =",distance) ; 125msg = "uploading ... "+percentage(onePercent)+" %" ; 126if (percentage(onePercent)>100){ 127 onePercent = 0 ; 128 msg = "" ; 129 } 130text(msg,20,45) ; 131save("model.tif") ; 132} 133// CAMERA ROTATION 134void mouseDragged(){ 135 if (viewModeOn == true){ 136rotateX(mouseX*0.1) ; 137rotateY(mouseY*0.1) ; 138} 139} 140 141void mouseWheel(MouseEvent event){ 142float e = event.getCount() ; 143f += e*(6) ; 144} 145 146int percentage(int secPass){ 147 float huPer = ((360/oneStepDegree)*(-maxVirtualHigh/stepHigh)) ; //100% 148 int percentage = int((100/huPer)*secPass) ; 149return percentage ; 150}
arduino.exe
arduino
1 char val; // Data received from the serial port 2 int ledPin = 13; // Set the pin to digital I/O 13 3 boolean startCalculatingPointsCoordinations ; 4 const int stepPiny = 13 ; 5const int dirPiny = 12 ; 6const int stepPinx = 6 ; 7const int dirPinx = 7 ; 8int motorStepsX = 36 ; // it will take 36 points in each full rotation 9int motorStepsY = 36 ; // the more you make points the better picture // resolution you get 10float maxDistance = 150 ; 11#define sensor A0 12 void setup() { 13 pinMode(stepPiny,OUTPUT) ; 14pinMode(dirPiny, OUTPUT) ; 15pinMode(stepPinx,OUTPUT) ; 16pinMode(dirPinx, OUTPUT) ; 17 pinMode(ledPin, OUTPUT); 18 Serial.begin(9600); 19 startCalculatingPointsCoordinations = false ; 20 } 21 void loop() { 22 if (Serial.available()){ // If data is available to read, 23 val = Serial.read(); // read it and store it in val 24 } 25 if (val == '1') 26 { // If 1 was received 27 digitalWrite(ledPin, HIGH); // turn the LED on 28 startCalculatingPointsCoordinations = true ; 29 } else { 30 digitalWrite(ledPin, LOW); // otherwise turn it off 31 } 32 if(startCalculatingPointsCoordinations){ 33//--------------------moving motors-------------------------------------- 34 for(int y = 0 ; y < motorStepsY ; y++){ 35// horizental axe 36for(int x = 0 ; x < motorStepsX ; x++){ 37calculateDistance() ; 38moveMotorX() ; 39} 40moveMotorY() ; 41float actualDistance = calculateDistance() ; 42if ((actualDistance > maxDistance)&&(y > 40)){ 43stopMotors() ; 44} 45} 46//---------------------------------------------------------------- 47 } 48delay(10); // Wait 10 milliseconds for next reading 49} 50 51void moveMotorX(){ 52digitalWrite(dirPiny,HIGH) ; 53digitalWrite(stepPinx,HIGH); 54delay(200); 55digitalWrite(stepPinx,LOW); 56delay(200); 57} 58 59void moveMotorY(){ 60 for (int i = 0 ; i <120; i++){ 61 digitalWrite(dirPiny,HIGH) ; 62digitalWrite(stepPiny,HIGH) ; 63delay(50) ; 64digitalWrite(stepPiny,LOW) ; 65delay(50) ; 66 } 67} 68 69void stopMotors(){ 70digitalWrite(stepPinx,LOW); 71digitalWrite(stepPinx,LOW); 72} 73 74float calculateDistance(){ 75 float volts = analogRead(sensor)*0.0048828125; 76 int distance1 = 13*pow(volts, -1); 77 distance1 = map(distance1,4,45,0,200) ; 78 int distance2 = constrain(distance1,0,200) ; 79 delay(200); // 200 80 Serial.println(distance2); 81 Serial.flush() ; 82 return distance2 ; 83}
pfa2.pde
processing
first , all distance measures will be saved in an array list then processing will use it to draw many points that will form our objects 3d model
1/* 2i have plenty of ideas to make this code better and easier to use but unfortunately i can't find enough time for that because of study but it's functional so i will be glad to see what is the changes you made 3*/ 4import processing.serial.*; 5boolean startScanButtonPressed ; 6Serial myPort; 7PShape button ; 8float y ; 9float f ; 10float maxVirtualHigh = -40 ; 11float stepHigh = 5 ; 12float distance ; 13int secPass ; 14ArrayList <PVector> twod = new ArrayList<PVector>() ; 15ArrayList <PVector> threed = new ArrayList<PVector>() ; 16int pointIndex ; 17float angle ; 18float oneStepDegree = 10 ; 19boolean startDrawing ; 20boolean viewModeOn ; 21boolean changeBg ; 22void setup() 23{ 24 size(800,700,P3D); 25 myPort = new Serial(this, "COM4", 9600); 26 angle = 0 ; 27 startDrawing = false ; 28 myPort.bufferUntil('\n'); 29 startScanButtonPressed = false ; 30 viewModeOn = false ; 31 changeBg = false ; 32button() ; 33} 34 35void draw() { 36 if (startScanButtonPressed == true) { //if we clicked in the window 37 myPort.write('1'); //send a 1 38 println("receiving data from arduino ..."); 39 delay(200) ; 40 changeBg = true ; 41 startScanButtonPressed = false ; 42 } 43 else { 44if (changeBg){ 45background(0) ; 46} 47 println("press button to start ...") ; 48 if ((y<=-20)&&(startDrawing == true)) 49viewModeOn = true ; 50 pushMatrix() ; 51translate(250,400,f) ; 52 if (startDrawing == true){ 53myPort.stop(); // seeME PLEASE 54mouseDragged() ; 55drawMe() ; 56 } 57popMatrix() ; 58programControl(secPass) ; 59save("Object.OBG") ; 60 } 61} 62 63//---------------------------------------------------------------------- 64void mousePressed(){ 65if ((150 <= mouseX)&&(mouseX <= 350)&&(220 <= mouseY) 66&&(mouseY <= 260)){ 67startScanButtonPressed = true ; 68} 69else startScanButtonPressed = false ; 70} 71 72void button(){ 73button = createShape() ; 74button.beginShape() ; 75fill(0,0,250) ; 76rect(150,220,200,40) ; 77fill(255) ; 78textSize(18) ; 79text("Start Scanning",190,245) ; 80button.endShape() ; 81} 82 83float serialEvent(Serial myPort){ 84 distance = float(myPort.readStringUntil('\n')) ; 85 secPass++ ; 86 threed.add(new PVector()) ; 87 twod.add(new PVector()) ; 88 twod.get(pointIndex).y = distance ; 89 twod.get(pointIndex).rotate(angle) ; 90 threed.get(pointIndex).z = twod.get(pointIndex).y ; 91 threed.get(pointIndex).x = twod.get(pointIndex).x ; 92 threed.get(pointIndex).y = y ; 93angle = oneStepDegree+angle ; 94 if (angle >= 360) 95 { 96 y-=stepHigh ; 97 angle = 0 ; 98 if (y <= maxVirtualHigh){ 99 startDrawing = true ; 100 } 101} 102pointIndex++ ; 103return distance ; 104 } 105 106void drawMe(){ 107 stroke(255) ; 108beginShape(POINTS) ; // QUADS LINES 109 for(int j = 0 ; j<threed.size() ; j++){ 110vertex(threed.get(j).x,threed.get(j).y,threed.get(j).z) ; 111} 112endShape(CLOSE) ; // CLOSE 113} 114 115// WATCH DATA 116void programControl(int onePercent){ 117 String msg ; 118println(" angle = ",angle) ; 119println(" startDrawing =",startDrawing) ; 120text("pixel number = " + pointIndex,20,20) ; 121println("viewModeOn =",viewModeOn) ; 122println("distance =",distance) ; 123msg = "uploading ... "+percentage(onePercent)+" %" ; 124if (percentage(onePercent)>100){ 125 onePercent = 0 ; 126 msg = "" ; 127 } 128text(msg,20,45) ; 129save("model.tif") ; 130} 131// CAMERA ROTATION 132void mouseDragged(){ 133 if (viewModeOn == true){ 134rotateX(mouseX*0.1) ; 135rotateY(mouseY*0.1) ; 136} 137} 138 139void mouseWheel(MouseEvent event){ 140float e = event.getCount() ; 141f += e*(6) ; 142} 143 144int percentage(int secPass){ 145 float huPer = ((360/oneStepDegree)*(-maxVirtualHigh/stepHigh)) ; //100% 146 int percentage = int((100/huPer)*secPass) ; 147return percentage ; 148}
Downloadable files
3d scanner schema
NOTE (very important) : there isn't a4988 driver in proteus 8 so i replaced it with l298 in simulation .
3d scanner schema
3d scanner schema
NOTE (very important) : there isn't a4988 driver in proteus 8 so i replaced it with l298 in simulation .
3d scanner schema
Comments
Only logged in users can leave comments
mohamedalimani
0 Followers
•0 Projects
Table of contents
Intro
1
0