微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 硬件工程师文库 > 萌版蜘蛛侠:爬墙机器人DIY作品秀

萌版蜘蛛侠:爬墙机器人DIY作品秀

时间:07-29 来源:电子发烧友网 点击:

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 ((

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top