BASIC4MCU | 질문게시판 | 코딩을 합치고 싶습니다.
페이지 정보
작성자 Norway 작성일2019-05-30 18:48 조회13,070회 댓글3건본문
1번째 동작 = .초음파 + 금속감지 + 서보모터(180도) 제어하는 코딩과
2번째 동작 = (전압변화를 이용해 소리측정 -> 볼트값을 이용해서 0.5보다 크면 서보모터를 정회전 한바퀴 -> 원위치 /
0.5보다 작으면 역회전 한바퀴 -> 원위치
합치려고합니다.
1번 동작을 마친후에 2번동작이 이루어지게 하려고 합니다.
--------------------------------------------------
#include <Servo.h> // servo motor header
Servo firstServo; // servo motor at the entrance of machine
int trigPin = 12; // trigger pin of ultrasonic sensor
int echoPin = 7; // echo pin of ultrasonic sensor
int CDS = A0; // light sensor pin
int motorPin = 9; // motor pin
int pos = 90; // servo motor initial pos
int light = 0; // ligth value
int go = 0; // Matlab send parameter
int BAUD_RATE = 9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_RATE);
pinMode(trigPin,OUTPUT); // set trig pin into OUTPUT mode
pinMode(echoPin,INPUT); // set echo pin into INPUT mode
pinMode(CDS,INPUT); // set light sensor pin into INPUT mode
firstServo.attach(motorPin); // attach motor pin
}
void loop() {
// put your main code here, to run repeatedly:
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin,HIGH); // measure time while echoPin receives HIGH signal
distance=((float)(340 * duration) / 1000) / 2; // convert time unit into distance unit
Serial.print(duration);
Serial.print("\nDIstance:");
Serial.print(distance);
Serial.println("mm\n");
delay(1000);
// separate metal
if(distance <= 150) { // when the garbage enters the entrance
light = analogRead(CDS); // read light from metal detector
if(light >= 123) { // when metal detector works
for(pos = 90; pos > 0; pos--) {
firstServo.write(pos); // move 1st servo motor to left
delay(10);
}
delay(1000); // wait the garbage falling
pos = 90; // set the servo motor to initial pos
firstServo.write(pos);
}
// if metal detector does not work
else {
firstServo.write(pos);
Serial.println(go); // send paramter to Matlab
for(pos = 90; pos < 180; pos++) {
firstServo.write(pos); // move 1st servo motor to right
delay(10);
}
delay(1000); // wait the garbage falling
pos = 90; // set the servo motor to initial pos
firstServo.write(pos);
}
}
}
--------------------------------------------------------------------------------------------- 동작 1을 제어하는 코딩
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
Serial.println(volts);
}
--------------------------------------------------------------------- 소리의 크기를 전압변화 으로 나타내주는 코딩
1번째 동작은 잘 제어가 됩니다.
2번째 동작은 처음에는 Sensor Value 값으로 제어를 하려고했는데 센서값이 잘 안나와서
전압변화를 이용하는 방식으로 코딩을 짜려고 합니다 .
그 다음 1번째와 2번째를 코딩을 합쳐서 1번째 동작후에 2번째 동작을 실행하도록 만들려고 하는데
한국에서 코딩 찾아보고 조금만 수정을해서 사용하는데 문제가없었는데
현제 해외에 와서 하려고 하다보니 여러가지 상황이 겹치다 보니까 너무 어렵습니다 .
기초를 탄탄하게 다지고 아두이노를 사용했어야했는데 제 착오였습니다.. 정말 마지막으로 한번만 도움 부탁드리겠습니다.
꾸짖음도 달게 받겠습니다.
댓글 3
조회수 13,070master님의 댓글
master 작성일
원하는대로 제어가 안됩니다.
loop문은 반복적으로 실행되는 함수인데요
1번째 동작(초음파 + 금속감지)
2번째 동작(전압)
1번째 동작으로 서보모터가 어느 각도로 이동한다고하면
2번째 동작에서는 다른 각도로 갈 수가 있습니다.
반복해서 실행 되므로 1번 제어에 의한 각도와 2번 제어에 의한 각도 사이를 오락가락하게 됩니다.
알고리즘부터 확실히 만드세요
모든 경우의 수를 고려해서 디테일하게 작성해야합니다.
Norway님의 댓글
Norway 작성일
선생님 그러면 아두이노 우노 2대를 사용해서
2번 동작을 따로만든다면 어떻게 되나요?
master님의 댓글
master
뭘 하려는지 제가 정확하게 알지 못합니다.
절 이해시켜야지 이런저런 조언을 드릴 수 있습니다.