정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

코드 합치기 질문드립니다.
김형준 | 2016-11-28

#include  

Servo servo1;  // create servo object to control a servo 
Servo servo2;  // create servo object to control a servo 

int lastValue = 0;
int sensorValue =0;
float difference=0;
float filtered=0;//             마이크 
int a =1;

unsigned long previousMillis = 0; 

int pos1;      // angle of servo 1
int pos2;      // angle of servo2

void setup() 

  

  Serial.begin(9600);
 servo1.attach(10);  // attaches the servo on pin 9 to the servo object 
 servo2.attach(11);  // attaches the servo on pin 10 to the servo object 
  servo2.write(240);
     servo1.write(-40);

void loop() 


unsigned long currentMillis = millis();
lastValue = sensorValue;
sensorValue=analogRead(A0);
difference =abs(lastValue - sensorValue);
filtered = filtered*0.9 + difference *0.1;
Serial.println(filtered/2);
 
    if (currentMillis - previousMillis > 1000) {
    previousMillis = currentMillis;
   if(filtered/2>=20)  //2.8~3
   {a=a*-1;} 
    
   switch(a){
     case -1:
     servo2.write(-40);
     servo1.write(240);
     break;
     
     case 1:
     servo2.write(240);
     servo1.write(-40);
     break;

   }

}
}

1 위는 마이크 받으면 서보 두대가 동시에 돌아가는 코드이고

 

#include
#include


#define echoPin1 7 // Echo Pin
#define trigPin1 8 // Trigger Pin
//#define echoPin2 5// Echo Pin
//#define trigPin2 6 // Trigger Pin

int vol = 230;

unsigned long previousMillis = 0;
const long interval = 1000;


unsigned long preMillis = 0;

long duration1, distance1; // Duration used to calculate distance
long duration2, distance2; // Duration used to calculate distance


void setup()
{
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  //pinMode(trigPin2, OUTPUT);
  //pinMode(echoPin2, INPUT);

  // put your setup code here, to run once:
  mp3.Init(3, 2);
  mp3.setVolume(vol);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > 50) {
    previousMillis = currentMillis;
    digitalWrite(trigPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);
    duration1 = pulseIn(echoPin1, HIGH);
    //Calculate the distance (in cm) based on the speed of sound.
    distance1 = duration1 / 58.2;

    Serial.print("distance1=");
    Serial.println(distance1);
    Serial.println(mp3.getName());
    Serial.println(vol);
    
    if (distance1 <= 10 ) {
      //mp3.PlayTrack(3);
      if (mp3.getName() != "003.mp3") {
        vol-=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
      }
      else if (mp3.getName() = "003.mp3") {
        vol+=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
      }
      if (vol == 189) {
        mp3.Stop();
        mp3.PlayTrack(3);
       vol = 195;
      
        
      }
    }
    else if (distance1 > 10 && distance1 <= 20) {
      
      //mp3.PlayTrack(1);
      if (mp3.getName() != "002.mp3") {
        vol-=2;
        vol = constrain(vol, 189, 230);
        mp3.setVolume(vol);
      }
       else if (mp3.getName() = "002.mp3") {
        vol+=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
       }
      if (vol == 189) {
        mp3.Stop();
        mp3.PlayTrack(1);
        vol = 195;
      }
    }
    else if (distance1 > 20) {
      
      //mp3.PlayTrack(2);
      if (mp3.getName() != "001.mp3") {
        vol-=2;
        vol = constrain(vol, 189, 230);
        mp3.setVolume(vol);
      }
       else if (mp3.getName() = "001.mp3") {
        vol+=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
       }
      if (vol == 189) {
        mp3.Stop();
        mp3.PlayTrack(2);
        vol = 195;
      }
    }

  }


  // put your main code here, to run repeatedly:
}

2 초음파센서받고 mp3재생하는 코드를 각각 작성해서

 

