Proyecto: Arduino + Processing + Bluetooth HC-05
Bluetooth communication between Arduino and the PC using a small interface programmed in Processing.
Components and supplies
RGB Diffused Common Cathode
Servo Motor HS-311
Jumper wires (generic)
Step Down DC/DC lm2596
HC-05 Bluetooth Module
Sharp 2Y0A21
Arduino UNO
Breadboard (generic)
Project description
Code
Processing Code
processing
1 import processing.serial.*; 2 3 Serial btserial; 4 5 PShape l , r, flecha; 6 float varin; 7 boolean over; 8 boolean click; 9 float pos0=2, pos1=2, pos2=2, pos3=2, pos4=2, pos5=2, pos6=2, pos7=2, pos8=2; 10 float datain, senIn, datasen; 11 String bufferin,ID="uno", ID2="dos",data,data2; 12 13 14void setup(){ 15 btserial=new Serial(this,Serial.list()[1],9600); 16 17 printArray(Serial.list()); 18 background(20); 19 size(600,200,P2D); 20 smooth(); 21 22 23 24 25} 26 27void draw(){ 28 29 textAlign(LEFT); 30 fill(255); 31 text("Arduino + HC05",10,20); 32 33 34 textAlign(CENTER); 35 text("Led",100,140); 36 text("Servo",400,165); 37 text("SHARP IR",230,35); 38 text("Distancia:",230,80); 39 fill(0); 40 rectMode(CENTER); 41 rect(230,94,58,18); 42 fill(255); 43if(senIn>80){text("F.D.R",230,100);}else{ 44 text(int(senIn),230,100);} 45 46 image(img1,430,-10,200,60); 47 image(img2,0,160); 48 image(img3,448,170,150,33); 49 50 // LED 51 stroke(126); 52 fill(200); 53 ellipseMode(CENTER); 54 if(mouseX>50 && mouseX<150 && mouseY>50 && mouseY<150) 55 {fill(255); 56 } 57 58 if(ID2.equals("D") && senIn>20 && senIn<30){fill(255,0,0);} 59 if(ID2.equals("D") && (senIn>=30 || senIn<=20)){fill(200);} 60 ellipse(100,100,50,50); 61 62 //Sensor SHARP 63 rectMode(CENTER); 64 fill(126); 65 rect(230,50,60,20); 66 rect(230,60,25,7); 67 fill(150); 68 rect(230,50,58,18); 69 fill(0); 70 ellipse(210,50,15,15); 71 fill(255,0,0); 72 ellipse(250,50,15,15); 73 74 75 //Radar o Movimiento del Servo 76 stroke(0); 77 strokeWeight(3); 78 //fill(56,206,56); 79 fill(100); 80 arc(400,150,200,200,-PI,0); 81 //stroke(100,100,100); 82 stroke(40); 83 strokeWeight(1); 84 line(400,150,400+(100*(cos(radians(22.5)))),150-(100*(radians(22.5)))); 85 line(400,150,400+(100*(cos(PI/4))),150-(100*(sin(PI/4)))); 86 line(400,150,400+(100*(cos(3*PI/8))),150-(100*(sin(3*PI/8)))); 87 line(400,150,400+(100*(cos(PI/2))),150-(100*(sin(PI/2)))); 88 line(400,150,400+(100*(cos(5*PI/8))),150-(100*(sin(5*PI/8)))); 89 line(400,150,400+(100*(cos(3*PI/4))),150-(100*(sin(3*PI/4)))); 90 line(400,150,400+(100*(cos(7*PI/8))),150-(100*(sin(7*PI/8)))); 91 92 93 94 if(mouseX>488 && mouseX<508 && mouseY>138 && mouseY<158) 95 {over=true;if(datain!=pos0){fill(0,255,0);} 96 if(over && mousePressed){click=true;pos0=180.00;} 97 if(click){btserial.write("S"+pos0);}}else{fill(2,165,0);} 98 if(ID!=null && ID.equals("S") && datain==180){fill(255,255,255);} 99 ellipse(498,148,20,20); //Elipse en 180 100 101 102 if(mouseX>470 && mouseX<500 && mouseY>102 && mouseY<122) 103 {over=true;if(datain!=pos1){fill(0,255,0);} 104 if(over && mousePressed){click=true;pos1=157.50;} 105 if(click){btserial.write("S"+pos1);}} else{fill(2,165,0);} 106 if(ID!=null && ID.equals("S") && datain==157){fill(255,255,255);} 107 ellipse(490,112,20,20);//Elipse en 157.5 108 109 110 if(mouseX>459 && mouseX<479 && mouseY>69 && mouseY<89) 111 {over=true;if(datain!=pos2){fill(0,255,0);} 112 if(over && mousePressed){click=true;pos2=135.000;} 113 if(click){btserial.write("S"+pos2);}}else{fill(2,165,0);} 114 if(ID!=null && ID.equals("S") && datain==135){fill(255,255,255);} 115 ellipse(469,79,20,20);//Elipse en 135 116 117 118 if(mouseX>427 && mouseX<447 && mouseY>47 && mouseY<67) 119 {over=true;if(datain!=pos3){fill(0,255,0);} 120 if(over && mousePressed){click=true;pos3=112.500;} 121 if(click){btserial.write("S"+pos3);}}else{fill(2,165,0);} 122 if(ID!=null && ID.equals("S") && datain==112){fill(255,255,255);} 123 ellipse(437,57,20,20);//Elipse en 112.5 124 fill(2,165,0); 125 126 if(mouseX>390 && mouseX<410 && mouseY>40 && mouseY<60) 127 {over=true;if(datain!=pos4){fill(0,255,0);} 128 if(over && mousePressed){click=true;pos4=90.000;} 129 if(click){btserial.write("S"+pos4);}}else{fill(2,165,0);} 130 if(ID!=null && ID.equals("S") && datain==90){fill(255,255,255);} 131 ellipse(400,50,20,20);// Elipse en 90 132 133 if(mouseX>351 && mouseX<371 && mouseY>47 && mouseY<67) 134 {over=true;if(datain!=pos5){fill(0,255,0);} 135 if(over && mousePressed){click=true;pos5=67.500;} 136 if(click){btserial.write("S"+pos5);}}else{fill(2,165,0);} 137 if(ID!=null && ID.equals("S") && datain==67){fill(255,255,255);} 138 ellipse(361,57,20,20);// Elipse en 67.5 139 140 if(mouseX>319 && mouseX<339 && mouseY>69 && mouseY<89) 141 {over=true;if(datain!=pos6){fill(0,255,0);} 142 if(over && mousePressed){click=true;pos6=45;} 143 if(click){btserial.write("S"+pos6);}}else{fill(2,165,0);} 144 if(ID!=null && ID.equals("S") && datain==45){fill(255,255,255);} 145 ellipse(329,79,20,20); // Elipse en 45 146 147 if(mouseX>300 && mouseX<320 && mouseY>102 && mouseY<122) 148 {over=true;if(datain!=pos6){fill(0,255,0);} 149 if(over && mousePressed){click=true;pos7=22.5;} 150 if(click){btserial.write("S"+pos7);}}else{fill(2,165,0);} 151 if(ID!=null && ID.equals("S") && datain==22.5){fill(255,255,255);} 152 ellipse(310,112,20,20);// Elipse en 22.5 153 154 155 if(mouseX>291 && mouseX<311 && mouseY>138 && mouseY<158) 156 {over=true;if(datain!=pos8){fill(0,255,0);} 157 if(over && mousePressed){click=true;pos8=0;} 158 if(click){btserial.write("S"+pos8);}}else{fill(2,165,0);} 159 if(ID!=null && ID.equals("S") && datain==0){fill(255,255,255);} 160 ellipse(301,148,20,20);// Elipse en 0 161 162 163 164if(btserial.available()>0) 165 {bufferin=null; 166 bufferin=btserial.readStringUntil('\ 167'); 168 if(bufferin!=null){ 169 println("ID= "+ID+" ID2= "+ID2); 170 if(bufferin.substring(0,1).equals("S")){ //S180 D29 171 ID="S"; 172 data=bufferin.substring(1); 173 float indata=float(data); 174 datain=indata; 175 btserial.clear();} 176 if(bufferin.substring(0,1).equals("D")){ 177 ID2="D"; 178 data2=bufferin.substring(1); 179 float indata2=float(data2); 180 senIn=indata2; 181 println(datain); 182 btserial.clear();} 183 } 184 }} 185 186 187void mouseReleased(){ 188 over=false; 189 click=false; 190 191} 192
Arduino Code
arduino
1#include <Servo.h>; 2//#include <SoftwareSerial.h>; 3float dataout; 4String datain; 5String id; 6int dataID; 7float data; 8int SIR,dist,prom; 9 10//SoftwareSerial BTserial(10,9); 11 12Servo servo1; 13 14void setup() { 15// BTserial.begin(9600); 16 Serial.begin(9600); 17 delay(1000); 18 servo1.attach(20); 19 pinMode(2,OUTPUT);//RED 20 pinMode(3,OUTPUT);//GREEN 21 pinMode(4,OUTPUT);//BLUE 22servo1.write(90); 23digitalWrite(3,HIGH); 24delay(1000); 25digitalWrite(3,LOW); 26 digitalWrite(4,HIGH); 27 delay(50); 28 digitalWrite(4,LOW); 29 dataout=servo1.read(); 30 Serial.print("S"); 31 Serial.println(dataout); 32} 33 34void loop() { 35 prom=0; 36 for(int i=0;i<=10;i++){ 37 SIR=analogRead(A0); 38 dist=pow(10,log10(SIR/1821.2)/-0.65);delay(100); 39 prom+=dist; 40 } 41 prom=prom/11; 42 43 if(prom<30 && prom>20){ 44 digitalWrite(2,HIGH); 45 Serial.print("D"); 46 Serial.println(prom); 47 }else{ 48 digitalWrite(2,LOW); 49 Serial.print("D"); 50 Serial.println(prom); 51 52 } 53 54 55 if(Serial.available()>0) 56 {datain= Serial.readStringUntil('\ 57'); 58 59 id=datain.substring(0,1); 60 datain=datain.substring(1); 61 data=datain.toFloat(); 62 63 if(id=="S"){ 64 servo1.write(data); 65 digitalWrite(4,HIGH); 66 delay(50); 67 digitalWrite(4,LOW); 68 dataout=servo1.read(); 69 Serial.print("S"); 70 Serial.println(dataout); 71 } 72 } 73 } 74 75 76 77
Processing Code
processing
1 import processing.serial.*; 2 3 Serial btserial; 4 5 PShape 6 l , r, flecha; 7 float varin; 8 boolean over; 9 boolean click; 10 float pos0=2, 11 pos1=2, pos2=2, pos3=2, pos4=2, pos5=2, pos6=2, pos7=2, pos8=2; 12 float datain, 13 senIn, datasen; 14 String bufferin,ID="uno", ID2="dos",data,data2; 15 16 17 18void setup(){ 19 btserial=new Serial(this,Serial.list()[1],9600); 20 21 22 printArray(Serial.list()); 23 background(20); 24 size(600,200,P2D); 25 smooth(); 26 27 28 29 30 31} 32 33void draw(){ 34 35 textAlign(LEFT); 36 fill(255); 37 38 text("Arduino + HC05",10,20); 39 40 41 textAlign(CENTER); 42 text("Led",100,140); 43 44 text("Servo",400,165); 45 text("SHARP IR",230,35); 46 text("Distancia:",230,80); 47 48 fill(0); 49 rectMode(CENTER); 50 rect(230,94,58,18); 51 fill(255); 52if(senIn>80){text("F.D.R",230,100);}else{ 53 54 text(int(senIn),230,100);} 55 56 image(img1,430,-10,200,60); 57 image(img2,0,160); 58 59 image(img3,448,170,150,33); 60 61 // LED 62 stroke(126); 63 fill(200); 64 65 ellipseMode(CENTER); 66 if(mouseX>50 && mouseX<150 && mouseY>50 && mouseY<150) 67 68 {fill(255); 69 } 70 71 if(ID2.equals("D") && senIn>20 && senIn<30){fill(255,0,0);} 72 73 if(ID2.equals("D") && (senIn>=30 || senIn<=20)){fill(200);} 74 ellipse(100,100,50,50); 75 76 77 //Sensor SHARP 78 rectMode(CENTER); 79 fill(126); 80 rect(230,50,60,20); 81 82 rect(230,60,25,7); 83 fill(150); 84 rect(230,50,58,18); 85 fill(0); 86 87 ellipse(210,50,15,15); 88 fill(255,0,0); 89 ellipse(250,50,15,15); 90 91 92 93 //Radar o Movimiento del Servo 94 stroke(0); 95 strokeWeight(3); 96 97 //fill(56,206,56); 98 fill(100); 99 arc(400,150,200,200,-PI,0); 100 //stroke(100,100,100); 101 102 stroke(40); 103 strokeWeight(1); 104 line(400,150,400+(100*(cos(radians(22.5)))),150-(100*(radians(22.5)))); 105 106 line(400,150,400+(100*(cos(PI/4))),150-(100*(sin(PI/4)))); 107 line(400,150,400+(100*(cos(3*PI/8))),150-(100*(sin(3*PI/8)))); 108 109 line(400,150,400+(100*(cos(PI/2))),150-(100*(sin(PI/2)))); 110 line(400,150,400+(100*(cos(5*PI/8))),150-(100*(sin(5*PI/8)))); 111 112 line(400,150,400+(100*(cos(3*PI/4))),150-(100*(sin(3*PI/4)))); 113 line(400,150,400+(100*(cos(7*PI/8))),150-(100*(sin(7*PI/8)))); 114 115 116 117 118 if(mouseX>488 && mouseX<508 && mouseY>138 && mouseY<158) 119 120 {over=true;if(datain!=pos0){fill(0,255,0);} 121 if(over && mousePressed){click=true;pos0=180.00;} 122 123 if(click){btserial.write("S"+pos0);}}else{fill(2,165,0);} 124 if(ID!=null 125 && ID.equals("S") && datain==180){fill(255,255,255);} 126 ellipse(498,148,20,20); 127 //Elipse en 180 128 129 130 if(mouseX>470 && mouseX<500 && mouseY>102 && mouseY<122) 131 132 {over=true;if(datain!=pos1){fill(0,255,0);} 133 if(over && mousePressed){click=true;pos1=157.50;} 134 135 if(click){btserial.write("S"+pos1);}} else{fill(2,165,0);} 136 if(ID!=null 137 && ID.equals("S") && datain==157){fill(255,255,255);} 138 ellipse(490,112,20,20);//Elipse 139 en 157.5 140 141 142 if(mouseX>459 && mouseX<479 && mouseY>69 && mouseY<89) 143 144 {over=true;if(datain!=pos2){fill(0,255,0);} 145 if(over && mousePressed){click=true;pos2=135.000;} 146 147 if(click){btserial.write("S"+pos2);}}else{fill(2,165,0);} 148 if(ID!=null 149 && ID.equals("S") && datain==135){fill(255,255,255);} 150 ellipse(469,79,20,20);//Elipse 151 en 135 152 153 154 if(mouseX>427 && mouseX<447 && mouseY>47 && mouseY<67) 155 156 {over=true;if(datain!=pos3){fill(0,255,0);} 157 if(over && mousePressed){click=true;pos3=112.500;} 158 159 if(click){btserial.write("S"+pos3);}}else{fill(2,165,0);} 160 if(ID!=null 161 && ID.equals("S") && datain==112){fill(255,255,255);} 162 ellipse(437,57,20,20);//Elipse 163 en 112.5 164 fill(2,165,0); 165 166 if(mouseX>390 && mouseX<410 && mouseY>40 167 && mouseY<60) 168 {over=true;if(datain!=pos4){fill(0,255,0);} 169 if(over && mousePressed){click=true;pos4=90.000;} 170 171 if(click){btserial.write("S"+pos4);}}else{fill(2,165,0);} 172 if(ID!=null 173 && ID.equals("S") && datain==90){fill(255,255,255);} 174 ellipse(400,50,20,20);// 175 Elipse en 90 176 177 if(mouseX>351 && mouseX<371 && mouseY>47 && mouseY<67) 178 179 {over=true;if(datain!=pos5){fill(0,255,0);} 180 if(over && mousePressed){click=true;pos5=67.500;} 181 182 if(click){btserial.write("S"+pos5);}}else{fill(2,165,0);} 183 if(ID!=null 184 && ID.equals("S") && datain==67){fill(255,255,255);} 185 ellipse(361,57,20,20);// 186 Elipse en 67.5 187 188 if(mouseX>319 && mouseX<339 && mouseY>69 && mouseY<89) 189 190 {over=true;if(datain!=pos6){fill(0,255,0);} 191 if(over && mousePressed){click=true;pos6=45;} 192 193 if(click){btserial.write("S"+pos6);}}else{fill(2,165,0);} 194 if(ID!=null 195 && ID.equals("S") && datain==45){fill(255,255,255);} 196 ellipse(329,79,20,20); 197 // Elipse en 45 198 199 if(mouseX>300 && mouseX<320 && mouseY>102 && mouseY<122) 200 201 {over=true;if(datain!=pos6){fill(0,255,0);} 202 if(over && mousePressed){click=true;pos7=22.5;} 203 204 if(click){btserial.write("S"+pos7);}}else{fill(2,165,0);} 205 if(ID!=null 206 && ID.equals("S") && datain==22.5){fill(255,255,255);} 207 ellipse(310,112,20,20);// 208 Elipse en 22.5 209 210 211 if(mouseX>291 && mouseX<311 && mouseY>138 && mouseY<158) 212 213 {over=true;if(datain!=pos8){fill(0,255,0);} 214 if(over && mousePressed){click=true;pos8=0;} 215 216 if(click){btserial.write("S"+pos8);}}else{fill(2,165,0);} 217 if(ID!=null 218 && ID.equals("S") && datain==0){fill(255,255,255);} 219 ellipse(301,148,20,20);// 220 Elipse en 0 221 222 223 224if(btserial.available()>0) 225 {bufferin=null; 226 227 bufferin=btserial.readStringUntil('\ 228'); 229 if(bufferin!=null){ 230 println("ID= 231 "+ID+" ID2= "+ID2); 232 if(bufferin.substring(0,1).equals("S")){ //S180 D29 233 234 ID="S"; 235 data=bufferin.substring(1); 236 float indata=float(data); 237 238 datain=indata; 239 btserial.clear();} 240 if(bufferin.substring(0,1).equals("D")){ 241 242 ID2="D"; 243 data2=bufferin.substring(1); 244 float indata2=float(data2); 245 246 senIn=indata2; 247 println(datain); 248 btserial.clear();} 249 } 250 }} 251 252 253 254void mouseReleased(){ 255 over=false; 256 click=false; 257 258} 259
Arduino Code
arduino
1#include <Servo.h>; 2//#include <SoftwareSerial.h>; 3float dataout; 4String 5 datain; 6String id; 7int dataID; 8float data; 9int SIR,dist,prom; 10 11//SoftwareSerial 12 BTserial(10,9); 13 14Servo servo1; 15 16void setup() { 17// BTserial.begin(9600); 18 19 Serial.begin(9600); 20 delay(1000); 21 servo1.attach(20); 22 pinMode(2,OUTPUT);//RED 23 24 pinMode(3,OUTPUT);//GREEN 25 pinMode(4,OUTPUT);//BLUE 26servo1.write(90); 27digitalWrite(3,HIGH); 28delay(1000); 29digitalWrite(3,LOW); 30 31 digitalWrite(4,HIGH); 32 delay(50); 33 digitalWrite(4,LOW); 34 dataout=servo1.read(); 35 36 Serial.print("S"); 37 Serial.println(dataout); 38} 39 40void loop() 41 { 42 prom=0; 43 for(int i=0;i<=10;i++){ 44 SIR=analogRead(A0); 45 dist=pow(10,log10(SIR/1821.2)/-0.65);delay(100); 46 47 prom+=dist; 48 } 49 prom=prom/11; 50 51 if(prom<30 && prom>20){ 52 digitalWrite(2,HIGH); 53 54 Serial.print("D"); 55 Serial.println(prom); 56 }else{ 57 digitalWrite(2,LOW); 58 59 Serial.print("D"); 60 Serial.println(prom); 61 62 } 63 64 65 66 if(Serial.available()>0) 67 {datain= Serial.readStringUntil('\ 68'); 69 70 71 id=datain.substring(0,1); 72 datain=datain.substring(1); 73 data=datain.toFloat(); 74 75 76 if(id=="S"){ 77 servo1.write(data); 78 digitalWrite(4,HIGH); 79 80 delay(50); 81 digitalWrite(4,LOW); 82 dataout=servo1.read(); 83 Serial.print("S"); 84 85 Serial.println(dataout); 86 } 87 } 88 } 89 90 91 92
Downloadable files
Squematic
Squematic
Squematic
Squematic
Comments