|
|
新程序,打算用 夏普 的红外传感器壁障。
#include <Servo.h>
Servo myservo;
Servo roll;
Servo pitch;
int left;
int right;
int front;
int back;
// read analog input from IR ranger finder
int val1 = 1;
int val2 = 2;
int val3 = 3;
int val4 = 4;
const int echo = 12;
const int trig = 13;
const int sw = 3;
const int th = 4;
const int rollin = 5;
const int pitchin = 6;
void setup()
{
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
pinMode(rollin,INPUT);
pinMode(pitchin,INPUT);
int sensorValue = digitalRead(rollin);
int sensorValue = digitalRead(pitchin);
int sensorValue = digitalRead(echo);
int swValue = digitalRead(sw);
int thValue = digitalRead(th);
myservo.attach(9);
roll.attach(8);//there must be somthing wrong here!!!
// 8,7pin do not allow PWM output!
// DO NOT FORGET CHANGE THE NUMBER,Before test.
pitch.attach(7);
}
void loop()
{
long rol, pit;
rol = pulseIn(rollin,HIGH);
pit = pulseIn(pitchin,HIGH);
left = analogRead(val1);
right = analogRead(val2);
front = analogRead(val3);
back = analogRead(val4);
//left is from 0 to 1023,distance between 10cm and 80cm.see graph.
// rol is from 1000 to 2000
rolling = lift - right; // rolling is from -1023 to +1023
map(rolling, -1023, +1023, -150, 150);
rol = rol + rolling;
map(rol, -850, 2150, 0, 179);
roll.write(rol);//maybe it need to be reviced. swtch left and right.
pitching = front - back;
map(pitching, -1023, +1023, -150, +150);
pit = pit + pitching;
map(pit, -850, 2150, 0, 179);//the samthing here.
pitch.write(pitch);
long duration, hold ,power;
hold = pulseIn(sw,HIGH);
if (hold>1500)
{
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(12);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
duration = map(duration, 0, 2000, 45, 135);
myservo.write(duration);
delay(15);
}
else {
power = pulseIn(th,HIGH);
power = map (power, 1000, 2000, 135, 45);
myservo.write(power);
delay(15);
}
} |
|