Wednesday, November 18, 2015

Robot controlled by Ethernet

How to build a Robot on an Arduino that can be controlled remotely online:
by J.B. Wylzan
The Web Server



This robot is controlled using your web server.




ARDUINO SKETCH:

/* ===============================================================
      Project Homotronics: Robot controlled by Ethernet
      Author: J. B. Wylzan with some help from the Arduino community
      Website: http://www.ihackrobot.blogspot.com
      Abstract: Control a robot via the ethernet.
================================================================== */
#include <SPI.h>
#include <Ethernet.h>

const int MLWF = 4;             
const int MLBB = 7;  
const int MRYF = 6;             
const int MRRB = 5;  

byte mac[] = { your mac address };   
byte ip[] = { your ip address  };                      
byte gateway[] = { 192, 168, x, x };                  
byte subnet[] = { 255, 255, x, x };                 
EthernetServer server(80);                             //or 8081     
String gString;

void setup() {

  Serial.begin(9600);

  pinMode(MLWF, OUTPUT);  
  pinMode(MLBB, OUTPUT); 
  pinMode(MRYF, OUTPUT); 
  pinMode(MRRB, OUTPUT);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("my server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  
  EthernetClient client = server.available();
  if (client == true) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
        if (gString.length() < 100) {
          gString = gString + c;
         }

         if (c == '\n') {          
           Serial.println(gString); 
     
           client.println("HTTP/1.1 200 OK"); 
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<!DOCTYPE HTML>");
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<TITLE>iHackLab</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY bgcolor='#ff4d4d'>");
          client.println("<center>");
           client.println("<H1>iHackLab Homotronics Website</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>Robotics with Arduino Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/fwd\"\">Move Forward</a>");
              client.println("<br />");   
               client.println("<br />");  
           client.println("<a href=\"/rvs\"\">Move Backward</a><br />");   
           client.println("<br />"); 
            client.println("<br />");  
           client.println("<a href=\"/rht\"\">Turn Right</a>");
              client.println("<br />");   
               client.println("<br />");  
           client.println("<a href=\"/lft\"\">Turn Left</a><br />"); 
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<br />");    
           client.println("<a href=\"/stp\"\">Stop</a><br />"); 
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<br />");   
           client.println("<hr />");
           client.println("<www.thearduinosketch.blogspot.com>"); 
           client.println("<hr />");
           client.println("<br />"); 
           client.println("</center>");
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);  //page loading delay
           client.stop();
          Serial.println("client disconnected");

           if (gString.indexOf("fwd") >0){
               Serial.println("Forward");
        digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 255);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, HIGH);
           }
           if (gString.indexOf("rvs") >0){
                Serial.println("Reverse");
         digitalWrite(MLBB, 255);         
    digitalWrite(MLWF, 0);
    digitalWrite(MRRB, HIGH);        
    digitalWrite(MRYF, LOW);
           }
           if (gString.indexOf("lft") >0){
                 Serial.println("Left");
        digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 255);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, LOW);
           }
           if (gString.indexOf("rht") >0){
              Serial.println("Right");
         digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 0);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, HIGH);
           }
           if (gString.indexOf("stp") >0){
               Serial.println("Stop");
        digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 0);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, LOW);
           }
            gString="";  
           
         }
       }
    }
}

/* ================================================================== */
 Circuit: (optional)

 * Ethernet shield prepares to be attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5
 * On-board SS micro-SD card slot must be on pin 4 

The shield contains a number of informational LEDs: 
  • PWR: indicates that the board and shield are powered 
  • LINK: indicates the presence of a network link and flashes when the shield transmits or receives data
  • FULLD: indicates that the network connection is full duplex 
  • 100M: indicates the presence of a 100 Mb/s network connection (as opposed to 10 Mb/s) 
  • RX: flashes when the shield receives data 
  • TX: flashes when the shield sends data 
  • COLL: flashes when network collisions are detected.



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
 ================================================================== 






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
 ================================================================== 






Wednesday, November 4, 2015

Robot controlled by Sonar

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




ARDUINO SKETCH:

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

#define trigPin 12
#define echoPin 11
#define IN1 10
#define IN2 9
const int redLed = 6;     
const int greenLed = 7;   
const int yellowLed = 8;  

// ENABLE PWM 3,5,6,9,10,11

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);    
  pinMode(echoPin, INPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
    pinMode(redLed, OUTPUT);
 pinMode(greenLed, OUTPUT);
 pinMode(yellowLed, OUTPUT);
}

void loop() {
  long time, range;
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(2);   //2
  digitalWrite(trigPin, LOW);

  time = pulseIn(echoPin, HIGH);
  range = (time/2)/29.1;    //58.2

  if (range < 20)
  {
    analogWrite(IN1, 0);        
    analogWrite(IN2, 255);
   
    Serial.println("Moving forward");
    Serial.println("Blinking Green");
     Serial.print(range);
    Serial.println(" cm");

 //   delay(1000);
  //  digitalWrite(redLed, HIGH);
//delay(1000);
//digitalWrite(redLed, LOW);
  }
  else        
    {
        analogWrite(IN1, 255);   
        analogWrite(IN2, 0);      
       
        Serial.println("stopping");
         Serial.println("Blinking Red");
          Serial.print(range);
    Serial.println(" cm");

     // digitalWrite(yellowLed, HIGH);
//delay(1000);
//digitalWrite(yellowLed, LOW);                                         
    } 
}





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
 ==================================================================