Skip to content

Instantly share code, notes, and snippets.

@IsaacGonzalez
Created October 19, 2015 03:28
Show Gist options
  • Save IsaacGonzalez/0f44e520a0d48efa0d2c to your computer and use it in GitHub Desktop.
Save IsaacGonzalez/0f44e520a0d48efa0d2c to your computer and use it in GitHub Desktop.
Código para hacer que un motor de impresora se mueva a las 5 posiciones establecidas mediante un arduino y una serie de push buttons.
#include <NewPing.h>
#define TRIGGER_PIN 8
#define ECHO_PIN 9
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
const int pin_boton_1 = 2;
const int pin_boton_2 = 3;
const int pin_boton_3 = 4;
const int pin_boton_4 = 6;
const int pin_boton_5 = 7;
const int pin_motor_adelante = 10;
const int pin_motor_atras = 11;
const int pin_led = 13;
int boton_1 = 0;
int boton_2 = 0;
int boton_3 = 0;
int boton_4 = 0;
int boton_5 = 0;
int distancia = 0;
void setup() {
pinMode(pin_led, OUTPUT);
pinMode(pin_boton_1, INPUT);
pinMode(pin_boton_2, INPUT);
pinMode(pin_boton_3, INPUT);
pinMode(pin_boton_4, INPUT);
pinMode(pin_boton_5, INPUT);
pinMode(pin_motor_adelante, OUTPUT);
pinMode(pin_motor_atras, OUTPUT);
Serial.begin(9600);
}
void loop() {
boton_1 = digitalRead(pin_boton_1);
boton_2 = digitalRead(pin_boton_2);
boton_3 = digitalRead(pin_boton_3);
boton_4 = digitalRead(pin_boton_4);
boton_5 = digitalRead(pin_boton_5);
// imprimirEstatusBotones();
distancia = getDistanciaEnCentimetros(sonar);
if (boton_1 == HIGH) {
posicionarMotor(distancia, 9, 10);
} else if (boton_2 == HIGH) {
posicionarMotor(distancia, 14, 15);
} else if (boton_3 == HIGH) {
posicionarMotor(distancia, 20, 22);
} else if (boton_4 == HIGH) {
posicionarMotor(distancia, 26, 27);
} else if (boton_5 == HIGH) {
posicionarMotor(distancia, 33, 34);
} else {
detener();
}
delay(10);
}
void adelante(){
digitalWrite(pin_motor_adelante, HIGH);
digitalWrite(pin_motor_atras, LOW);
}
void atras(){
digitalWrite(pin_motor_adelante, LOW);
digitalWrite(pin_motor_atras, HIGH);
}
void detener(){
digitalWrite(pin_motor_adelante, LOW);
digitalWrite(pin_motor_atras, LOW);
}
void imprimirEstatusBotones(){
// Imprime una linea con lo que reciben los botones "Botones: 0 0 0 0 0 "
Serial.print("Botones: ");
Serial.print(boton_1);
Serial.print(" ");
Serial.print(boton_2);
Serial.print(" ");
Serial.print(boton_3);
Serial.print(" ");
Serial.print(boton_4);
Serial.print(" ");
Serial.print(boton_5);
Serial.print(" ");
Serial.println("");
}
int getDistanciaEnCentimetros(NewPing sonar){
int uS = sonar.ping();
return uS / US_ROUNDTRIP_CM;
}
void posicionarMotor(int distancia, int limiteInferior, int limiteSuperior){
if(distancia < limiteInferior){
adelante();
} else if (distancia > limiteSuperior ){
atras();
} else {
detener();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment