Вы находитесь на странице: 1из 6

/*** Autor: Alfredo Prades aprades gmail.

com Control de un servo mendiante envo de caracteres a travs de un puerto serie bluetooth La posicion del servo se controla manualmente mediante una de las entradas analogicas del arduino, en este caso "turnPostionPinIN" . Mediante dos seales digitales de salida del arduino, se controlan las puertas (gate) de los transistores del puente H que controla el motor que mueve el "servo" (motor) de la direccin de las ruedas. turnLeftPinOUT turnRightPinOUT Con seales digitales a los pines forwardPinOUT del puente H del motor princiapl El movimiento que queremos hacer se recibe por el puerto serie mediante caracteres, que indican hacia que posicion nos queremos mover. En nuestro caso el puerto serie esta conectado a un mdulo bluetooth el cual recibe los commandos (caracteres) desde un telefono android con un app de puerto serie bluetooth. En nuestro caso usamos el app Bluetooth serial controller disponible en el playstore. backwardPinOUT controlamos los "gates"

TODO: Se podria usar una variable target direction para transmittir directamente por serie el valor deseado de giro, de momento nos desplazaremos a izq, der, centro TODO: Buscar un app que use el acelerometro del movil para trasmitir direcciones y/o velocidad.

**/ #include <Arduino.h> const int turnPostionPinIN = 0; //VERDER : GND const int turnLeftPinOUT = 5; const int turnRightPinOUT = 3; const int forwardPinOUT = 9; const int backwardPinOUT = 10; //DOWN //UP -- Amarillo: 5V -- Blanco: ref

//naranja //NEGRO

const int marginalDirection = 30; // Margen al cual si nos acercamos suficiente dejamos de alimentar el servo

char command; // variable to receive data from the serial port const char CMD_RIGHT = 'R'; const char CMD_LEFT = 'L'; const char CMD_CENTER = 'S'; const char CMD_FORWARD = 'F'; const char CMD_BACKWARD = 'B'; const int POS_MAX_RIGHT = 1024; const int POS_CENTER = POS_MAX_RIGHT / 2; const int POS_MIN_LEFT = 0; const int MAX_SPEED = 255; const int SPEED_STEP = 16; int currentSpeed = 0; int safetyTime = 7000; long timeMark,timeMark2; //Speed goes from -255 to 255

void setup() { pinMode(turnLeftPinOUT,OUTPUT); pinMode(turnRightPinOUT,OUTPUT); pinMode(11, OUTPUT); analogWrite(11,130); Serial.begin(9600); // start serial communication at 9600bps Serial.println("OK __________________________ START"); analogWrite( forwardPinOUT , 0 ); timeMark = millis(); } void loop() { //Parpadear cada byte recibido if( Serial.available() ) // if data is available to read { command = Serial.read(); //Serial.print( command ); debuggin purposes // read it and store it in 'val' // echo the command back for

Serial.print( getTurnPosition ( turnPostionPinIN ) ); timeMark2 = timeMark; timeMark = millis(); if( command == CMD_RIGHT ) { } else } else moveRight(); if ( command == CMD_LEFT ){ moveLeft(); if ( command == CMD_CENTER ){ moveCenter(); stop(); } else if ( command == CMD_FORWARD ) { accelerate(); } else if ( command == CMD_BACKWARD ) { decelerate(); } else { timeMark = timeMark2; } } //Si no llega ningun comando vamos a la posicion central si no estamos ya ah //O NO hacemos nada ? else { if ( inCenter( ) != 1 ) { //moveCenter(); } stopTurn();

if ( timeMark + safetyTime < millis() ) { slowDown(); } } //Serial.println(currentSpeed); delay(25); } void stop () { currentSpeed = 0; setSpeed ( currentSpeed ); }

