Back to Parent

Servo mainServo;
int soundSensorPin1 = A1;
int soundSensorValue1 = 0;
int soundSensorPin2 = A2;
int soundSensorValue2 = 0;
int servoPos = 0;
int servoPin = A3;
int newPos;
int sensor1Min=999999;
int sensor1Max=0;
int sensor2Min=999999;
int sensor2Max=0;

void setup() {
    pinMode( soundSensorPin1, INPUT);
    Particle.variable("sound_amount_1", &soundSensorValue1, INT);
    
    pinMode( soundSensorPin2, INPUT);
    Particle.variable("sound_amount_2", &soundSensorValue2, INT);
    
    Particle.variable("position_angle", &newPos, INT);
    
    //servo code
    mainServo.attach( A3 ); //attached the servo to analog 3
    mainServo.write(90); //set Servo to 90, the middle
    delay(100);

}

void loop() {
    
    int read1_1 = analogRead( soundSensorPin1 );
    int read1_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read1_1>=sensor1Max){
        sensor1Max = read1_1;
    }
    if (read1_2>=sensor2Max){
        sensor2Max = read1_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read1_1<=sensor1Min){
        sensor1Min = read1_1;
    }
    if (read1_2<=sensor2Min){
        sensor2Min = read1_2;
    }
    delay(10);
    int read2_1 = analogRead( soundSensorPin1 );
    int read2_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read2_1>=sensor1Max){
        sensor1Max = read2_1;
    }
    if (read2_2>=sensor2Max){
        sensor2Max = read2_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read2_1<=sensor1Min){
        sensor1Min = read2_1;
    }
    if (read2_2<=sensor2Min){
        sensor2Min = read2_2;
    }
    delay(10);
    int read3_1 = analogRead( soundSensorPin1 );
    int read3_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read3_1>=sensor1Max){
        sensor1Max = read3_1;
    }
    if (read3_2>=sensor2Max){
        sensor2Max = read3_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read3_1<=sensor1Min){
        sensor1Min = read3_1;
    }
    if (read3_2<=sensor2Min){
        sensor2Min = read3_2;
    }
    delay(10);
    int read4_1 = analogRead( soundSensorPin1 );
    int read4_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read4_1>=sensor1Max){
        sensor1Max = read4_1;
    }
    if (read4_2>=sensor2Max){
        sensor2Max = read4_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read4_1<=sensor1Min){
        sensor1Min = read4_1;
    }
    if (read4_2<=sensor2Min){
        sensor2Min = read4_2;
    }
    delay(10);
    int read5_1 = analogRead( soundSensorPin1 );
    int read5_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read5_1>=sensor1Max){
        sensor1Max = read5_1;
    }
    if (read5_2>=sensor2Max){
        sensor2Max = read5_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read5_1<=sensor1Min){
        sensor1Min = read5_1;
    }
    if (read5_2<=sensor2Min){
        sensor2Min = read5_2;
    }
    delay(10);
    int read6_1 = analogRead( soundSensorPin1 );
    int read6_2 = analogRead( soundSensorPin2 );
     //maximum check for sensor 1 and sensor 2
    if (read6_1>=sensor1Max){
        sensor1Max = read6_1;
    }
    if (read6_2>=sensor2Max){
        sensor2Max = read6_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read6_1<=sensor1Min){
        sensor1Min = read6_1;
    }
    if (read6_2<=sensor2Min){
        sensor2Min = read6_2;
    }
    delay(10);
    int read7_1 = analogRead( soundSensorPin1 );
    int read7_2 = analogRead( soundSensorPin2 );
      //maximum check for sensor 1 and sensor 2
    if (read7_1>=sensor1Max){
        sensor1Max = read7_1;
    }
    if (read7_2>=sensor2Max){
        sensor2Max = read7_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read7_1<=sensor1Min){
        sensor1Min = read7_1;
    }
    if (read7_2<=sensor2Min){
        sensor2Min = read7_2;
    }
    delay(10);
    int read8_1 = analogRead( soundSensorPin1 );
    int read8_2 = analogRead( soundSensorPin2 );
      //maximum check for sensor 1 and sensor 2
    if (read8_1>=sensor1Max){
        sensor1Max = read8_1;
    }
    if (read8_2>=sensor2Max){
        sensor2Max = read8_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read8_1<=sensor1Min){
        sensor1Min = read8_1;
    }
    if (read8_2<=sensor2Min){
        sensor2Min = read8_2;
    }
    delay(10);
    int read9_1 = analogRead( soundSensorPin1 );
    int read9_2 = analogRead( soundSensorPin2 );
         //maximum check for sensor 1 and sensor 2
    if (read9_1>=sensor1Max){
        sensor1Max = read9_1;
    }
    if (read9_2>=sensor2Max){
        sensor2Max = read9_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read9_1<=sensor1Min){
        sensor1Min = read9_1;
    }
    if (read9_2<=sensor2Min){
        sensor2Min = read9_2;
    }
    delay(10);
    int read10_1 = analogRead( soundSensorPin1 );
    int read10_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read10_1>=sensor1Max){
        sensor1Max = read10_1;
    }
    if (read10_2>=sensor2Max){
        sensor2Max = read10_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read10_1<=sensor1Min){
        sensor1Min = read10_1;
    }
    if (read10_2<=sensor2Min){
        sensor2Min = read10_2;
    }
    delay(10);
    int read11_1 = analogRead( soundSensorPin1 );
    int read11_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read11_1>=sensor1Max){
        sensor1Max = read11_1;
    }
    if (read11_2>=sensor2Max){
        sensor2Max = read11_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read11_1<=sensor1Min){
        sensor1Min = read11_1;
    }
    if (read11_2<=sensor2Min){
        sensor2Min = read11_2;
    }
    delay(10);
    int read12_1 = analogRead( soundSensorPin1 );
    int read12_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read12_1>=sensor1Max){
        sensor1Max = read12_1;
    }
    if (read12_2>=sensor2Max){
        sensor2Max = read12_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read12_1<=sensor1Min){
        sensor1Min = read12_1;
    }
    if (read12_2<=sensor2Min){
        sensor2Min = read12_2;
    }
    delay(10);
    int read13_1 = analogRead( soundSensorPin1 );
    int read13_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read13_1>=sensor1Max){
        sensor1Max = read13_1;
    }
    if (read13_2>=sensor2Max){
        sensor2Max = read13_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read13_1<=sensor1Min){
        sensor1Min = read13_1;
    }
    if (read13_2<=sensor2Min){
        sensor2Min = read13_2;
    }
    delay(10);
    int read14_1 = analogRead( soundSensorPin1 );
    int read14_2 = analogRead( soundSensorPin2 );
        //maximum check for sensor 1 and sensor 2
    if (read14_1>=sensor1Max){
        sensor1Max = read14_1;
    }
    if (read14_2>=sensor2Max){
        sensor2Max = read14_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read14_1<=sensor1Min){
        sensor1Min = read14_1;
    }
    if (read14_2<=sensor2Min){
        sensor2Min = read14_2;
    }
    delay(10);
    int read15_1 = analogRead( soundSensorPin1 );
    int read15_2 = analogRead( soundSensorPin2 );
    //maximum check for sensor 1 and sensor 2
    if (read15_1>=sensor1Max){
        sensor1Max = read15_1;
    }
    if (read15_2>=sensor2Max){
        sensor2Max = read15_2;
    }
    //minimum check for sensor 1 and sensor 2
    if (read15_1<=sensor1Min){
        sensor1Min = read15_1;
    }
    if (read15_2<=sensor2Min){
        sensor2Min = read15_2;
    }
    
    int average1 = (((read1_1)+(read2_1)+(read3_1)+(read4_1)+(read5_1)+(read6_1)+(read7_1)+(read8_1)+(read9_1)+(read10_1)+(read11_1)+(read12_1)+(read13_1)+(read14_1)+(read15_1))/15);
    int average2 = (((read1_2)+(read2_2)+(read3_2)+(read4_2)+(read5_2)+(read6_2)+(read7_2)+(read8_2)+(read9_2)+(read10_2)+(read11_2)+(read12_2)+(read13_2)+(read14_2)+(read15_2))/15);
    //note that we could have subtracted the min and max of each sensor set, then divided by 13 to try and weed out bad values. But I found this hurts more than helps, so left it out.
    //but the variables are already created and used. Just type them in.
    
    soundSensorValue1 = average1;
    soundSensorValue2 = average2;
    
    if(soundSensorValue1 > 55 && soundSensorValue2 > 55){
    
    //note that 1 is left and 2 is R. The larger the number, the more left to go.
    int LR_Value = (soundSensorValue1+7) - soundSensorValue2;
    
   
   
   //ifs with the newPos categories
   if(LR_Value>=40){
       newPos = 15;
   }
   else if (LR_Value>=25){
       newPos = 55;
   }
   else if (LR_Value>=5){
       newPos = 90;
   }
   else if (LR_Value>=-10){
       newPos = 90;
   }
   else if (LR_Value>=-30){
       newPos = 120;
   }
   else if (LR_Value>=-45){
       newPos = 150;
   }
   else{
       newPos = 165;
   }
   
    
    //send the servo to that spot
    mainServo.write(newPos);
    
    delay(3000);
    }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

1