Bubulindo preciso da sua ajuda, estou tentando fazer um robozinho com um sistema anti colisão, estou usando um Grove ultrasonic, LED RGB, e um mini servo para girar o sensor, até agora consegui fazer o robô andar sozinho, identificar objetos e parar, claro que no ângulo certo de 30 graus frente ao sensor fora isso ele colide, mas enfim, o que eu não consigo fazer é virar o sensor para esquerda armazenar a distância, depois girar para direita armazenar distância novamente e após isso compará-las e depois escolher qual o caminho mais livre a seguir.
Segue um video do meu robozinho para melhor entendimento =>
O código é um pouco grande, foi o jeito que eu consegui fazer até agora que na verdade ele não compara as distâncias somente segue em frente caso a distância seja maior que 30cm. Tenho uma outra variação do programa onde eu tento comparar a distância mas funciona na pratica pior que este código, no final colocarei ele como uma referência.
Me desculpe pela bagunça, mas eu sou atrapalhado mesmo, muitas coisas para fazer, estudar, trabalhar meu filho etc, as vezes preciso ser um pouco prático hehehe
O código abaixo funciona razoavelmente bem, entretanto ele não é dotado de inteligencia alguma.
#include "Ultrasonic.h"
#include <AFMotor.h>
#include <Servo.h>
#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long int
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
// connect to digital 0,1
int Clkpin = 0;
int Datapin = 1;
void ClkProduce(void)
{
digitalWrite(Clkpin, LOW);
delayMicroseconds(20);
digitalWrite(Clkpin, HIGH);
delayMicroseconds(20);
}
void Send32Zero(void)
{
unsigned char i;
for (i=0; i<32; i++)
{
digitalWrite(Datapin, LOW);
ClkProduce();
}
}
uint8 TakeAntiCode(uint8 dat)
{
uint8 tmp = 0;
if ((dat & 0x80) == 0)
{
tmp |= 0x02;
}
if ((dat & 0x40) == 0)
{
tmp |= 0x01;
}
return tmp;
}
// gray data
void DatSend(uint32 dx)
{
uint8 i;
for (i=0; i<32; i++)
{
if ((dx & 0x80000000) != 0)
{
digitalWrite(Datapin, HIGH);
}
else
{
digitalWrite(Datapin, LOW);
}
dx <<= 1;
ClkProduce();
}
}
// data processing
void DataDealWithAndSend(uint8 r, uint8 g, uint8 b)
{
uint32 dx = 0;
dx |= (uint32)0x03 << 30; // highest two bits 1,flag bits
dx |= (uint32)TakeAntiCode(b) << 28;
dx |= (uint32)TakeAntiCode(g) << 26;
dx |= (uint32)TakeAntiCode(r) << 24;
dx |= (uint32)b << 16;
dx |= (uint32)g << 8;
dx |= r;
DatSend(dx);
}
Ultrasonic ultrasonic(2);
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 15; // variable to store the servo position ;
void setup() {
pinMode(Datapin, OUTPUT);
pinMode(Clkpin, OUTPUT);
//Serial.begin(9600);
myservo.attach(10); // attaches the servo on pin 10 to the servo object
motor1.setSpeed(165); // set the speed to 200/255
motor2.setSpeed(255); // set the speed to 200/255
myservo.write(90);
}
void loop() {
// int i;
// Yellow();
myservo.write(90);
ultrasonic.MeasureInCentimeters();
if (ultrasonic.RangeInCentimeters >= 40)
{
Green();
forward();
}
else if (ultrasonic.RangeInCentimeters < 35 && ultrasonic.RangeInCentimeters >20){
Yellow();
compare();
}
if (ultrasonic.RangeInCentimeters < 15 && ultrasonic.RangeInCentimeters > 0){
Red();
stop();
backward();
}
}
//-------------------------------------------------------------------------------
//Funçao motor
void forward()
{
motor1.run(FORWARD);
motor2.run(FORWARD); // turn it on going forward
}
void backward()//
{
motor1.run(BACKWARD);
motor2.run(BACKWARD); // the other way
}
void right()//
{
motor1.setSpeed(255);
motor2.setSpeed(255);
motor1.run(BACKWARD);
motor2.run(FORWARD);
// delay(500);
}
void left()//
{
motor2.setSpeed(255);
motor1.setSpeed(255);
motor2.run(BACKWARD);
motor1.run(FORWARD);
// delay(500);
}
void stop()//
{
motor1.run(RELEASE);
motor2.run(RELEASE); // stopped
}
//----------------------------------------------------------------------------
//FUNÇAO DO LED
void Red (){
// {
Send32Zero(); // begin vermelho /azul
DataDealWithAndSend(255, 0, 0); // first node data
DataDealWithAndSend(255, 255, 0); // second node data
// DataDealWithAndSend(0, 255, 255); // third node data
Send32Zero(); // send to update data
}
// delay(1500);
void Yellow (){
Send32Zero();
DataDealWithAndSend(255, 255, 0); // azul / verde
DataDealWithAndSend(0, 255, 0);
// DataDealWithAndSend(255, 0, 255);
Send32Zero();
// delay(1500);
}
void Green (){
Send32Zero();
DataDealWithAndSend(0, 255, 0); //verde /vermelho
DataDealWithAndSend(255, 0, 0);
// DataDealWithAndSend(255,255,0);
Send32Zero();
// delay(1500);
}
//--------------------------------------------------------------------------------------------
// FUNÇAO SERVO SENSOR
void compare (){
myservo.write(50);
stop();
delay(500);
if (ultrasonic.RangeInCentimeters > 30){
right();
delay(550);}
else
myservo.write(140);
stop();
delay(500);
myservo.write(140);
left();
delay(550);
}
O código abaixo é apenas uma variação do primeiro código que postei.
void loop() {
myservo.write(90);
ultrasonic.MeasureInCentimeters(); // call the ultrasonic proccess
if (ultrasonic.RangeInCentimeters >= 40) //move forward
{
Green(); // set the RGB LED green
forward();
}
else if (ultrasonic.RangeInCentimeters < 35 && ultrasonic.RangeInCentimeters >20){ //if have something between this ranges
Yellow();
myservo.write(50); // look right
stop(); // stop both motors
Rdist = ultrasonic.RangeInCentimeters; //set the distance on int Rdist
delay(1000);
myservo.write(140); // look left
Ldist = ultrasonic.RangeInCentimeters; //set the distance on int Ldist
stop();
delay(1000);
compare(); // compare distances
}
if (ultrasonic.RangeInCentimeters < 15 && ultrasonic.RangeInCentimeters > 0){ // stop the car
Red(); // LED turn red
stop();
backward(); // Move backward
}
}
void compare (){
if (Rdist > Ldist){
right();
delay(1000);}
else if (Ldist > Rdist) {
left();
delay(1000);}
else {
stop();
backward();
}
}