Wednesday, November 11, 2015

Robot controlled by Bluetooth

How to build a Robot on an Arduino that can be controlled using a Bluetooth:
by J.B. Wylzan

the accelarometer is actually a bluetooth module




ARDUINO SKETCH:

/* ===============================================================
      Project Homotronics: Robot controlled by bluetooth
      Author: J. B. Wylzan with some help from the Arduino community
      Website: http://www.ihackrobot.blogspot.com
      Abstract: Control a robot via bluetooth.
================================================================== */

int IN1 = 6; 
int IN2 = 5;  
int ENA = 4;  
int mode;
int key=0;        

void setup() {
   
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    //pinMode(ENA, OUTPUT);
    // digitalWrite(ENA, HIGH);
  
    Serial.begin(9600);
}

void loop() {
     
    if(Serial.available() > 0){     
      mode = Serial.read();   
      key=0;
    }   
    
    if (mode == '0') {
          analogWrite(IN1, 0);        
          analogWrite(IN2, 0);
        if(key== 0){
            Serial.println("Pressed 0");
            Serial.println("Stopping");
            Serial.println("Blinking Red");
          key=1;
        }
    }
    
    else if (mode == '1') {
       analogWrite(IN1, 0);        
       analogWrite(IN2, 255);
        if(key== 0){
           Serial.println("Pressed 1");
           Serial.println("Moving forward");
           Serial.println("Blinking Green");
          key=1;
        }
    }
    
    else if (mode == '2') {
        analogWrite(IN1, 255);        
        analogWrite(IN2, 0);
        if(key== 0){
            Serial.println("Pressed 2");
            Serial.println("Moving reverse");
            Serial.println("Blinking Yellow");
          key=1;
        }
    }

}


*** Warning ***
1. Remove the RX and TX cables when uploading the sketch.
2. Connect TX to RX and RX to TX.


//* HC06 bluetooth sensor with the relay


int relay = 8; 
void setup()
{
   Serial.begin (9600); 
   pinMode(relay,OUTPUT); 
 }
void loop()
{
   char val,mVal; 
  val = Serial.read(); 
  mVal=val;
  if (mVal=='1' ) 
 {
   digitalWrite(relay,HIGH); 
  }
  if(mVal=='0')  
  {
    digitalWrite(relay,LOW); 
  }
 }


//* HC06 bluetooth sensor w/o the relay

void setup()
{
   Serial.begin (9600); 
 }
void loop()
{
   char mVal;
  mVal= Serial.read(); 
  
if ((mVal>='a'&& mVal<='z') || (mVal>='A'&& mVal<='Z'))
  {
    Serial.println(mVal); 
  }


 }  */

// Processing: watch2PFNL
// Click on the image to give it focus,
// and then press any key.

import processing.serial.*; 

Serial port;                       // Create object from Serial class

void setup() { 
  size(100, 100); 
  noStroke(); 
  frameRate(10); 
  // Open the port that the board is connected to and use the same speed (9600 bps)
  port = new Serial(this,"COM#",9600); 

int value = 0;

void draw() {
  fill(value);
  rect(25, 25, 50, 50);
}

void keyPressed() 
{
  
   switch (keyCode) {   
    case 'J':
    value = 255;
    port.write('1');
    break;
    
    case 'L':
    value = 0;
    port.write('2');
    break; 
    
   }
}



Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 






No comments:

Post a Comment