Está en la página 1de 4

19/12/2020

File "/control_lapiz/main.cpp" printed from os.mbed.com on 19/12/2020

1 #include "mbed.h"
2 #include "Adafruit_SSD1306.h"
3 #include "I2C_Call.h"
4
5 PwmOut servo1(PTB0);
6 PwmOut servo2(PTB1);
7 InterruptIn b1(PTD7);
8 BusOut motor(PTE20, PTE21, PTE22, PTE23);
9 Serial pc(USBTX, USBRX); //PRIMERO ES TX , EL SEGUNDO ES RX, para modulo
10
11 I2C gI2C(PTC2,PTC1);
12 Adafruit_SSD1306_I2c gOled2(gI2C,PTE20);
13
14 char v;
15 char a[7];
16 int s;
17
18 uint16_t posicion=0;
19 int x=0,p1=0;
20 int aux6=0;
21 int m=0;
22
23 int bandera=0;
24
25 int movi=0;
26
27 int u=0,u1=0,d=0,d1=0,c=0,c1=0;
28 int u2=0,u3=0,d2=0,d3=0,c2=0,c3=0;
29 uint16_t valor;
30 uint16_t valor1;
31
32 uint16_t grados;
33 uint16_t grados1;
34
35 Timer t;
36 float c_time=0;
37 Timer k;
38 float k_time=0;
39 float aux=0;
40 float aux1=24000;
41 float aux2=0;
42 uint16_t valorreal=0;
43 uint16_t valoraux=0;
44
45
46 void recibir(){
47 v=pc.getc();
48 a[s]=v;
49 s++;
50 aux1=24000;
51 }
52
53 void accion(){//esto es del segundo programa pero como funcion
54
55 if(a[6]=='A'){
56 /*gOled2.setTextCursor(25,40);
57 gOled2.printf("hola");
58 gOled2.display();*/
59 u=a[2];
60 u1=u-48;
61 if (a[1]==' '){
62 d1=0;
63 }
64 else{
65 d=a[1];
66 d1=d-48;
67 }
68 if (a[0]==' '){
69 c1=0;
70 }
71 else{
72 c=a[0];
73 c1=c-48;
74 }
75 valor=(u1+(d1*10)+(c1*100));
76 u2=a[5];
77 u3=u2-48;
78 if (a[4]==' '){
79 d3=0;
80 }
81 else{
82 d2=a[4];
83 d3=d2-48;
84 }
85 if (a[3]==' '){
86 c3=0;
87 }
88 else{
89 c2=a[3];
90 c3=c2-48;
91 }
92 valor1=(u3+(d3*10)+(c3*100));
93 s=0;
94 }
95

1/4
19/12/2020
96
97 }
98
99 void velocidad (){
100 accion();
101 t.start();
102 c_time=t.read();
103 aux=24000/5;
104 if (c_time>=1){
105 aux1=aux1-aux;
106 t.reset();
107
108 }
109 /*if (aux1<=3000){
110 aux1=3000;
111 wait(1);
112 m=1;
113 }*/
114 }
115
116
117
118
119
120 void pos(){
121 bandera=2;
122 }
123
124 void angulo(){
125 accion();
126 grados=(valor*11.111)+500;
127 grados1=3000-grados;
128 if (grados==500){
129 servo1.pulsewidth_us(2500);
130 }
131
132 servo1.pulsewidth_us(grados1);
133
134
135 servo2.pulsewidth_us(grados);
136 }
137
138 int main() {
139 gOled2.begin();
140 gOled2.clearDisplay();
141 gOled2.setTextCursor(25,20);
142 gOled2.printf("proyecto 6\n\r");
143 gOled2.display();
144 pc.baud(9600);
145 pc.attach(recibir);
146 b1.fall(&pos);
147 servo1.period_ms(20);
148 servo2.period_ms(20);
149 while(1) {
150 switch (bandera) {
151 case 0 :
152
153 //servo1.pulsewidth_us(2);
154 for (x=0; x<20; x++){
155 motor = (0xA); //1010 9
156 wait_us(8000);
157 motor = (0x6);//0110 5
158 wait_us(8000);
159 motor = (0x5);//0101 6
160 wait_us(8000);
161 motor = (0x9);//1001 a
162 wait_us(8000);
163 }
164 wait(0.5);
165 bandera=1;
166
167 break;
168
169 case 1 :
170 for (x=0; x<20; x++){
171 motor = (0x9); //1010 9
172 wait_us(8000);
173 motor = (0x5);//0110 5
174 wait_us(8000);
175 motor = (0x6);//0101 6
176 wait_us(8000);
177 motor = (0xA);//1001 a
178 wait_us(8000);
179 }
180 //accion();
181 //angulo();
182 servo1.pulsewidth_us(2500);
183 servo2.pulsewidth_us(500);
184
185 break;
186
187 case 2 :
188 //myled2=1;
189 wait(1);
190 motor = (0x0);
191
192 /*a[6]='0';
193 a[7]='0';

2/4
19/12/2020
194 a[8]='0';*/
195 //valor2=0;
196
197 /*for (s=0; s<=9; s++) { // The less-than-or-equal-to (<=) should be replaced with less-than (<)
198 s = 0;} */
199 gOled2.setTextCursor(25,40);
200 gOled2.printf("%d",bandera);
201 gOled2.display();
202 wait_ms(500);
203 bandera=3;
204
205 break;
206
207 case 3 :
208 //myled2=0;
209 accion();
210 angulo();
211
212
213 if (valor1!=0){
214
215 if (valor1>valorreal){
216 movi=valor1-valorreal;
217 bandera=4;
218 }
219
220 else if (valor1<valorreal){
221 movi=valorreal-valor1;
222 bandera=5;
223 }
224 else{
225 bandera=2;
226 }
227
228 }
229 gOled2.setTextCursor(25,40);
230 gOled2.printf("%d",bandera);
231 gOled2.display();
232 aux2=15200;
233 pc.printf("%5.2f\r",aux2);
234 wait_ms(500);
235 break;
236
237
238 case 4 :
239 //myled = 0;
240 posicion=movi*6;
241 valoraux=valor1;
242 valorreal=movi+valorreal;
243 wait_us(800);
244
245
246 for (x=0; x<posicion; x++){
247 velocidad();
248 aux2=aux1;
249 pc.printf("%5.2f\r",aux2);
250 motor = (0xA); //1010
251 wait_us(aux2);
252 motor = (0x6);//0110
253 wait_us(aux2);
254 motor = (0x5);//0101
255 wait_us(aux2);
256 motor = (0x9);//1001
257 wait_us(aux2);
258 }
259 gOled2.setTextCursor(25,40);
260 gOled2.printf("%d",bandera);
261 gOled2.display();
262 wait_ms(500);
263 bandera=2;
264
265 break;
266
267 case 5 :
268 //myled = 0;
269 posicion=movi*6;
270 //valoraux=valor2;
271 valorreal=valorreal-movi;
272 wait_us(800);
273
274
275 for (x=0; x<posicion; x++){
276 velocidad();
277 aux2=aux1;
278 pc.printf("%5.2f\r",aux2);
279 motor = (0x9); //10109
280 wait_us(aux2);
281 motor = (0x5);//01105
282 wait_us(aux2);
283 motor = (0x6);//01016
284 wait_us(aux2);
285 motor = (0xA);//1001a
286 wait_us(aux2);
287 }
288 gOled2.setTextCursor(25,40);
289 gOled2.printf("%d",bandera);
290 gOled2.display();
291 wait_ms(500);

3/4
19/12/2020
292 bandera=2;
293
294 break;
295 }
296
297
298 /*
299 accion();
300 if (valor2!=0 ){
301 posicion=valor2*6;
302 for (x=0; x<posicion; x++){
303 velocidad();
304 aux2=aux1;
305 //pc.printf("%5.2f\r",aux2);
306 motor = (0xA); //1010
307 wait_us(aux2);
308 motor = (0x6);//0110
309 wait_us(aux2);
310 motor = (0x5);//0101
311 wait_us(aux2);
312 motor = (0x9);//1001
313 wait_us(aux2);
314
315 }
316 if (x>=posicion){
317 valoraux=valor2;
318
319 wait_ms(200);
320 aux6=1;
321 }
322 }
323 if (aux6==1){
324 motor=(0x0);
325 a[6]='0';
326 a[7]='0';
327 a[8]='0';
328 gOled2.setTextCursor(25,40);
329 gOled2.printf("%d",valoraux);
330 gOled2.display();
331 //accion();
332 }
333
334
335 */
336 }
337 }

File "/control_lapiz/main.cpp" printed from os.mbed.com on 19/12/2020

4/4

También podría gustarte