void accelerate () { if ( currentSpeed < MAX_SPEED ) { currentSpeed = currentSpeed + SPEED_STEP;

} setSpeed ( currentSpeed ); } void decelerate() { if ( currentSpeed > -MAX_SPEED ) { currentSpeed = currentSpeed - SPEED_STEP;

} setSpeed ( currentSpeed ); }

void slowDown(){ if (currentSpeed > 0 ) { currentSpeed -= 1; } else { currentSpeed += 1; } setSpeed(currentSpeed); //Serial.println(currentSpeed); } void setSpeed(int speed ) { if (speed > MAX_SPEED ) speed = MAX_SPEED; if (speed < -MAX_SPEED ) speed = -MAX_SPEED;

//Serial.println(speed); if ( speed > SPEED_STEP ) { digitalWrite( backwardPinOUT , LOW ); analogWrite( forwardPinOUT , speed ); } else if (speed < -SPEED_STEP ) { digitalWrite( forwardPinOUT , LOW ); analogWrite( backwardPinOUT , -speed ); } else { digitalWrite( forwardPinOUT , LOW ); digitalWrite( backwardPinOUT , LOW ); } } void moveRight( ) { if ( ! isRightMost () ){ right(); }

} void moveLeft ( ) { if ( ! isLeftMost() ) { left(); } } void moveCenter () {

Serial.println("CENTER "); if ( ! inCenter () ){ if ( inLeftSide() ) { right(); Serial.println("right "); } else{ left(); Serial.println("left "); } } } int inCenter () { if ( getTurnPosition ( turnPostionPinIN ) - marginalDirection < POS_CENTER && getTurnPosition ( turnPostionPinIN ) + marginalDirection > POS_CENTER ) { return 1; } else { return 0; } }

int isRightMost () { if ( getTurnPosition ( turnPostionPinIN ) + marginalDirection > POS_MAX_RIGHT ) { return 1; } else { return 0; } }

int isLeftMost () { if ( getTurnPosition ( turnPostionPinIN ) - marginalDirection < POS_MIN_LEFT return 1; } ) {

else { return 0; } } //Posicion actual del servo ( 0 - MAX_RIGHT) int getTurnPosition (int pin ) { int raw =analogRead( pin ); int directionServoReading = map ( raw , 260 , 870 ,POS_MIN_LEFT , POS_MAX_RIGHT) ; Serial.print("RAW: " ); Serial.print(raw); Serial.print(" Pos:"); Serial.println(directionServoReading); return directionServoReading; }

