Preganncy Test Arduino Code

		
/* Design and Professional Practice 2 */
/* Engineering Design of a Pregnancy Test for Visually Impaired Users */

/* PCB Arduino source code - June 10th*/
/* Group 10 (2020-21) */

#include< Servo.h >

// Button 
const int buttonPin = 5;
int buttonState = 0;

// Buzzer - high pitch for postive, low pitch for negative
int buzzerPin = 10;
#define NOTE_C4  262
int melody[] = {NOTE_C4};
int noteDurations[] = {4, 4, 4, 4, 4, 4, 4, 4};

// Servo Motor
Servo myservo;
int motorPin = 4;

// 4 phototransistor pins
int sensorPin1 = 0;
int sensorPin2 = 1;
int sensorPin3 = 2;
int sensorPin4 = 3;

int transistorValue1 = 0;
int transistorValue2 = 0;

// Result (0 for invalid, 1 for negative, 2 for positive)
int result = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(motorPin);
  myservo.write(90); //move to neutral position at start
  
  int sum1 = 0; 
  int sum2 = 0;
  int diff;
  
  // adds up the values of the signals (how much light hits the phototransistors) over a 0.5 seconds
  for (int i = 0; i < 500; i++) 
  {  
    //Each pair of phototransistors are under the same line
    transistorValue1 = analogRead(sensorPin1)+ analogRead(sensorPin2); 
    transistorValue2 = analogRead(sensorPin3)+ analogRead(sensorPin4);
    sum1 = transistorValue1/50 + sum1;
    sum2 = transistorValue2/50 + sum2;
    delay(1); 
  }

  // determining the result
  diff = abs(sum1-sum2); // calculate the difference between sum1 and sum2 

  if(diff > 450) // for negative result
  { 
    result = 1;
  } 
  else { 		// for positive result
    result = 2;
  }

  // Motor output
  if (result == 2){
        myservo.write(0);   //motor for positive result
  } else if (result == 1){
        myservo.write(180); //motor for negative result
  } else { //invalid
    myservo.write(0);
  }
}

// Buzzer output
void loop()
{
  // read the state of the button
  buttonState = analogRead(buttonPin);
  if (buttonState > 950) {
      //when button pressed
      if (result == 2)
      {
        //sound for positive result
        tone (buzzerPin,1568,1000);
      }
      if (result == 1)
      {
        //sound for negative result
        for (int thisNote = 0; thisNote < 8; thisNote++) 
        {
          int noteDuration = 1000 / noteDurations[thisNote];
          tone(buzzerPin, melody[thisNote], noteDuration);
          int pauseBetweenNotes = noteDuration * 3;
          delay(pauseBetweenNotes);
          noTone(buzzerPin);
        }
      }
  }
}