#include  
#include
#include
#define echoPin1 7 // Echo Pin
#define trigPin1 8 // Trigger Pin


Servo servo1;  // create servo object to control a servo 
Servo servo2;  // create servo object to control a servo 

int vol = 230;
int lastValue = 0;
int sensorValue =0;
float difference=0;
float filtered=0;//             마이크 
int a =1;

unsigned long previousMillis = 0; 
const long interval = 1000;
int pos1;      // angle of servo 1
int pos2;      // angle of servo2

long duration1, distance1; // Duration used to calculate distance
long duration2, distance2; // Duration used to calculate distance


void setup() 

  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  mp3.Init(3, 2);
  mp3.setVolume(vol);
  
 servo1.attach(10);  // attaches the servo on pin 9 to the servo object 
 servo2.attach(11);  // attaches the servo on pin 10 to the servo object 
  servo2.write(240);
     servo1.write(-40);

void loop() 


unsigned long currentMillis = millis();
lastValue = sensorValue;
sensorValue=analogRead(A0);
difference =abs(lastValue - sensorValue);
filtered = filtered*0.9 + difference *0.1;
Serial.println(filtered/2);

 if (currentMillis - previousMillis > 50) {
    previousMillis = currentMillis;
    digitalWrite(trigPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);
    duration1 = pulseIn(echoPin1, HIGH);
    //Calculate the distance (in cm) based on the speed of sound.
    distance1 = duration1 / 58.2;

    Serial.print("distance1=");
    Serial.println(distance1);
    Serial.println(mp3.getName());
    Serial.println(vol); 
 }

  
 
    if (currentMillis - previousMillis > 1000) {
    previousMillis = currentMillis;
   if(filtered/2>=20)  //2.8~3
   {a=a*-1;} 
    
   switch(a){
     case -1:
     servo2.write(-40);
     servo1.write(240);

     //unsigned long currentMillis = millis();

    while(a=-1){

   
    
    if (distance1 <= 10 ) {
      //mp3.PlayTrack(3);
      if (mp3.getName() != "003.mp3") {
        vol-=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
      }
      else if (mp3.getName() = "003.mp3") {
        vol+=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
      }
      if (vol == 189) {
        mp3.Stop();
        mp3.PlayTrack(3);
       vol = 195;
      
        
      }
    }
    else if (distance1 > 10 && distance1 <= 20) {
      
      //mp3.PlayTrack(1);
      if (mp3.getName() != "002.mp3") {
        vol-=2;
        vol = constrain(vol, 189, 230);
        mp3.setVolume(vol);
      }
       else if (mp3.getName() = "002.mp3") {
        vol+=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
       }
      if (vol == 189) {
        mp3.Stop();
        mp3.PlayTrack(1);
        vol = 195;
      }
    }
    else if (distance1 > 20) {
      
      //mp3.PlayTrack(2);
      if (mp3.getName() != "001.mp3") {
        vol-=2;
        vol = constrain(vol, 189, 230);
        mp3.setVolume(vol);
      }
       else if (mp3.getName() = "001.mp3") {
        vol+=2;
        vol = constrain(vol,189 , 230);
        mp3.setVolume(vol);
       }
      if (vol == 189) {
        mp3.Stop();
        mp3.PlayTrack(2);
        vol = 195;
      }
    }
    }
    
 
     
     if(a=1){break;}
     
     case 1:
     servo2.write(240);
     servo1.write(-40);
     break;

   }
 

}
}

두 코드를 합친건데 센서를 읽어도 모터가 하나만 돌아가거나 작동을 안하는데 어디가 잘못된 건지 모르겠습니다.

 

어디가 잘못된건지 조언 좀 부탁드립니다.

이전글   |    스텝모터 제어시 궁금한 점이 있습니다. ... 2016-11-27
다음글   |    오렌지보드 가르쳐주셈 ㅠㅠㅠ 2016-11-28