Skip to content

Instantly share code, notes, and snippets.

@eljojo
Created April 13, 2014 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eljojo/10600864 to your computer and use it in GitHub Desktop.
Save eljojo/10600864 to your computer and use it in GitHub Desktop.
small program to teach my brother custom delays in arduino, using loops and measuring distance
int rojor = 2;
int blancof = 4;
int valiza = 5;
int parlante = 8;
int cuenta = 0;
int distancia = 0;
int estado = 0; // cuando estado == 1, esta prendido, == 0, esta apagado
//----------------------ARDUMOTO-------------------------//
#define derecha 0 //
#define izquierda 1 //
#define MOTOR_A 0 //
#define MOTOR_B 1 //
const byte PWMA = 3; // PWM control (speed) for motor A //
const byte PWMB = 11; // PWM control (speed) for motor B //
const byte DIRA = 12; // Direction control for motor A //
const byte DIRB = 13; // Direction control for motor B //
//---------------------ULTRASONICO-----------------------//
int echo = 6; //
int trig = 7; //
//-------------------------------------------------------//
void setup() {
// CONFIGURAR ARDUMOTO
// configurar pins ardumoto como SALIDA
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
// apagar pines de ardumoto
digitalWrite(PWMA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, LOW);
// CONFIGURAR LEDS
pinMode(rojor, OUTPUT);
pinMode(blancof, OUTPUT);
pinMode(valiza, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
// CONFIGURAR VALIZA
digitalWrite(valiza, HIGH);
}
void loop() {
int botonEncendido = analogRead(A0);
int botonApagado = analogRead(A1);
if (botonEncendido >= 955){
cuenta = cuenta + 1;
if (cuenta >= 5){
estado = 1; // ACTIVAMOS EL AUTO
}
}
if (botonApagado >= 10){
cuenta = 0;
distancia = 0;
estado = 0;
}
ardumoto();
delay(1000);
}
int resultadoDelay = 0;
void ardumoto() {
// driveArdumoto(MOTOR_(A-B), direccion, velocidad(0-255)
driveArdumoto(MOTOR_B, izquierda, 127);
//se define un loop para el tiempo que gire el motor
resultadoDelay = delayArdumoto(1000);
if(resultadoDelay == 1) {
stopArdumoto(MOTOR_B);
return; // cancelamos el resto de la funcion ardumoto
}
// detener motor B
stopArdumoto(MOTOR_B);
// esperar 1 segundo
delay(1000);
// girar a la derecha
driveArdumoto(MOTOR_B, derecha, 255);
resultadoDelay = delayArdumoto(1000);
if(resultadoDelay == 1) {
stopArdumoto(MOTOR_B);
return; // cancelamos el resto de la funcion ardumoto
}
stopArdumoto(MOTOR_B);
}
int delayArdumoto(int milisegundos) {
// si ejecuto delayArdumoto(1000)
// entonces dentro de delayArdumoto, milisegundos = 1000;
int vueltas = milisegundos / 50; // si milisegundos es 1000, vueltas = 20
for(int vuelta = 0; vuelta < vueltas; vuelta = vuelta + 1) {
if(ultrasonico() < 20) { // 20 centimetros
return(1);
}
delay(50);
}
return(0);
}
void driveArdumoto(byte motor, byte dir, byte spd){ //
if (motor == MOTOR_A){ //
digitalWrite(DIRA, dir); //
analogWrite(PWMA, spd); //
} //
else if (motor == MOTOR_B){ //
digitalWrite(DIRB, dir); //
analogWrite(PWMB, spd); //
} //
} //
void stopArdumoto(byte motor){ //
driveArdumoto(motor, 0, 0); //
} //
//--------------------------ULTRASONICO--------------------------------//
int ultrasonico(){ //
long duration, distance; //
digitalWrite(trig, LOW); // Added this line //
delayMicroseconds(2); // Added this line //
digitalWrite(trig, HIGH); //
//delayMicroseconds(1000); - Removed this line //
delayMicroseconds(10); // Added this line //
digitalWrite(trig, LOW); //
duration = pulseIn(echo, HIGH); //
distance = (duration/2) / 29.1; //
if (distance >= 200 || distance <= 0){ //
Serial.println("Out of range"); //
}else{ //
Serial.print(distance); //
Serial.println(" cm"); //
} //
return(distance);
} //
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment