2015自走車研習

問卷調查  https://docs.google.com/forms/d/1XeHOjBv7rYQ1yYBf1nCVG-onJzGL0xJBA47xF9Tkyp8/viewform?usp=send_form

下午程式 一:

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
#include "Ultrasonic.h"
Ultrasonic ultrasonic(12,13);  //Trige,Echo
int ulval;
int act;

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

int pinI1=8;//定义I1接口
int pinI2=9;//定义I2接口
int speedpin=11;//定义EA(PWM调速)接口
int pinI3=6;//定义I3接口
int pinI4=7;//定义I4接口
int speedpin1=10;//定义EB(PWM调速)接口

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpin,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpin1,OUTPUT);
  myservo.attach(5);  // attaches the servo on pin 9 to the servo object
}

// the loop routine runs over and over again forever:
void loop() {
  myservo.write(90);
  ulval=ultrasonic.Ranging(CM);

  if (ulval > 10){
   act=0; 
  }
  
  if (ulval < 5){
   act=2; 
  }
  else {
   act=1; 
  }
  Serial.println(ulval);

 switch (act)
  {
   case 0:
    stop(5);
    Serial.println("stop");
    break;
    
   case 1:
    move(10);
   Serial.println("move");
    break;
    
   case 2:
    back(10);
   Serial.println("back");
    break;
    
   default:
    stop(5);
     Serial.println("defult");

  } 



  delay(100);        // delay in between reads for stability
}


//=====================================================================================

void move(int a){
     analogWrite(speedpin,100);//输入模拟值进行设定速度
     analogWrite(speedpin1,100);
     digitalWrite(pinI4,LOW);//使直流电机(右)逆时针转
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI1,LOW);//使直流电机(左)顺时针转
     digitalWrite(pinI2,HIGH);
     delay(a*100);
}

void stop(int a){
     digitalWrite(pinI4,HIGH);//使直流电机(右)刹车
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI1,HIGH);//使直流电机(左)刹车
     digitalWrite(pinI2,HIGH);
     delay(a*100);
}

void right(int a){
     analogWrite(speedpin,60);//输入模拟值进行设定速度
     analogWrite(speedpin1,60);
     digitalWrite(pinI4,HIGH);//使直流电机(右)顺时针转
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI1,LOW);//使直流电机(左)顺时针转
     digitalWrite(pinI2,HIGH);
     delay(a*100);
}

void left(int a){
     analogWrite(speedpin,60);//输入模拟值进行设定速度
     analogWrite(speedpin1,60);
     digitalWrite(pinI4,LOW);//使直流电机(右)逆时针转
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI1,HIGH);//使直流电机(左)逆时针转
     digitalWrite(pinI2,LOW);
     delay(a*100);
}

void back(int a){
     analogWrite(speedpin,100);//输入模拟值进行设定速度
     analogWrite(speedpin1,100);
     digitalWrite(pinI4,HIGH);//使直流电机(右)顺时针转
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI1,HIGH);//使直流电机(左)逆时针转
     digitalWrite(pinI2,LOW);
     delay(a*100);
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。