정보나눔

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

Dc 모터 속도제어
jjjj | 2017-06-15

dc모터의 속도를 128이하로 떨어트리기만 하면 모터가 동작하지 않는데 어떻게 바꿔야 속도80으로 모터가 돌아가나요!ㅜㅜ

 

 

 

#include <SoftwareSerial.h>
#include <Servo.h>

//bluetooth
#define BT_TXD 2
#define BT_RXD 3
SoftwareSerial BTSerial(BT_TXD, BT_RXD);

// dust_sensor
int pin = 7;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float pcsPerCF = 0;
float ugm3 = 0;
float ratio = 0;
float concentration = 0;

// servo_motor
int servoPin = 6; 
Servo servo;   
int angle = 0; // servo position in degrees

//////////////////////////////////////////
void setup() 
{
  // bluetooth__dust_sensor
  Serial.begin(9600);
  BTSerial.begin(9600);
  pinMode(7, INPUT);
  starttime = millis();//get the current time;

  // rainsensor
  pinMode(4, OUTPUT);

  // servo_motor
  servo.attach(servoPin); 

  // dc_motor
   pinMode(8, OUTPUT);//right
   pinMode(9, OUTPUT);//inverse

}
////////////////////////////////////////////

void loop() 
  {
  // bluetooth to signal monitor  
  if(BTSerial.available())
  {
    Serial.write(BTSerial.read());    
  }
  // signal monitor to bluetooth
  if(Serial.available())
  {
    BTSerial.write(Serial.read());
  }
   
  // dust_sensor
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy + duration;
  if ((millis() - starttime) > sampletime_ms) //if the sampel time == 30s
  {
    ratio = lowpulseoccupancy / (sampletime_ms * 10.0); // Integer percentage 0=>100
    concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve
    pcsPerCF = concentration * 100;
    ugm3 = pcsPerCF / 13000;
    Serial.println("   "); // enter empty space
    Serial.print(ugm3); // ugm3 data output
    lowpulseoccupancy = 0;
    starttime = millis();
  }


if(BTSerial.available())
{
  char data=BTSerial.read();
  Serial.write(data);

  if(data=='1') //closed
  {
    angle = 85;
    servo.write(angle); 
    delay(50);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    analogWrite(10, 100);
     delay(10000);
  }
  
  else if(data=='0') //open!
  {
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    analogWrite(10, 100);
    delay(10000);
    angle = 0;
    servo.write(angle); 
    delay(10000);
  }
  if (Serial.available())  BTSerial.write(Serial.read());
  }

if(analogRead(0)>500 & angle==0) // 1st
// no rain (open!! make window closed or stay_ following dust)
{
    // no rain
    Serial.println("nr");
    BTSerial.print("nr");//signal

    if (ugm3>30)
   {
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    analogWrite(10, 60);
    delay(10000);//window closed
    angle = 85;
    servo.write(angle); 
    delay(50); 

    // very bad dust (make window closed)
    if (ugm3>80) 
    {
     Serial.println("bd");
     BTSerial.print("bd");
     delay(50);
     } 

     // bad dust (make window closed)
     else 
     {
     Serial.println("nd");
     BTSerial.print("nd");
     delay(50);
     }
   }

   // good dust (keep open)
    else 
    {
    Serial.println("gd");
    BTSerial.print("gd");
    angle = 0;
    servo.write(angle); 
    delay(50);
    }
}


else if(analogRead(0)>500 & angle==85) // 2nd
// no rain (closed!! make window stay or open_ following dust)
{
   // no rain
   Serial.println("nr");
   BTSerial.print("nr");

   // very bad dust (Keep window closed)
   if (ugm3>80) 
   {
    Serial.println("bd");
    BTSerial.print("bd");
    delay(50);
    
    angle = 85;
    servo.write(angle); 
    delay(50); 
    } 

  // bad dust (keep window closed)
  else if(ugm3>30)
  {
    Serial.println("nd");
    BTSerial.print("nd");
    delay(50);
    
    angle = 85;
    servo.write(angle); 
    delay(50);
  }

  // good dust (make window opened)
  else 
  {
   Serial.println("gd");
   BTSerial.print("gd");
   
   angle = 0;
   servo.write(angle); 
   delay(50);

   digitalWrite(8, LOW);
   digitalWrite(9, HIGH);
   analogWrite(10, 60);
   delay(10000);
  }
}
 
else if(analogRead(0)<=500 & angle==85) // 3rd_ rain! (window closed_ not open)
{
   if(analogRead(0)<300)   // heavy rain
   {
   Serial.println("hr");
   BTSerial.print("hr");
   delay(50);
   }

   else   // normal rain
   {
   Serial.println("nr");
   BTSerial.print("nr");
   delay(50);
   }
  
   Serial.println("gd");
   BTSerial.print("gd");
}
  
else if(analogRead(0)<=500 & angle==0) // final_ rain! (window open_ make closed)
{
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    analogWrite(10, 60);
    delay(10000);//window closed
    
    angle = 85;
    servo.write(angle); 
    delay(50); 

   if(analogRead(0)<300)   // heavy rain
   {
   Serial.println("hr");
   BTSerial.print("hr");
   delay(50);
   }

   else   // normal rain
   {
   Serial.println("nr");
   BTSerial.print("nr");
   delay(50);
   }
  
   Serial.println("gd");
   BTSerial.print("gd");
}

이전글   |    아두이노 가스감지기센서 도와주세요 2017-06-14
다음글   |    ESP8266 AP 여러개 사용하기 2017-06-15