Fishing!

Our goal was to create an interaction between a fisherman and a fish. The fisherman would tease the fish with bait, moving it up and down. Once the bait falls into the fishes mouth, the fish sees the bait and closes it’s mouth around it. The fisherman then sees that the bait has been taken and pulls in the line until the fish is all the way up. It continues to fight the fish, moving its rod up and down as it reels it in. It didnt go quite as planned, there were a lot of mishaps along the way and some of the interactions are pretty particular so it was hard to get everything to work at once.

Code for Fisherman:

#include <Servo.h&gt; 

Servo myservo;

void setup() 
{ 
  myservo.attach(3);
  Serial.begin(9600);
  myservo.write(170);  
  pinMode(A0,INPUT);
  pinMode(A1, INPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
} 

float sampleA1(){
  float sum = 0;
  for(int i = 0;i< 20; i++){
    sum += analogRead(A1);
    delay(10);
  }
  return sum/20;
}

float sampleA0(){
  float sum = 0;
  for(int i = 0;i< 20; i++){
    sum += analogRead(A0);
    delay(10);
  }
  return sum/20;
}

void checkServo(){
  Serial.println(myservo.read());
  if (myservo.read() < 137){
    myservo.write(170);
  }
  if (myservo.read() &gt; 167){
      myservo.write(135);

  }
}
void loop() {
  static int state= 0;
  
  if (sampleA1() < 380 &amp;&amp; state == 0){
    while (sampleA0() &gt; 800){
      digitalWrite(11, HIGH);
      analogWrite(10, 50);
    }
    state = 1;
    Serial.println("done");
    digitalWrite(11,LOW);
    digitalWrite(10,LOW);
  
  }
  
} 

Code for Fish:

#include <Servo.h&gt;

Servo mouth_svo;

const int SENSOR_PIN = A0;

const int THRESHOLD = 400;
const int open = 55;
const int closed = 85;

int servo_pos = open;

bool is_closed = false;
int start_closed_time = 0;

int trigger_close = millis();

void setup() {
  mouth_svo.attach(9);
  Serial.begin(9600);
  pinMode(SENSOR_PIN, INPUT);
}

void loop() {
  short val = analogRead(SENSOR_PIN);
  Serial.println(val);

  if (val &gt; THRESHOLD &amp;&amp; !is_closed) {
    servo_pos = closed;
    is_closed = true;
    start_closed_time = millis();
  }
    
  if (is_closed) {
//    Serial.println(millis() - start_closed_time);
    if (millis() - start_closed_time &gt;= 10000) {
      servo_pos = open;
      is_closed = false;
    }
  
  mouth_svo.write(servo_pos);
  delay(5);
  }
}

Cad Files

https://drive.google.com/drive/folders/1hl5-t826zmA_N-bGbYctw8gfsnhVIWne?usp=sharing