Saltar la navegación

Móvil de peces Actualizado

Para actualizar este proyecto se ha dispuesto el uso de un sensor PIR que garantiza la detección del movimiento a una distancia mayor que la conseguida por los ultrasonidos.
Link al sensor PIR de AliExpres

Web Thingiverse desde donde puedes bajarte las piezas para construirlo: Link

captura thingiverse


Primer Código

// he definit una funció que el que fa és llegir la distància.
//una funció per fer un gir
//dóna algun error de lectura a cero però funciona dels sensor

//conexiones ultrasonido
int trigPin = 12; // Trigger
int echoPin = 11;
// variables ultrasonido
long duration, cm;

//conexiones motor
int IN1_motor=2;//28BYJ48 In1
int IN2_motor=3;
int IN3_motor=4;
int IN4_motor=5;
//variables motor
int motorSpeed = 8000; //variable para fijar la velocidad, A MAYOR VALOR GIRO MÁS LENTO
int stepCounter = 0; // contador para los pasos
int stepsPerRev = 4076; // pasos para una vuelta completa ?4076

//secuencia media fase
const int numSteps = 8;
const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };


void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(IN1_motor, OUTPUT);
pinMode(IN2_motor, OUTPUT);
pinMode(IN3_motor, OUTPUT);
pinMode(IN4_motor, OUTPUT);
}

void loop() {
leer_distancia();
if (cm < 15 && cm >0 ){
hacer_un_giro();
}
else{
}

}
void clockwise()
{
stepCounter++;
if (stepCounter >= numSteps) stepCounter = 0;
setOutput(stepCounter);
};

void anticlockwise()
{
stepCounter--;
if (stepCounter < 0) stepCounter = numSteps - 1;
setOutput(stepCounter);
}


void setOutput(int step)
{
digitalWrite(IN1_motor, bitRead(stepsLookup[step], 0));
digitalWrite(IN2_motor, bitRead(stepsLookup[step], 1));
digitalWrite(IN3_motor, bitRead(stepsLookup[step], 2));
digitalWrite(IN4_motor, bitRead(stepsLookup[step], 3));
};

void hacer_un_giro(){
for (int i = 0; i < stepsPerRev ; i++)
{
clockwise();
delayMicroseconds(motorSpeed);
//delay(1000);
}
/*for (int i = 0; i < stepsPerRev ; i++)
{
anticlockwise();
delayMicroseconds(motorSpeed);
//delay(100);
}*/
delay(10);


}

void leer_distancia(){
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
//inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135

//Serial.print(inches);
//Serial.print("in, ");
Serial.print(cm);// valor en cm
Serial.print("cm");
Serial.println();
delay(100);

}

segundo código

Variable numero de giros

//Funcionament: en detectar alguna cosa a menys de 15 cm fa que el motor faci un recorregut determinat, en aquest cas una volta, encara que ens allunyem.
// he definit una funció que el que fa és llegir la distància.
//una funció per fer un gir,(es pot variar el nombre de girs amb la variable giros, admet decimals) en funció de dues funcions clockwise i una anticlockwise
//el sensor dóna algun error de lectura a cero, s'han eliminat els efectes afegint al if cm>0, d'aquesta manera funciona correctament

//conexiones ultrasonido
int trigPin = 12; // Trigger
int echoPin = 11;
// variables ultrasonido
long duration, cm;
float giros=0.2;

//conexiones motor
int IN1_motor=2;//28BYJ48 In1
int IN2_motor=3;
int IN3_motor=4;
int IN4_motor=5;
//variables motor
int motorSpeed = 8000; //variable para fijar la velocidad, A MAYOR VALOR GIRO MÁS LENTO
int stepCounter = 0; // contador para los pasos
int stepsPerRev = 4076; // pasos para una vuelta completa ?4076

//secuencia media fase
const int numSteps = 8;
const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };


void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(IN1_motor, OUTPUT);
pinMode(IN2_motor, OUTPUT);
pinMode(IN3_motor, OUTPUT);
pinMode(IN4_motor, OUTPUT);
}

void loop() {
leer_distancia();
if (cm < 15 && cm >0 ){
hacer_un_giro();
}
else{
}

}
void clockwise()
{
stepCounter++;
if (stepCounter >= numSteps) stepCounter = 0;
setOutput(stepCounter);
};

void anticlockwise()
{
stepCounter--;
if (stepCounter < 0) stepCounter = numSteps - 1;
setOutput(stepCounter);
}


void setOutput(int step)
{
digitalWrite(IN1_motor, bitRead(stepsLookup[step], 0));
digitalWrite(IN2_motor, bitRead(stepsLookup[step], 1));
digitalWrite(IN3_motor, bitRead(stepsLookup[step], 2));
digitalWrite(IN4_motor, bitRead(stepsLookup[step], 3));
};

void hacer_un_giro(){
for (int i = 0; i < stepsPerRev*giros ; i++)
{
clockwise();
delayMicroseconds(motorSpeed);
//delay(1000);
}
/*for (int i = 0; i < stepsPerRev*giros ; i++)
{
anticlockwise();
delayMicroseconds(motorSpeed);
//delay(100);
}*/
delay(10);


}

void leer_distancia(){
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
//inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135

//Serial.print(inches);
//Serial.print("in, ");
Serial.print(cm);// valor en cm
Serial.print("cm");
Serial.println();
delay(100);

}