Video Tutorial And Code For Object Avoiding Robot

Here is the code for object avoiding robot that uses one HC-SR04 Ultrasonic Object Detector.

int trigPin = 2;   
int echoPin = 3;   
long duration, inches;
void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(4,OUTPUT);
 pinMode(7,OUTPUT);
}
void loop()
{
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 inches = (duration/2) / 74;
 delay(100);
 if(inches<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
 }
 if(inches>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,HIGH);
 }
}

Learn how to set up a headless raspberry pi (needs to connected to the display for the one-time setup)
Check out other articles from learn2create.

Arduino Controlled Floating Magnet Switch

The Floating Magnet Switch

Another option for building the switch is to use magnets to control the switch. This is more for adding that “cool factor” to the project than function.

The Parts

A Shoe Box

Magnets

String or Wire

 

Building The Frame

Attach your magnets to your string or wire. I used wire because I didn’t have any string lying around. Poke a hole in the top and the bottom of the shoe box so you can run the strings or wires through. Tie them off on the outside of the box.

 

Make sure the magnets aren’t too far apart. If the magnets are too close, all you have to do is tie knots onto the string or wire on the inside of the box to pull the magnets further apart.

 

Once the magnets are attached to the box properly, stand the box up and hang the bottom magnet from the top one. Poke a hole in the side of the box at about the height of the top magnet. Run the wires for the ultrasonic object detector through this hole and then attach the sensor to the wires. Position the sensor so it is aiming at the bottom magnet.

Afterwards, follow the steps above to upload code to the board, but use this code instead:

int trigPin = 2;    
int echoPin = 3;    
long duration, inches;
int switchState = 0;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(7, OUTPUT);
  pinMode(13,OUTPUT);
}
void loop() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    inches = (duration/2) / 74; 
    delay(200);
    while(switchState==0) {
      if(inches>5){
        break;
      }
      if(inches<=5) {
          switchState=1;
        analogWrite(6,127);
        digitalWrite(13,HIGH);
        digitalWrite(7,HIGH);
        delay(100);
        analogWrite(6,0);
        delay(100);   
        break;
      }
    }
    while(switchState==1) {
      if(inches<=5) {
        break;
      }
      if(inches>5) {
        switchState=0;
        analogWrite(6,127);
        digitalWrite(13,LOW);
        digitalWrite(7,LOW);
        delay(100);
        analogWrite(6,0);
        delay(100);
        break;
      }
    }
}

This is a Modified version of the Touchless Lightswitch.
Click here to see the full build article!