Short Description: This is a prototype of a robotic fishing boat created with a partner using Arduino and by 3D printing parts of it. It’s a very basic version of what could be a more refined and complex iteration.
Code:
#include
#include
#define sensorIRL A0
#define sensorIRR A1
#define sensorbot A2
//#define sensorIRR A1
Servo myservo;
Ultrasonic ultrasonic(8);
// constants won't change. They're used here to
// set pin numbers:
const int motor = 11;
const int motor2 = 10;
int pos = 0;
//float sensorValueL, inchesL;
//float sensorValue2;
//int front_sensor = ultrasonic.RangeInCentimeters;
void setup() {
Serial.begin(9600);
myservo.attach(9);
pinMode(motor, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(sensorIRL, INPUT);
pinMode(sensorIRR, INPUT);
pinMode(sensorbot, INPUT);
}
void loop(){
ultrasonic.MeasureInCentimeters();
Serial.print("Front 1: ");
Serial.println(ultrasonic.RangeInCentimeters);
//delay(500);
if (ultrasonic.RangeInCentimeters < = 20) {
digitalWrite(motor, LOW);
digitalWrite(motor2, LOW);
uint16_t value = analogRead(sensorIRL);
double distanceL = get_IR_L (value);
Serial.print("First Left: ");
Serial.println(distanceL);
uint16_t value2 = analogRead(sensorIRR);
double distanceR = get_IR_R (value2);
Serial.print("First Right: ");
Serial.println(distanceR);
if (distanceL <= 10){
digitalWrite(motor2, HIGH);
digitalWrite(motor, LOW);
Serial.println("go right!");
//delay(500);
uint16_t value = analogRead(sensorIRL);
double distanceL = get_IR_L (value);
Serial.print("Left: ");
Serial.println(distanceL);
}
if (distanceR <= 10){
digitalWrite(motor2, LOW);
digitalWrite(motor, HIGH);
Serial.println("go left!");
//delay(500);
uint16_t value2 = analogRead(sensorIRR);
double distanceR = get_IR_R (value2);
Serial.print("Right: ");
Serial.println(distanceR);
}
ultrasonic.MeasureInCentimeters();
Serial.print("Front 2: ");
Serial.println(ultrasonic.RangeInCentimeters);
//delay(500);
fish();
} else {
digitalWrite(motor, HIGH);
digitalWrite(motor2, HIGH);
}
delay(500);
}
void fish(){
uint16_t value3 = analogRead(sensorIRR);
double distancebot = get_IR_bot (value3);
Serial.print("Bottom: ");
Serial.println(distancebot);
if (distancebot <= 12){
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
//return distance (cm)
double get_IR_L (uint16_t value) {
if (value < 16) value = 16;
return 2076.0 / (value - 11.0);
}
double get_IR_R (uint16_t value2) {
if (value2 < 16) value2 = 16;
return 2076.0 / (value2 - 11.0);
}
double get_IR_bot (uint16_t value3) {
if (value3 < 16) value3 = 16;
return 2076.0 / (value3 - 11.0);
}