Skip to content

Commit 4115fbb

Browse files
committed
Codigos robotica | carrinho arduino
1 parent 167a973 commit 4115fbb

File tree

6 files changed

+206
-154
lines changed

6 files changed

+206
-154
lines changed

carro/carro/Ultrasonic.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Ultrasonic.h - Library for HC-SR04 Ultrasonic Sensing Module.
3+
*
4+
* With ideas from:
5+
* Created by ITead studio. Alex, Apr 20, 2010.
6+
* iteadstudio.com
7+
*
8+
* SVN Keywords
9+
* ----------------------------------
10+
* $Author: cnobile $
11+
* $Date: 2011-12-07 21:49:14 -0500 (Wed, 07 Dec 2011) $
12+
* $Revision: 38 $
13+
* ----------------------------------
14+
*
15+
* Thank you to Rowan Simms for pointing out the change in header name with
16+
* Arduino version 1.0 and up.
17+
*
18+
*/
19+
20+
#ifndef ULTRASONIC_H
21+
#define ULTRASONIC_H
22+
23+
#include <stddef.h>
24+
25+
#if defined(ARDUINO) && ARDUINO >= 100
26+
#include <Arduino.h>
27+
#else
28+
#include <WProgram.h>
29+
#endif
30+
31+
// Undefine COMPILE_STD_DEV if you don't want Standard Deviation.
32+
#define COMPILE_STD_DEV
33+
34+
35+
typedef struct bufferCtl
36+
{
37+
float *pBegin;
38+
float *pIndex;
39+
size_t length;
40+
bool filled;
41+
} BufCtl;
42+
43+
class Ultrasonic
44+
{
45+
public:
46+
Ultrasonic(int tp, int ep);
47+
long timing();
48+
float convert(long microsec, int metric);
49+
void setDivisor(float value, int metric);
50+
static const int IN = 0;
51+
static const int CM = 1;
52+
53+
#ifdef COMPILE_STD_DEV
54+
bool sampleCreate(size_t size, ...);
55+
void sampleClear();
56+
float unbiasedStdDev(float value, size_t bufNum);
57+
#endif // COMPILE_STD_DEV
58+
59+
private:
60+
int _trigPin;
61+
int _echoPin;
62+
float _cmDivisor;
63+
float _inDivisor;
64+
65+
#ifdef COMPILE_STD_DEV
66+
size_t _numBufs;
67+
BufCtl *_pBuffers;
68+
void _sampleUpdate(BufCtl *buf, float msec);
69+
void _freeBuffers();
70+
#endif // COMPILE_STD_DEV
71+
};
72+
73+
#endif // ULTRASONIC_H

carro/carro/carro.ino

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <AFMotor.h>
2+
#include <Ultrasonic.h>
3+
4+
#define DISTANCIA_DESVIAR 30.0 //cm
5+
#define DELAY_LEITURA 250 //ms
6+
#define pino_trigger 13
7+
#define pino_echo 2
8+
9+
AF_DCMotor motor(4);
10+
AF_DCMotor eixo(1);
11+
12+
Ultrasonic ultrasonic(pino_trigger, pino_echo);
13+
14+
int quantidadeVerificacao = 0;
15+
16+
void setup() {
17+
eixo.setSpeed(255);
18+
motor.setSpeed(255);
19+
VirarDireita();
20+
delay(500);
21+
VirarEsquerda();
22+
delay(500);
23+
Frente();
24+
delay(500);
25+
MotorParaFrente();
26+
Frente();
27+
}
28+
29+
void loop() {
30+
VerificarObjeto();
31+
delay(DELAY_LEITURA);
32+
}
33+
34+
void VerificarObjeto(){
35+
MotorParaFrente();
36+
37+
if(CalcularDistanciaSensor() <= DISTANCIA_DESVIAR){
38+
switch(quantidadeVerificacao){
39+
case 0:
40+
MotorParaAtras();
41+
delay(100);
42+
//DesligarMotor();
43+
VirarEsquerda();
44+
MotorParaFrente();
45+
delay(2000);
46+
break;
47+
case 1:
48+
MotorParaAtras();
49+
delay(2000);
50+
DesligarMotor();
51+
delay(500);
52+
VirarDireita();
53+
MotorParaFrente();
54+
delay(2000);
55+
break;
56+
case 2:
57+
MotorParaAtras();
58+
VirarEsquerda();
59+
delay(2000);
60+
MotorParaFrente();
61+
Frente();
62+
delay(500);
63+
break;
64+
default:
65+
quantidadeVerificacao = 0;
66+
VerificarObjeto();
67+
}
68+
69+
DesligarMotor();
70+
//servo.attach(frente);
71+
quantidadeVerificacao++;
72+
73+
VerificarObjeto();
74+
}else{
75+
quantidadeVerificacao = 0;
76+
MotorParaFrente();
77+
Frente();
78+
}
79+
}
80+
void DesligarMotor(){
81+
motor.run(RELEASE);
82+
}
83+
84+
void MotorParaFrente(){
85+
motor.run(FORWARD);
86+
}
87+
88+
void MotorParaAtras(){
89+
motor.run(BACKWARD);
90+
}
91+
92+
void VirarDireita(){
93+
eixo.run(FORWARD);
94+
}
95+
96+
void VirarEsquerda(){
97+
eixo.run(BACKWARD);
98+
}
99+
100+
void Frente(){
101+
eixo.run(RELEASE);
102+
}
103+
104+
float CalcularDistanciaSensor(){
105+
long microsec = ultrasonic.timing();
106+
return ultrasonic.convert(microsec, Ultrasonic::CM);
107+
}

motor/motor.ino

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
#include <AFMotor.h>
2+
#include <Servo.h>
3+
#include <Ultrasonic.h>
4+
5+
Servo servo;
6+
//AF_DCMotor motor(1);
7+
//AF_DCMotor motor2(2);
8+
9+
#define pino_trigger 13
10+
#define pino_echo 2
11+
12+
//Ultrasonic ultrasonic(pino_trigger, pino_echo);
13+
14+
int frente = 93;
15+
int esquerda = 0;
16+
int direita = 180;
17+
int acelerar = 150;
18+
int i = 0;
19+
20+
long distanciaDesviar = 25.0;
21+
122
void setup() {
2-
pinMode(13, OUTPUT);
23+
Serial.begin(9600);
24+
servo.attach(10);
25+
servo.write(0);
26+
327
}
428

529
void loop() {
6-
// put your main code here, to run repeatedly:
7-
digitalWrite(13, HIGH);
8-
30+
servo.write(direita);
931
}

servo/servo/Keyboard.h

-149
This file was deleted.

servo/servo/code.png

280 KB
Loading

servo/servo/servo.ino

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Servo servo;
44
int posicao = 90;
55
char caractere;
66

7-
87
void setup() {
98
pinMode(13, OUTPUT);
109
servo.attach(8);

0 commit comments

Comments
 (0)