fbpx
5: Cockroach Robot
Arduino Project

Cockroach Bot

Make a Robot that hides in the Dark.

Components Required

Introduction

We will learn about how to make a Cockroach bot using Magicbit (ESP-32)  development board and Arduino .  This robot will move away from any light sources.

 

Theory and Methodology

We will use the onboard LDR on the Magicbit board to get a measure of the light intensity. Whenever the light intensity goes over a threshold value, we will make the robot (magicbot) move in the opposite direction to the light.

Code

Upload the given code to the Magicbit board.

bool dir = 1;


void setup() {
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(18, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int lux = analogRead(36);
  if (lux>1200) {
    if (dir==1) {
      digitalWrite(17, HIGH);
      digitalWrite(16, LOW);
      digitalWrite(18, HIGH);
      digitalWrite(27, LOW);
    }

    else {
      digitalWrite(17, LOW);
      digitalWrite(16, HIGH);
      digitalWrite(27, HIGH);
      digitalWrite(18, LOW);
    }
    delay(2000);
    dir = !dir;
  }else{
      digitalWrite(17, LOW);
      digitalWrite(16, LOW);
      digitalWrite(18, LOW);
      digitalWrite(27, LOW);
  }

}

Deploy and Enjoy!

Related Posts
Leave a Reply