Our code involved using logic gates to activate and control spatula movement based on different key timings. Using the load cell hooked up to the Arduino, we constantly weigh the chicken as it is cooking in order to monitor the percent weight decrease.
Once the chicken is a certain percent of its initial weight (perfectly cooked on one side), we send signals to the stepper motors to move the spatula forward.
As the spatula moves, the IR sensor moves with it. Once the IR sensor detects the chicken, we send signals to the servo motors to flip the chicken onto its other side. The spatula then returns to its original position.
Once again, we constantly weigh the percent weight decrease of the new side of the chicken until the chicken is fully cooked. Once the weight passes this threshold, the serial monitor prints that the chicken is done.
We utilized the Stepper Motor's ability to count the number of steps it has taken in order to allow the user to place the chicken breast anywhere he wants on the griddle.
The following Arduino code was used to cook the chicken:
#include <Stepper.h>
#include <Servo.h>
#include <C:\Users\Sun\Desktop\FINAL_CODE_ME102B\HX711.h>
HX711 scale(A1, A0); //initialize load cell
const int stepsPerRevolution = 200; //number of steps per revolution for our stepper motor
const int ir_pin = 4; //digital pin number for IR sensor
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12); // initialize the stepper library on pins 9 through 12:
Stepper myStepper2(stepsPerRevolution, 2, 7, 8, 13); //initialize second stepper motor
Servo myservo1; //initialize servo motor 1
Servo myservo2; //initialize servo motor 2
int stepCount = 0; // number of steps the motor has taken
int irCount = 0; //number of times IR changes from 1 (no detection) to 0 (detection)
int flipCount = 0; //number of times the spatula flipped
int weightCount; //number of times weight passes a threshold
int sensor1 = 1; //sensor variable starts out as "no detection"
int pos = 0; //servo position variable
long initialWeight; //initial weight of raw chicken breast
void setup()
{
Serial.begin(9600);
pinMode (ir_pin, INPUT); //initialize IR sensor
myservo1.attach(3); //attach servo1 to pin 6
myservo2.attach(6); //attach servo2 to pin 3
myservo1.write(240); //set position of servos
myservo2.write(0);
initialWeight = ((scale.get_value())-7774041)/16427.0f*162; //scale the weight on the load cell to grams
Serial.print("initial weight: ");
Serial.println(initialWeight);
weightCount = 0;
}
void loop()
{
if (weightCount == 0) //if the chicken is still raw
{
Serial.println(initialWeight); //print the initial weight of the chicken
Serial.print("current weight: ");
long currentWeight = ((scale.get_value())-7774041)/16427.0f*162; //keep updating the current weight of the chicken in grams
Serial.println(currentWeight);
if (currentWeight < .8*initialWeight) //when the first side of the chicken is fully cooked
{
weightCount++; //update the weight count
}
}
int sensor2 = digitalRead(ir_pin); //continuously read the ir sensor
if (irCount == 0) //if the ir sensor hasnt detected the end of the chicken yet and the chicken is cooked on one side
{
myStepper.step(-1); //move the motor towards the chicken
myStepper2.step(-1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++; //keep track of the number of steps taken
delay(5); //milliseconds between each step
}
if (sensor2 != sensor1) //when the current read of the ir sensor has detection
{
irCount++; //update the ir count
sensor1 = sensor2; //set the value of sensor1 to the current read of the ir sensor
}
if (irCount == 1) //when the ir sensor passes through the chicken twice
{
flipCount++;
delay(2000);
for (pos = 0; pos < 300 ; pos++)
{
myStepper.step(-1);
myStepper2.step(-1);
delay(5);
}
for (pos = 0; pos < 180; pos++) //move both servos
{
myservo1.write(pos);
myservo2.write(-pos+180);
delay(20);
}
for (pos = 180; pos > 180; pos--)
{
myservo1.write(pos);
myservo2.write(-pos+180);
delay(20);
}
delay(2000);
myservo1.detach();
myservo2.detach();
}
if (flipCount == 1) //once the servos are done flipping the chicken
{
long newInitialWeight = ((scale.get_value())-7774041)/16427.0f*162; //new weight of the flipped chicken is recorded
stepCount = stepCount + 300;
for (pos = 0; pos < stepCount; pos++)
{
myStepper.step(1);
myStepper2.step(1);
delay(5);
}
stepCount = 0;
irCount = 0;
flipCount = -1;
if ((scale.get_value()-7774041)/16427.0f*162 < 0.85*newInitialWeight) //if current weight of flipped chicken is less than 85% of its initial weight
{
//make a buzzing noise and/or light up the LED
Serial.println("your chicken is cooked!");
irCount = 10; //number of times IR changes from 1 (no detection) to 0 (detection)
flipCount = 10; //number of times the spatula flipped
weightCount= 10; //number of times weight passes a threshold
}
}
}