Ultrasound code:
This is for the HC-SR04 Ultrasound as an input to the Arduino UNO.
Warning: We have only got this working in air and NOT underwater
----------------------------------------------------------------
#define LogicR1 2 // Defining the h-bridge
pins to digital outputs on the arduino
#define LogicR2 3
#define LogicL1 4
#define LogicL2 5
#define trigger 13 // defining the sensor's
trigger and echo pin
#define echo 12
void setup()
{
Serial.begin(9600); //9600 bits per
seconds.A default setting for arduino
pinMode(echo,INPUT); // configuring the
echo pin to act as an input
pinMode(LogicR1,OUTPUT); // configuring all
the other defined pins to act as outputs.
pinMode(LogicR2,OUTPUT);
pinMode(LogicL1,OUTPUT);
pinMode(LogicL2,OUTPUT);
pinMode(trigger,OUTPUT);
pinMode(LogicX1,OUTPUT);
pinMode(LogicX2,OUTPUT);
}
void loop()
{
long Time;
long distance; // defining the terms
distance and time as long integer
digitalWrite(trigger, LOW);
//sets the trigger pin off
delayMicroseconds(4); // delays for 4 microseconds
digitalWrite(trigger, HIGH); //
http://arduino.cc/en/Reference/DelayMicroseconds
delayMicroseconds(9); //
reference:
http://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
digitalWrite(trigger, LOW); // This process configures the trigger pin
to work as an output. Reference websit above.
Time = pulseIn(echo, HIGH); //pulseIn reads the pulse when the echo pin
is high and sets the value as Time.
distance = (Time/2) / 6.910; // 6.910 is the pace of sound underwater
if
(distance < 6) {
STOP();
Back(3000); // the AUV goes back for 3 seconds before turning right for
seconds.
Turn_Right(2000);
Go_Forward();
}
else {
Go_Forward();
}
if
(distance >= 140 || distance <= 0){ // if the distance is far, the AUV is
set to turn left and then go straight.
Serial.println("The AUV can't detect anything");
Turn_Left(2000);
Go_Forward();
}
else {
Serial.print(distance);
Serial.println(" cm"); // prints the distance to the object in
centimeters.
}
delay(400); // delays for 4 milliseconds
}
void Go_Forward()
{
Serial.println("The AUV is going
forward!");
digitalWrite(LogicR1,LOW); // H-bridge configuration for the AUV to go
forward
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);
}
void STOP()
{
Serial.println ("STOPPPPP!!!");
// H-bridge configuration for the AUV to go stop
digitalWrite(LogicR1,LOW);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,LOW);
}
void Turn_Right(int Enter_Time)
{
Serial.println ("The AUV turns
right"); // H-bridge configuration for the AUV to go right
digitalWrite(LogicR1,LOW);
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,HIGH);
digitalWrite(LogicL2,LOW);
delay(Enter_Time);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
}
void Turn_Left(int Enter_Time)
{
Serial.println ("The AUV turns
left");
digitalWrite(LogicR1,HIGH); // h-bridge pin
configuration for turning left
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);
delay(Enter_Time); // once the time is
entered, the AUV turns left for that many seconds.
digitalWrite(LogicR1,LOW); // Stops the
motor after turning left.
digitalWrite(LogicL2,LOW);
}
void Back(int Enter_Time)
{
Serial.println ("The AUV is going
back"); // H-bridge configuration
for the AUV to go backwards
digitalWrite(LogicR1,HIGH);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,HIGH);
digitalWrite(LogicL2,LOW);
delay(Enter_Time);
digitalWrite(LogicR1,LOW);
digitalWrite(LogicL1,LOW);
}
Back up code:
This is based on a timer, where the program tells the AUV to pause, go forward for a period of time then reverse and turn. This program then loops round.
----------------------------------------------------------------
#define LogicR1 2 // Defining the h-bridge
pins to digital outputs on the arduino
#define LogicR2 3
#define LogicL1 4
#define LogicL2 5
#define trigger 13 // defining the sensor's
trigger and echo pin
#define echo 12
#define LogicX1 7
#define LogicX2 8
void setup(){Serial.begin(9600); //9600
bits per seconds.A default setting for arduino
pinMode(echo,INPUT); // configuring the
echo pin to act as an input
pinMode(LogicR1,OUTPUT); // configuring all
the other defined pins to act as outputs.
pinMode(LogicR2,OUTPUT);
pinMode(LogicL1,OUTPUT);
pinMode(LogicL2,OUTPUT);
pinMode(trigger,OUTPUT);
pinMode(LogicX1,OUTPUT);
pinMode(LogicX2,OUTPUT);
}
void loop(){
unsigned long Last_Time_Update = 0; // last
time update
long Duration = 6000; // interval at which
to do something (milliseconds) (5sec)
long Time;
long distance; // defining the terms
distance and time as long integer
unsigned long Present_Time = millis();
delay(10000); //delay by 10 seconds
if(Present_Time < Duration){
Go_Forward();
}
if(Present_Time
- Last_Time_Update > Duration) {
Last_Time_Update = Present_Time;
Back(2000);
Turn_Left(1000);
Forward(1000);
}
}
void Go_Forward()
{
Serial.println("The AUV is going
forward!");
digitalWrite(LogicR1,LOW); // H-bridge configuration for the AUV to go
forward
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);
}
void Forward(int Enter_time)
{
Serial.println("The AUV is going
forward!");
digitalWrite(LogicR1,LOW); // H-bridge configuration for the AUV to go
forward
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);
delay(Enter_time);
//
H-bridge configuration for the AUV to go forward
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL2,LOW);
}
void STOP()
{
Serial.println ("STOPPPPP!!!");
// H-bridge configuration for the AUV to go stop
digitalWrite(LogicR1,LOW);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,LOW);
}
void Turn_Right(int Enter_Time)
{
Serial.println ("The AUV turns
right"); // H-bridge configuration for the AUV to go right
digitalWrite(LogicR1,LOW);
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,HIGH);
digitalWrite(LogicL2,LOW);
delay(Enter_Time);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
}
void Turn_Left(int Enter_Time)
{
Serial.println ("The AUV turns
left");
digitalWrite(LogicR1,HIGH); // h-bridge pin
configuration for turning left
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);
delay(Enter_Time); // once the time is
entered, the AUV turns left for that many seconds.
digitalWrite(LogicR1,LOW); // Stops the
motor after turning left.
digitalWrite(LogicL2,LOW);
}
void Back(int Enter_Time)
{
Serial.println ("The AUV is going
back"); // H-bridge configuration
for the AUV to go backwards
digitalWrite(LogicR1,HIGH);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,HIGH);
digitalWrite(LogicL2,LOW);
delay(Enter_Time);
digitalWrite(LogicR1,LOW);
digitalWrite(LogicL1,LOW);
}
No comments:
Post a Comment