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!