Solar Powered Black Cockroach Bug Toy Documentation
The Solar Powered Black Cockroach Bug Toy is an innovative Internet of Things (IoT) component that combines a solar-powered energy harvesting system with a robotic bug toy. This component is designed for entertainment, education, and research purposes, offering a unique platform for exploring robotic movement, solar energy, and IoT integration.
Solar Panel: 2V, 100mA, 10x10mm
Microcontroller: ATtiny85, 8-bit, 8KB Flash
Motor: 2x DC Gear Motor, 1.5V, 100RPM
Sensor: Light Dependent Resistor (LDR) for ambient light detection
Communication: Infrared (IR) transceiver for remote control
Power: Solar powered with backup battery (30mAh, 1.5V)
Dimensions: 70x40x20mm (LxWxH)
### Example 1: Basic Movement Control using Arduino
In this example, we'll demonstrate how to control the Solar Powered Black Cockroach Bug Toy using an Arduino board. We'll use the Arduino Uno as the control unit, and the bug toy will receive IR commands to move forward, backward, left, and right.
Arduino Code:
```c++
#include <IRremote.h>
const int irReceiverPin = 11; // IR receiver pin on Arduino Uno
IRrecv irrecv(irReceiverPin);
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
if (irrecv.decode()) {
Serial.println(irrecv.decodedIRData.command);
switch (irrecv.decodedIRData.command) {
case 0xFF38C7: // Forward command
motorForward();
break;
case 0xFF5AA5: // Backward command
motorBackward();
break;
case 0xFF4AB5: // Left command
motorLeft();
break;
case 0xFF9867: // Right command
motorRight();
break;
}
irrecv.resume(); // Receive the next IR command
}
}
void motorForward() {
// Control the motor to move forward
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
}
void motorBackward() {
// Control the motor to move backward
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
}
void motorLeft() {
// Control the motor to turn left
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
}
void motorRight() {
// Control the motor to turn right
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
```
### Example 2: Ambient Light Sensing using NodeMCU (ESP8266)
In this example, we'll demonstrate how to use the Solar Powered Black Cockroach Bug Toy's LDR sensor to detect ambient light levels and adjust its movement accordingly. We'll use a NodeMCU board (ESP8266) as the control unit.
NodeMCU Code:
```lua
-- Import necessary libraries
local gpio = require("gpio")
local adc = require("adc")
-- Define pin assignments
local ldrPin = 0 -- LDR pin on NodeMCU
local motorPin1 = 2 -- Motor pin 1 on NodeMCU
local motorPin2 = 4 -- Motor pin 2 on NodeMCU
-- Initialize motor pins as outputs
gpio.mode(motorPin1, gpio.OUTPUT)
gpio.mode(motorPin2, gpio.OUTPUT)
-- Read LDR value and adjust motor speed
while true do
local ldrValue = adc.read(0) -- Read LDR value (0-1023)
if ldrValue < 300 then -- Low light condition
-- Move slowly
gpio.write(motorPin1, gpio.HIGH)
gpio.write(motorPin2, gpio.LOW)
tmr.delay(100)
elseif ldrValue > 700 then -- High light condition
-- Move quickly
gpio.write(motorPin1, gpio.LOW)
gpio.write(motorPin2, gpio.HIGH)
tmr.delay(50)
else -- Medium light condition
-- Move at medium speed
gpio.write(motorPin1, gpio.HIGH)
gpio.write(motorPin2, gpio.HIGH)
tmr.delay(75)
end
tmr.delay(50) -- Small delay between iterations
end
```
These examples demonstrate the Solar Powered Black Cockroach Bug Toy's versatility in various IoT applications. You can customize and expand upon these examples to explore more complex scenarios and use cases.