int inLeftSide ( ) { return getTurnPosition ( turnPostionPinIN ) < POS_CENTER ; } void right () { Serial.println("RIGHT"); digitalWrite( turnLeftPinOUT , LOW); delay(1); digitalWrite( turnRightPinOUT , HIGH); delay(40); } void left () { Serial.println("LEFT"); digitalWrite( turnRightPinOUT , LOW); delay(1); digitalWrite( turnLeftPinOUT , HIGH); delay(40); } void stopTurn() { //Serial.println("STOP"); digitalWrite( turnRightPinOUT , LOW); digitalWrite( turnLeftPinOUT , LOW); }

Вам также может понравиться

  • Control de Dirección
    Control de Dirección
    Документ7 страниц
    Control de Dirección
    Abraham Rodriguez
    Оценок пока нет
  • Arduino Control Car V2 by
    Arduino Control Car V2 by
    Документ7 страниц
    Arduino Control Car V2 by
    edith
    Оценок пока нет
  • Función Map
    Función Map
    Документ19 страниц
    Función Map
    Librero1234
    Оценок пока нет
  • Arduino 2
    Arduino 2
    Документ4 страницы
    Arduino 2
    Carlos Baide
    Оценок пока нет
  • Ejercicios ARDUINO 2
    Ejercicios ARDUINO 2
    Документ3 страницы
    Ejercicios ARDUINO 2
    Eduardo Núñez
    Оценок пока нет
  • Codigo
    Codigo
    Документ3 страницы
    Codigo
    Dominic Edmundo
    Оценок пока нет
  • Programas Arduino
    Programas Arduino
    Документ8 страниц
    Programas Arduino
    micha2mc
    Оценок пока нет
  • Coche Bluetooth
    Coche Bluetooth
    Документ6 страниц
    Coche Bluetooth
    Carlos Antonio Licona
    Оценок пока нет
  • Arduino Control Car V2 by
    Arduino Control Car V2 by
    Документ7 страниц
    Arduino Control Car V2 by
    Julio Gomez Hernández
    Оценок пока нет
  • Programación en Arduino
    Programación en Arduino
    Документ34 страницы
    Programación en Arduino
    John Morillo
    100% (1)
  • Robot Esquiva Obstaculos
    Robot Esquiva Obstaculos
    Документ4 страницы
    Robot Esquiva Obstaculos
    service électricité bâtiment
    Оценок пока нет
  • Código Cochecito
    Código Cochecito
    Документ11 страниц
    Código Cochecito
    Alex Quiroga
    Оценок пока нет
  • Codigo Del Balancin
    Codigo Del Balancin
    Документ3 страницы
    Codigo Del Balancin
    Fernando Hernandez
    Оценок пока нет
  • Vehiculo Autónomo
    Vehiculo Autónomo
    Документ4 страницы
    Vehiculo Autónomo
    enrique
    Оценок пока нет
  • Arduino
    Arduino
    Документ5 страниц
    Arduino
    lucy
    0% (1)
  • Instrumentación. Clase 14
    Instrumentación. Clase 14
    Документ25 страниц
    Instrumentación. Clase 14
    Katherine Guerrero
    Оценок пока нет
  • M1H 287003 281535 272016
    M1H 287003 281535 272016
    Документ31 страница
    M1H 287003 281535 272016
    Matías Corvetto
    Оценок пока нет
  • Arduino Example 1
    Arduino Example 1
    Документ5 страниц
    Arduino Example 1
    Laura Cardenas
    Оценок пока нет
  • Carro
    Carro
    Документ4 страницы
    Carro
    Gir Estrada
    Оценок пока нет
  • Sistema Transporte Paciente Arduino
    Sistema Transporte Paciente Arduino
    Документ5 страниц
    Sistema Transporte Paciente Arduino
    Raul Perez
    Оценок пока нет
  • Lenguaje ST Maniobra Paro Marcha
    Lenguaje ST Maniobra Paro Marcha
    Документ3 страницы
    Lenguaje ST Maniobra Paro Marcha
    XIMO
    Оценок пока нет
  • Program, A Display 7 Segmentos Casero
    Program, A Display 7 Segmentos Casero
    Документ8 страниц
    Program, A Display 7 Segmentos Casero
    helmer ruiz
    Оценок пока нет
  • Informe Motor DC y Encoder
    Informe Motor DC y Encoder
    Документ3 страницы
    Informe Motor DC y Encoder
    Marcelo Mamani Chara
    Оценок пока нет
  • AguilarAlvaradoJoseMartin19091249-Protns de Arduino
    AguilarAlvaradoJoseMartin19091249-Protns de Arduino
    Документ10 страниц
    AguilarAlvaradoJoseMartin19091249-Protns de Arduino
    Griselda Sinahi Perez Martinez
    Оценок пока нет
  • Codigo Mesa Sismica
    Codigo Mesa Sismica
    Документ5 страниц
    Codigo Mesa Sismica
    0Avendano
    Оценок пока нет
  • Informe Final
    Informe Final
    Документ19 страниц
    Informe Final
    Denisse Baldivieso
    Оценок пока нет
  • DSPIC30f2010 Código C Sanjay
    DSPIC30f2010 Código C Sanjay
    Документ16 страниц
    DSPIC30f2010 Código C Sanjay
    ScribdTranslations
    Оценок пока нет
  • Quiz 5christian Ricardo Resendiz 159669
    Quiz 5christian Ricardo Resendiz 159669
    Документ5 страниц
    Quiz 5christian Ricardo Resendiz 159669
    Cristian Lopezz
    Оценок пока нет
  • Practica (Scratch) - Optoelectronica
    Practica (Scratch) - Optoelectronica
    Документ32 страницы
    Practica (Scratch) - Optoelectronica
    Carlos Alfonso Alvarez Lopez
    Оценок пока нет
  • Codigo Avanzado
    Codigo Avanzado
    Документ3 страницы
    Codigo Avanzado
    Armando Rojas Rojas
    Оценок пока нет
  • Arduino Control House
    Arduino Control House
    Документ2 страницы
    Arduino Control House
    BrandonSilverMena
    Оценок пока нет
  • Proyecto Arudino
    Proyecto Arudino
    Документ3 страницы
    Proyecto Arudino
    Wilder Colque
    Оценок пока нет
  • Programacion Arduino Sistemas de Control
    Programacion Arduino Sistemas de Control
    Документ5 страниц
    Programacion Arduino Sistemas de Control
    Tavo Reyes
    Оценок пока нет
  • Robot Velocista
    Robot Velocista
    Документ8 страниц
    Robot Velocista
    MarioSeverichZurita
    Оценок пока нет
  • Como Armar Un Auto A Control Remoto
    Como Armar Un Auto A Control Remoto
    Документ17 страниц
    Como Armar Un Auto A Control Remoto
    Briian Ariel
    Оценок пока нет
  • Control Arduino Ethernet
    Control Arduino Ethernet
    Документ4 страницы
    Control Arduino Ethernet
    Pao Cabrera
    Оценок пока нет
  • Codigo Servo Ultrasonido
    Codigo Servo Ultrasonido
    Документ2 страницы
    Codigo Servo Ultrasonido
    Anonymous IuHX8x
    Оценок пока нет
  • Código Pista de Carros
    Código Pista de Carros
    Документ2 страницы
    Código Pista de Carros
    Jhonattan Javier
    Оценок пока нет
  • PROGRAMACIONES
    PROGRAMACIONES
    Документ14 страниц
    PROGRAMACIONES
    Aracely Pumaricra Villarreal
    Оценок пока нет
  • Inernadero
    Inernadero
    Документ6 страниц
    Inernadero
    Jose Antonio J M-a
    Оценок пока нет
  • Programacion Lamborghino
    Programacion Lamborghino
    Документ7 страниц
    Programacion Lamborghino
    Alex Garcia
    Оценок пока нет
  • Trabajo Andrea Robotica
    Trabajo Andrea Robotica
    Документ5 страниц
    Trabajo Andrea Robotica
    Nicolle Arcos
    Оценок пока нет
  • CODIGO Modelo de Peaje Con Ultrasonico y Servomotor
    CODIGO Modelo de Peaje Con Ultrasonico y Servomotor
    Документ1 страница
    CODIGO Modelo de Peaje Con Ultrasonico y Servomotor
    Crizz Cardenas
    0% (2)
  • Ejemplos Con Arduino
    Ejemplos Con Arduino
    Документ6 страниц
    Ejemplos Con Arduino
    cesar optimus
    Оценок пока нет
  • Motor Paso A Paso Ajustado A Grados
    Motor Paso A Paso Ajustado A Grados
    Документ2 страницы
    Motor Paso A Paso Ajustado A Grados
    florentino perez
    Оценок пока нет
  • Reporte de Seguidor de Linea
    Reporte de Seguidor de Linea
    Документ10 страниц
    Reporte de Seguidor de Linea
    Raymond Kenney
    Оценок пока нет
  • Laberinto Cuadrado
    Laberinto Cuadrado
    Документ4 страницы
    Laberinto Cuadrado
    Paco Sanz
    Оценок пока нет
  • Codigos Arduino
    Codigos Arduino
    Документ14 страниц
    Codigos Arduino
    Willbert Nungaray
    Оценок пока нет
  • 2 - Jairon - Carro Que Evita Obstaculo
    2 - Jairon - Carro Que Evita Obstaculo
    Документ3 страницы
    2 - Jairon - Carro Que Evita Obstaculo
    Gabriel Arellano
    Оценок пока нет
  • Codigos Arduino
    Codigos Arduino
    Документ5 страниц
    Codigos Arduino
    giulio
    Оценок пока нет
  • Ball and Beam Pid Arduino
    Ball and Beam Pid Arduino
    Документ2 страницы
    Ball and Beam Pid Arduino
    Fernando Gomez
    Оценок пока нет
  • Final Editado Inverso
    Final Editado Inverso
    Документ2 страницы
    Final Editado Inverso
    Solufastit Solufastit
    Оценок пока нет
  • IoT Semaforo PDF
    IoT Semaforo PDF
    Документ2 страницы
    IoT Semaforo PDF
    Centro Evolutivo Del Software
    Оценок пока нет
  • IoT Semaforo PDF
    IoT Semaforo PDF
    Документ2 страницы
    IoT Semaforo PDF
    Centro Evolutivo Del Software
    Оценок пока нет
  • Manual de Funcionamiento CNC
    Manual de Funcionamiento CNC
    Документ16 страниц
    Manual de Funcionamiento CNC
    Rogelio Cajtí
    Оценок пока нет
  • Codigo en Arduino Radar
    Codigo en Arduino Radar
    Документ18 страниц
    Codigo en Arduino Radar
    Pueblo Pit Cuenca
    Оценок пока нет
  • Ejercicios Bloque III y IV
    Ejercicios Bloque III y IV
    Документ2 страницы
    Ejercicios Bloque III y IV
    David Garcia Perez
    Оценок пока нет
  • Estudio de Impacto Ambiental
    Estudio de Impacto Ambiental
    Документ12 страниц
    Estudio de Impacto Ambiental
    Yersin Edison Machacuay Crispin
    Оценок пока нет
  • Los Esencial de La Hipnosis PDF
    Los Esencial de La Hipnosis PDF
    Документ213 страниц
    Los Esencial de La Hipnosis PDF
    Faby RoHu
    100% (1)
  • Ejercicio 4 Simce
    Ejercicio 4 Simce
    Документ6 страниц
    Ejercicio 4 Simce
    María José Vergara Pinto De Arellano
    Оценок пока нет
  • Lectura 1
    Lectura 1
    Документ5 страниц
    Lectura 1
    lucio
    Оценок пока нет
  • 1 5186362057200500959
    1 5186362057200500959
    Документ100 страниц
    1 5186362057200500959
    Ernesto Neto
    Оценок пока нет
  • Cementina
    Cementina
    Документ29 страниц
    Cementina
    Juan Reyes
    Оценок пока нет
  • Teoria Errores USS
    Teoria Errores USS
    Документ18 страниц
    Teoria Errores USS
    piero medina
    Оценок пока нет
  • Brochure Next Pampa V
    Brochure Next Pampa V
    Документ10 страниц
    Brochure Next Pampa V
    thematute
    100% (1)
  • M.P. Joaqd PDF
    M.P. Joaqd PDF
    Документ99 страниц
    M.P. Joaqd PDF
    Yecid Brayam Poma Callisaya
    Оценок пока нет
  • Aminoácidos Precursores de Las Proteínas
    Aminoácidos Precursores de Las Proteínas
    Документ5 страниц
    Aminoácidos Precursores de Las Proteínas
    Xitlalli Espinoza Salgado
    Оценок пока нет
  • Cars Models Brochure KN2 NA May 2019 PDF
    Cars Models Brochure KN2 NA May 2019 PDF
    Документ42 страницы
    Cars Models Brochure KN2 NA May 2019 PDF
    Carlosgduss Garcia
    Оценок пока нет
  • Gps Trimble 5800
    Gps Trimble 5800
    Документ2 страницы
    Gps Trimble 5800
    Robinson Vargas
    100% (1)
  • CAM Cobrethane HV Digital
    CAM Cobrethane HV Digital
    Документ2 страницы
    CAM Cobrethane HV Digital
    Jose Pitti
    Оценок пока нет
  • Examen Simulacion Unam Area 4 57363 Downloable 943269
    Examen Simulacion Unam Area 4 57363 Downloable 943269
    Документ20 страниц
    Examen Simulacion Unam Area 4 57363 Downloable 943269
    Ciber Tron PX
    Оценок пока нет
  • El Corto de Loja 24 08 2016
    El Corto de Loja 24 08 2016
    Документ48 страниц
    El Corto de Loja 24 08 2016
    JoséAndrésOrtizCuesta
    Оценок пока нет
  • El Naturismo Medico en La Medicina Actual
    El Naturismo Medico en La Medicina Actual
    Документ4 страницы
    El Naturismo Medico en La Medicina Actual
    api-3721768
    Оценок пока нет
  • Laboratorio Anato Pato I
    Laboratorio Anato Pato I
    Документ48 страниц
    Laboratorio Anato Pato I
    Medicina Humana 7
    Оценок пока нет
  • Barómetros
    Barómetros
    Документ2 страницы
    Barómetros
    nicol poma
    Оценок пока нет
  • Clasificacion de Los Costos Segun Su Aplicacion - Elemento-9 de Contabilidad
    Clasificacion de Los Costos Segun Su Aplicacion - Elemento-9 de Contabilidad
    Документ44 страницы
    Clasificacion de Los Costos Segun Su Aplicacion - Elemento-9 de Contabilidad
    Gary Ramírez Arce
    Оценок пока нет
  • Motor DSPIC
    Motor DSPIC
    Документ10 страниц
    Motor DSPIC
    Gustavo Choque Cueva
    Оценок пока нет
  • Sesion 3 y Como Planifico
    Sesion 3 y Como Planifico
    Документ12 страниц
    Sesion 3 y Como Planifico
    CharlotteStein
    Оценок пока нет
  • IC-G-D-30-001 - B Especificaciones Combustible Gas General PDF
    IC-G-D-30-001 - B Especificaciones Combustible Gas General PDF
    Документ6 страниц
    IC-G-D-30-001 - B Especificaciones Combustible Gas General PDF
    Cristian Del Alamo
    Оценок пока нет
  • Esquema Monografía Del Tarwi
    Esquema Monografía Del Tarwi
    Документ26 страниц
    Esquema Monografía Del Tarwi
    Anonymous 1Xw9uiFAcv
    Оценок пока нет
  • Informe Aceites Esenciales
    Informe Aceites Esenciales
    Документ8 страниц
    Informe Aceites Esenciales
    Augusto Muñoz Villarreal
    Оценок пока нет
  • Nikola Tesla
    Nikola Tesla
    Документ10 страниц
    Nikola Tesla
    Adrián Huerta
    Оценок пока нет
  • Examen de Los Ojos
    Examen de Los Ojos
    Документ9 страниц
    Examen de Los Ojos
    Catalina Villanueva
    Оценок пока нет
  • Historia 1 M
    Historia 1 M
    Документ3 страницы
    Historia 1 M
    Daniela
    Оценок пока нет
  • Proceso de Embutido de Metales
    Proceso de Embutido de Metales
    Документ1 страница
    Proceso de Embutido de Metales
    david_valdez_83
    Оценок пока нет
  • TFG Caballero Moyano Beatriz
    TFG Caballero Moyano Beatriz
    Документ74 страницы
    TFG Caballero Moyano Beatriz
    Muñoz Donoso Vanne
    Оценок пока нет