아두이노 관련 멘토를 맡게 되었다.


아두이노를 해본 적이 없는데 맡게 되어서 좀 당황스럽다.


코딩 및 아이디어 도출에 대한 조언을 목적으로 멘토를 뽑았다고 한다.


학생들이 처음 받은 과제는 "서브모터 3개를 활용하여 작품을 만들기" 이다.


서브모터 3개를 활용하여 집게를 만들기로 하였다.


코딩은 다음과 같이 진행을 하였다.


#include <Servo.h>

const int button1Pin = 2;
const int motor1Pin = 9;
const int motor2Pin = 10;
const int motor3Pin = 11;
int motor1moving = true;//false 면 a->b, true면 b->a
int motor2moving = true;
int motor3moving = true;
int buttonPress = false;
Servo motor1;
Servo motor2;
Servo motor3;
bool isRight = false;
int s = 0;
int e = 180;

int motor1angle = 0;
int motor2angle = 0;
int motor3angle = 0;

// 실행시 가장 먼저 호출되는 함수이며, 최초 1회만 실행됩니다.
void setup()
{
  pinMode(button1Pin, INPUT);
  motor1.attach(motor1Pin);
  motor2.attach(motor2Pin);
  motor3.attach(motor3Pin);
  motor1.write(motor1angle);
  motor2.write(motor2angle);
  motor3.write(motor3angle);
}

// setup() 함수가 호출된 이후, loop() 함수가 호출되며,
// 블록 안의 코드를 무한히 반복 실행합니다.
void loop()
{
  int button1State;
  if (buttonPress == false)
  {
    button1State = digitalRead(button1Pin);
    if (button1State == HIGH)
    {
      buttonPress = true;
    }
  }
 if (buttonPress == true)
   {
      if (motor1moving == motor2moving && motor2moving == motor3moving)
      {
        if (motor1moving)
          isRight = true;
        else
          isRight = false;
      }
      if (isRight)
      {
        if (motor1moving == isRight)
        {
          motor1.write(++motor1angle);
	delay(15);
          if (motor1angle == e)
            motor1moving = false;
        }
        else if (isRight == motor2moving)
        {
          motor2.write(++motor2angle);
	delay(15);
          if (motor2angle == e)
            motor2moving = false;
        }
        else if (isRight == motor3moving)
        {
          motor3.write(++motor3angle);
	delay(15);
          if (motor3angle == e)
          {
            buttonPress = false;
            motor3moving = false;
          }
        }
      }
      else
      {
        if (motor1moving == isRight) {
          motor3.write(--motor3angle);
	delay(15);
          if (motor3angle == s)
            motor1moving = true;
        }
        else if (isRight == motor2moving)
        {
          motor2.write(--motor2angle);
	delay(15);
          if (motor2angle == s)
            motor2moving = true;
        }
        else if (isRight == motor3moving)
        {
          motor1.write(--motor1angle);
	delay(15);
          if (motor1angle == s)
          {
            buttonPress = false;
            motor3moving = true;
          }
        }
      }
    }
}

완전한 구조까지 짜줄 수는 없지만 기본 코드로 사용할 수 있도록 다음과 같이 작성하였다.


기능은 다음과 같다.


1. 버튼을 지속적으로 누르는 것이 아닌 1회 버튼 클릭 시 모터가 일정 조건을 만족할 때까지 회전


2. 모터 3개가 1 - 2 - 3의 순서로 순차적으로 회전


3. 순차적으로 회전이 완료된 후 버튼을 재클릭 시, 3 - 2 - 1의 역순으로 회전


집게를 만든다고 할 때, 몸체가 되는 1번 모터가 회전을 하고, 팔꿈치에 해당하는 2번 모터가 회전,


마지막으로 집게에 해당하는 3번 모터가 회전을 한다.


다시 버튼을 눌렀을 때는 집게에 해당하는 3번 모터, 팔꿈치에 해당하는 2번 모터, 몸체에 해당하는 1번 모터가 회전하여


초기 위치로 돌아올 수 있게 한다.


초기 위치에서 물건을 쥐어주고 버튼을 누르면 특정 위치로 이동 후, 물건을 놓은 후에 다시 버튼을 누르면 초기 위치로 돌아온다.


기본 동작 자체는 별 것 없는데 delay()가 필수라던가 하는 부분에서 조금 꼬였다.


#include <Servo.h>

const int button1Pin = 13;
// led를 디지털 13번 핀에 연결합니다.
const int motor1Pin = 9;
const int motor2Pin = 8;
const int motor3Pin = 7;
int motor1moving = true;//false 면 a->b, true면 b->a
int motor2moving = true;
int motor3moving = true;
int buttonPress = false;
Servo motor1;
Servo motor2;
Servo motor3;
bool isRight = false;
int defaultAngle = 0;
int secondAngle = 45;
int thirdAngle = 90;
int maxAngle = 180;

int motor1angle = 0;
int motor2angle = 0;
int motor3angle = 0;

// 실행시 가장 먼저 호출되는 함수이며, 최초 1회만 실행됩니다.
void setup()
{
  pinMode(button1Pin, INPUT);
  motor1.attach(motor1Pin);
  motor2.attach(motor2Pin);
  motor3.attach(motor3Pin);
  motor1.write(motor1angle);
  motor2.write(motor2angle);
  motor3.write(motor3angle);
}

// setup() 함수가 호출된 이후, loop() 함수가 호출되며,
// 블록 안의 코드를 무한히 반복 실행합니다.
void loop()
{
  int button1State;
  if (buttonPress == false)
  {
    button1State = digitalRead(button1Pin);
    if (button1State == HIGH)
    {
      buttonPress = true;
    }
  }
  if (buttonPress == true)
  {
    if (motor1moving == motor2moving && motor2moving == motor3moving)
    {
      if (motor1moving)
        isRight = true;
      else
        isRight = false;
    }
    if (isRight)
    {
      if (motor1moving == isRight) 
      {
        motor1.write(++motor1angle);
        if (motor1angle == maxAngle)//1모터가 180도까지 다 돌았을 경우
          motor1moving = false;//모터 first 완료
          delay(15);
      }
      else if (isRight == motor2moving)
      {
        motor2.write(++motor2angle);
        if (motor2angle == thirdAngle)//1모터가 180도까지 다 돌았을 경우
          motor2moving = false;//모터 first 완료
          delay(15);
      }
      else if (isRight == motor3moving)
      {
        motor3.write(++motor3angle);
        if (motor3angle == secondAngle)//1모터가 180도까지 다 돌았을 경우
        {
          buttonPress = false;
          motor3moving = false;//모터 first 완료
          delay(15);
        }
      }
    }
    else
    {
      if (motor1moving == isRight) {
        motor3.write(--motor3angle);
        if (motor3angle == defaultAngle)//3모터가 0도까지 다 돌았을 경우
          motor1moving = true;//모터 first 완료
          delay(15);
      }
      else if (isRight == motor2moving)
      {
        motor2.write(--motor2angle);
        if (motor2angle == defaultAngle)//1모터가 180도까지 다 돌았을 경우
          motor2moving = true;//모터 first 완료
          delay(15);
      }
      else if (isRight == motor3moving)
      {
        motor1.write(--motor1angle);
        if (motor1angle == defaultAngle)//1모터가 180도까지 다 돌았을 경우
        {
          buttonPress = false;
          motor3moving = true;//모터 first 완료
          delay(15);
        }
      }
    }
  }
}

학생들이 위의 코드를 바탕으로 수정한 내용.


bool 변수 비교가 살짝 문제가 있었는지 수정을 하였고 결과가 괜찮게 나왔다.




+ Recent posts