ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Arduino] 스텝모터, 서보 모터, IR 수신
    전기전자공학/실습 2018. 1. 29. 17:37

    0~180, 180~0 도 사이로 서버모터 구동!


    #include <Servo.h>


    #define SERVO_PIN 9


    Servo myservo;

    int pos = 0;


    void setup()

    {

      myservo.attach(SERVO_PIN);


    }


    void loop() {

      for(pos = 0; pos < 180; pos+= 1)

      {

        myservo.write(pos);

        delay(15);

      }


      for(pos = 180; pos >= 1; pos -= 1)

      {

        myservo.write(pos);

        delay(15);

      }

    }



    x,y  조이스틱의 x 값으로 servo모터 변경


    #include <Servo.h>


    #define X_PIN A2

    #define Y_PIN A3

    #define SW_PIN 7


    #define SERVO_PIN 9

    #define POTEN_PIN A0


    Servo myservo;


    int x_max;


    /*

    int temp;

    int pre = 0;

    */

    void setup()

    {

      Serial.begin(9600);

      myservo.attach(SERVO_PIN);

      pinMode(SW_PIN, INPUT);

      pinMode(X_PIN, INPUT);

    //  pinMode(Y_PIN, INPUT);

    }


    void loop()

    {

      int i = 0;


      

      int x = analogRead(X_PIN);

    //  int y = analogRead(Y_PIN);

    /*  int z = digitalRead(SW_PIN);

      

      temp = z;


      if(temp != pre)

        Serial.println(z, DEC);

     */

      if(x == 1023)

       x_max++;

      if(x_max == 2){

        myservo.write(0);

        x_max = 0;

      }

      delay(10);


      x = map(x, 0, 1023, 0, 179);

    //  y = map(y, 0, 1023, 0, 179);

    //  if(x > y)

    //    x = y;

      myservo.write(x);

      delay(20);

     

    //  pre = z;


    }




    lcd 모니터에 출력  -- 미완

    #include <Servo.h>

    #include <Wire.h>


    #include <LiquidCrystal_I2C.h>


    LiquidCrystal_I2C lcd(0x27, 16, 2);


    //#define X_PIN A2



    Servo myservo;


    int x_max;


    void setup()

    {

      lcd.init();

      lcd.backlight();

      Serial.begin(9600);

    //  myservo.attach(SERVO_PIN);


    }


    void loop()

    {

      int v;

      String str1;

      int val = 10;

      str1 = (String)val;

      Serial.println(str1);

      v = str1.toInt();

      Serial.println(v);

      

      //if(str1)

        lcd.print(str1);

         

      delay(1000);

    }


    스텝 모터 구동

    #include <Stepper.h>


    #define IN1_PIN 8

    #define IN2_PIN 9

    #define IN3_PIN 10

    #define IN4_PIN 11


    Stepper motor(64, IN1_PIN, IN2_PIN, IN3_PIN, IN4_PIN);


    void setup() {

      for(int pin = IN1_PIN; pin < IN4_PIN; pin++)

        pinMode(pin, OUTPUT);


      Serial.begin(9600);

      motor.setSpeed(100);


      Serial.println("Speed : 100rpm");

      Serial.println("Enter steps: ");


    }


    void loop() {

      if(Serial.available())

      {

        int steps = Serial.parseInt();

        Serial.println(steps);

        motor.step(steps);

        Serial.print("Enter steps: ");

      }

    }



    IR  수신 정보 출력 및 LED 제어

    #include <boarddefs.h>

    #include <IRremote.h>

    #include <IRremoteInt.h>

    #include <ir_Lego_PF_BitStreamEncoder.h>




    #define IR_PIN 11

    #define LED_PIN 13


    boolean lightState = false;

    unsigned long last = millis();


    IRrecv  irrecv(IR_PIN);

    decode_results decResult;


    void setup()

    {

      Serial.begin(9600);

      pinMode(LED_PIN, OUTPUT);

      irrecv.enableIRIn();

    }


    void loop()

    {

      if(irrecv.decode(&decResult) == true)

      {

        Serial.println(decResult.value, HEX);

           delay(150);

        Serial.println(getDecodetype(decResult.decode_type));


        if(millis() - last > 250) {

          lightState = !lightState;

          digitalWrite(LED_PIN, lightState);

          

        }

        last = millis();

     

          irrecv.resume();

      }

      

    }

    String getDecodetype(int type)

      {

        String szType = "Type : ";

        switch(type)

        {

          case NEC: szType.concat("NEC");         break;

          case SONY: szType.concat("SONY");       break;

          case SAMSUNG: szType.concat("SAMSUNG"); break;

          case LG: szType.concat("LG");           break;

          default: szType.concat("Unknown");      break;

        }

        return szType;

      }


    댓글

Designed by Tistory.