萌版蜘蛛侠:爬墙机器人DIY作品秀
ghtSensor = 3; //300
// Hardcoded thresholds (not used because we auto-calibrate)
int topThreshhold = 400;
int leftThreshhold = 550;
int frontThreshhold = 200;
int rightThreshhold = 650;
// Current robot type (red gree or yellow)
int STATE = 0;
// State values
int RED = 0;
int GREEN = 1;
int ORANGE = 2;
// Pins to drive the top tri-color LED
int redPin = 5;
int greenPin = 6;
// Values to hold sensor readings
int front;
int right;
int left;
int top;
// Auto-calibrate light sensor thresholds
void calibrate() {
Serial.println("CALIBRATING");
long int val = 0;
for (int i = 0; i《5; i++) {
val += analogRead(frontSensor);
delay(10);
}
frontThreshhold = (val /5) - 80;
val = 0;
for (int i = 0; i《5; i++) {
val = val + analogRead(topSensor);
Serial.println(analogRead(topSensor));
Serial.println(val);
delay(10);
}
topThreshhold = (val /5) -200;
val = 0;
for (int i = 0; i《5; i++) {
val += analogRead(rightSensor);
}
rightThreshhold = (val /5) - 100;
val = 0;
for (int i = 0; i《5; i++) {
val += analogRead(leftSensor);
}
leftThreshhold = (val /5) - 100;
// Print threshold values for debug
Serial.print("top: ");
Serial.println(topThreshhold);
Serial.print("right: ");
Serial.println(rightThreshhold);
Serial.print("left: ");
Serial.println(leftThreshhold);
Serial.print("front: ");
Serial.println(frontThreshhold);
}
void setup()
{
// turn on pin 13 for debug
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// setup sensor pins
for (int i = 0; i《4; i++) {
pinMode(i, INPUT);
}
Serial.begin(9600);
calibrate();
// generate a random state
STATE = random(0, 3);
setColor(STATE);
}
// MOTOR FUNCTIONS
void turnLeft()
{
Serial.println("LEFT");
start();
delay(20);
for (int i = 0; i《20; i++) {
servo2.write(179);
servo1.write(1);
delay(20);
}
stop();
delay(20);
}
void turnRight() {
Serial.println("RIGHT");
start();
delay(20);
for (int i = 0; i《20; i++) {
servo2.write(1);
servo1.write(179);
delay(20);
}
stop();
delay(20);
}
void goForward(int del = 20) {
Serial.println("FORWARD");
start();
delay(20);
for (int i = 0; i《20; i++) {
servo1.write(179);
servo2.write(179);
delay(del);
}
stop();
delay(20);
}
void stop() {
servo1.detach();
servo2.detach();
delay(10);
}
void start() {
servo1.attach(10);
servo2.attach(9);
}
// Set the color of the top tri-color LED based on the current state
void setColor(int color) {
if (color == RED) {
digitalWrite(greenPin, 0);
analogWrite(redPin, 180);
}
else if (color == GREEN) {
digitalWrite(redPin, 0);
analogWrite(greenPin, 180);
}
else if (color == ORANGE) {
analogWrite(redPin, 100);
analogWrite(greenPin, 100);
}
}
// Blink the yellow color (when robot is confused)
void blinkOrange() {
for (int i = 0; i《5; i++) {
analogWrite(redPin, 100);
analogWrite(greenPin, 100);
delay(300);
digitalWrite(redPin, 0);
digitalWrite(greenPin, 0);
delay(300);
}
analogWrite(redPin, 100);
analogWrite(greenPin, 100);
}
void loop()
{
top = analogRead(topSensor);
long int time = millis();
while (analogRead(topSensor) 《 topThreshhold) {
delay(10); // while there is an arm wave from the user don‘t do anything
}
if ((
- 先进手术系统所需的独特电源需求,凌力尔特来接招(10-11)
- 3G/WIFI控制太阳能驱动机器人制作详解(10-28)
- 超声波移动机器人导航设计方法(04-11)
- ROCKWELL系统在机器人汽车焊装线上的应用(07-07)
- 智能机器人在家庭医疗保健的设计和应用(09-19)
- 基于DSP和机器人的声控系统设计与实现(02-21)