Arduino (IoT): Simple Tutorial Robot Autónomo Parte III

Arduino Autonomous Rover Technology Santiapps

Arduino Honduras Santiapps Marcio Valenzuela

Tutorial Robot Autónomo: Parte III

Este sketch es un poco mas fácil de entender y no utiliza un motor shield si ese es tu caso.

Requisitos:

  1. Computadora (mac)
  2. Arduino UNO.
  3. Llantas (2)
  4. Motores DC (2)
  5. Sensor Ultrasonico
  6. Mini Servo
  7. Arduino IDE (https://www.arduino.cc/en/Main/Software)

 

Componentes

Arduino Robot Autonomo Movil Motor Shield Tutorial Santiapps Marcio Valenzuela
Arduino Robot Autonomo Movil Motor Shield

La única diferencia es que no usa el motor shield o sea que los motores se conectan directamente a la UNO.  Veamos el código:

#include <Servo.h>
#include <NewPing.h>

//CONSTANTES
#define LeftMotorForward 2
#define LeftMotorBackward 3
#define RightMotorForward 5
#define RightMotorBackward 4
#define USTrigger 8
#define USEcho 9
#define MaxDistance 100
#define LED 13

Servo servo;
NewPing sonar(USTrigger, USEcho, MaxDistance);

//VARIABLES
unsigned int duration;
unsigned int distance;
unsigned int FrontDistance;
unsigned int LeftDistance;
unsigned int RightDistance;
unsigned int Time;
unsigned int CollisionCounter;

void setup() {
  Serial.begin(9600);
  pinMode(LeftMotorForward, OUTPUT); //Modo de los pines
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(LED, OUTPUT);
  servo.attach(6); //Servo en pin 6
}

void loop(){
  servo.write(90); //Ver frente                 
  scan(); //Mapear
  FrontDistance = distance; //Que tenemos al frente
  Serial.println("Distancia frontal = ");
  Serial.print(distance);
  if(FrontDistance > 40 || FrontDistance == 0) {
   moveForward(); //Si no hay nada, mover al frente
  } else {
    CollisionCounter = CollisionCounter + 1;
    moveStop(); //Detener
    navigate(); //Navegar
  }
}

void moveForward(){
  Serial.println("");
  Serial.println("Avanzando");
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
}

void moveBackward(){
  Serial.println("");
  Serial.println("Retrocediendo");
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
}

void moveLeft(){
  Serial.println("");
  Serial.println("Izquierda");
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
  
}

void moveRight(){
  Serial.println("");
  Serial.println("Derecha");
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
}

void moveStop(){
  Serial.println("");
  Serial.println("Deteniendo marcha");
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}
void scan(){
  Serial.println("");
  Serial.println("Mapeando");
  Time = sonar.ping(); 
  distance = Time / US_ROUNDTRIP_CM;
  delay(500);
}
void navigate(){
    Serial.println("PELIGRO!");
    servo.write(167); //Mover servo a la izquierda
    delay(1000); 
    scan(); 
    LeftDistance = distance;
    Serial.println("Distancia Izquierda = ");
    Serial.print(distance);
    servo.write(0); //Mover servo a la derecha
    delay(1000);
    scan();
    RightDistance = distance; 
    Serial.println("Distancia Derecha = ");
    Serial.print(distance);
    if(abs(RightDistance - LeftDistance) < 5){
      moveBackward();
      delay(200);
      moveRight();
      delay(100);
    }else if(RightDistance < LeftDistance){
     moveLeft();  
     delay(100);
    }else if(LeftDistance < RightDistance){
     moveRight(); 
     delay(100);
    }
}

Esperamos que te estes divirtiendo!

Arduino

Arduino Engineering
Arduino Engineering
Arduino Engineering

So the perfect evolution from mobile device programming is arduino/microcontroller programming.

It’s basically moving from controlling software to controlling hardware.  Ever feel like coding apps is cool but you wish they could actually move things?

I started off with a basic arduino kit with motors and leds.

Arduino Engineering
Arduino Engineering

Of course I started out with the:

– LED blink (on-board)

– LED blink with breadboard

I went a little crazy and my head overflowed with ideas for projects.  I even made Arduino Pancakes!  Very tough btw.

Arduino Engineering
Arduino Engineering

 

Pretty soon I was getting shields and sensors, motors, displays and a soldering iron.

Arduino Engineering
Arduino Engineering

I quickly moved onto:

– LCD display

– DC motor

– Servo Motor

Arduino Engineering
Arduino Engineering

Then I started looking into making the base for a robot that would have red eyes like ultra (just saw the avengers) and voice recognition, mobility and cloud connected.  I started getting into RMF values for motors and then I decided to hack an old RC my kid had.  I figured how to make the wheels go and turn and I was set.

Arduino Engineering
Arduino Engineering

I moved on to the sensors.  I got a Grove shield and sensors for:

– Temperature and Humidity

– Light and UV

– CO2 and NO

– VHOCs

– Dust

Arduino Engineering
Arduino Engineering

At this point I got sidetracked into learning:

– Bluetooth HC-05

– TinySine WiFi shield

– Sainsmart GSM/GPRS SIM900

Arduino Engineering
Arduino Engineering

This is where I got a better grip on:

– Serial Communication (Hardware vs Soft)

– Software libraries

– Shields and their components

So I learned to configure the WiFly module (component in the TinySine Wifi Shield) and how to use the GPRS module.

I even had to throw in a little Android:

Android Engineering
Android Engineering

I must confess I did impulsively buy a SainSmart OLED display. I may add it to the robot in the future but it was really overkill.

So my “robot” will have mobility, data collection and wireless communication capabilities.  If I could just make it fly!

Arduino Engineering
Arduino Engineering

Arduino (IoT): Simple Tutorial Detector de Vibraciones

Vibration Detection Arduino Santiapps

Arduino Honduras Santiapps Marcio Valenzuela

Tutorial Detector de Vibraciones

Detectar Movimiento.  PiezoVibration.ino.

Requisitos:

  1. Computadora (mac)
  2. Arduino MEGA (u otra variante Arduino) o incluso Arduino clone, Edison, Raspberry…  Este tutorial en particular usa una Arduino MEGA.
  3. Piezo Vibration Sensor
  4. Breadboard
  5. Arduino IDE (https://www.arduino.cc/en/Main/Software)
Arduino Tutorial Piezo Vibración Detector de Movimientos Santiapps Marcio Valenzuela
Arduino Tutorial Piezo Vibración Detector de Movimientos

 

El código:

[code]
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading &gt;= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string “Knock!” back to the computer, followed by newline
Serial.println(“Knock!”);
}
delay(100); // delay to avoid overloading the serial port buffer
}
[/code]

Ese proyecto se puede ver aquí:

Es todo!  Parece insignificante pero la base es importante.  Porque?  Pues los dejo con estas ideas:

  • Esa LED podría ser un motor o un cerrojo o una valvula de flujo
  • En lugar de repetir el ciclo cada segundo, podríamos controlar el flujo en cada ciclo agregando una condición, ie: Si un botón esta oprimido, o un sensor ( de luz, sonido, calidad de aire, temperatura, huella digital, código ingresado etc) cumplen con las condiciones requeridas para enviar esa corriente.
  • O que pasa si, en lugar de solo enviar corriente, mandamos o leemos datos?  A internet?  A un celular?  A un BT, NFC etc…

Allí es donde se pone interesante.  Nos vemos en el proximo donde controlaremos la LED usando botones y potenciometros y aprenderemos a leer electricidad, no solo escribirla!